请教!请教!精度问题
<P>用2分法计算时,同一个程序什么都不改动,居然2次搜出来的根不一样,怀疑是精度问题,哪位好心的朋友能给解答一下呀,该怎么改呢!!!???小女子万分感谢!!!</P>回复:(stu000)请教!请教!精度问题
试试vpa 使用format long可将精度提高回复:(ilfl)使用format long可将精度提高
<DIV class=quote><B>以下是引用<I>ilfl</I>在2006-4-14 15:51:41的发言:</B><BR>使用format long可将精度提高 </DIV><br>这个只是显示精度 <P>能不能换一种求根方法:solve ,roots ,fzero</P>
回复:(xjzhang)能不能换一种求根方法:solve ,root...
<DIV class=quote><B>以下是引用<I>xjzhang</I>在2006-4-14 20:35:00的发言:</B><BR><P>能不能换一种求根方法:solve ,roots ,fzero</P></DIV>
<br>什么意思? 谢谢各位好心的哥哥!!!不好意思,回复晚了呀:)小女子把程序贴出来,哪位好心的哥哥帮我看看呀!<BR><BR>global x ;<BR> xhr=zeros(200);<BR> vel=zeros(200,100);<BR>xtol=1.0e-6;<BR>xhs=0.25;<BR>xhe=0.25;<BR>nx=1;<BR>cs=0.90<BR>ce=0.905<BR>nc=20;<BR>=velsearch(nx,xhs,xhe,nc,cs,ce,xtol);<BR>% Plot the velocity verses the length ratio.<BR>plot(xhr,vel,'k.')<BR>axis()<BR>a=vel <P>function fp=eqn_w3(v,xh);<BR>% xh=0.25;<BR>% v=0.90;</P>
<P>xhh=0.25;<BR>roub=19300;<BR>rou=3879;<BR>c11b=18.6*10^10;<BR>c44b=4.2*10^10;<BR>c11=6.13*10^10;<BR>c44=2.18*10^10;<BR>lambda=c11-2*c44;<BR>lambdab=c11b-2*c44b;<BR>mu=c44;<BR>mub=c44b;</P>
<P>l2m = lambda + 2 * mu;</P>
<P>l2mb = lambdab + 2 * mub;<BR>% 1.不带电极层区域 <BR>hhhh=10;<BR>H=0.002;</P>
<P><BR>beta1=0.83797530;<BR>beta2=0.40325621;</P>
<P>beta(1)=beta1;<BR>beta(2)=beta2;<BR>Beta1=0.84081852;<BR>Beta2=0.41956813;<BR>Beta(1)=Beta1;<BR>Beta(2)=Beta2;</P>
<P>% Integration of the thickness expansion functions for large depth h<BR>fact1 = 1 - 1 / exp(2 * pi * (beta1 + beta1) * hhhh);<BR>fact2 = 1 - 1 / exp(2 * pi * (beta2 + beta2) * hhhh);<BR>fact12= 1 - 1 / exp(2 * pi * (beta1 + beta2) * hhhh);<BR>f11 = fact1/ (beta1 + beta1);<BR>f12 = fact12 / (beta1 + beta2);<BR>f21 = f12;<BR>f22 = fact2/ (beta2 + beta2);</P>
<P>% Define the frequency matrix A(4, 4)<BR>syms k;<BR>A(1, 1) =f11 * (l2m * k^2 + mu * beta1^2 - mu * v^2 );<BR>A(1, 2) =f11 * beta1 * (lambda - mu) * k;<BR>A(1, 3) =f21 * (l2m * k^2 + mu * beta1 * beta2 - mu * v^2);<BR>A(1, 4) =f21 * (lambda * beta2 - mu * beta1) * k;<BR>A(2, 1) =f11 * beta1 * (lambda - mu) * k;<BR>A(2, 2) =f11 * (mu * k^2 + l2m * beta1^2 - mu * v^2 );<BR>A(2, 3) =f21 * (beta1 * lambda - beta2 * mu) * k;<BR>A(2, 4) =f21 * (mu * k^2 + l2m * beta1 * beta2 - mu * v^2);<BR>A(3, 1) =f21 * (l2m * k^2 + mu * beta1 * beta2 - mu * v^2);<BR>A(3, 2) =f21 * (beta1 * lambda - beta2 * mu) * k;<BR>A(3, 3) =f22 * (l2m * k^2 + mu * beta2^2 - mu * v^2);<BR>A(3, 4) =f22 * beta2 * (lambda - mu) * k;<BR>A(4, 1) =f21 * (lambda * beta2 - mu * beta1) * k;<BR>A(4, 2) =f21 * (mu * k^2 + l2m * beta1 * beta2 - mu * v^2);<BR>A(4, 3) =f22 * beta2 * (lambda - mu) * k;<BR>A(4, 4) =f22 * (mu * k^2 + l2m * beta2^2 - mu * v^2);</P>
<P>y=det(A);</P>
<P> kk=solve(y,k);</P>
<P> G(1)=numeric(real(kk(1)));<BR> G(2)=numeric(real(kk(3)));<BR> G(3)=numeric(imag(kk(5))*i);<BR> G(4)=numeric(imag(kk(7))*i);<BR> </P>
<P> <BR> </P>
<P>for n=1:4,<BR> d(1,1)=f11 * (l2m * G(n)^2 + mu * beta1^2 - mu * v^2 );<BR> d(1,2)=f11 * beta1 * (lambda - mu) * G(n) ;<BR> d(1,3)=f21 * (l2m * G(n)^2 + mu * beta1 * beta2 - mu * v^2);<BR> d(2,1)=f11 * beta1 * (lambda - mu) * G(n) ;<BR> d(2,2)=f11 * (mu * G(n)^2 + l2m * beta1^2 - mu * v^2 );<BR> d(2,3)=f21 * (beta1 * lambda - beta2 * mu) * G(n);<BR> d(3,1)=f21 * (l2m * G(n)^2 + mu * beta1 * beta2 - mu * v^2);<BR> d(3,2)=f21 * (beta1 * lambda - beta2 * mu) * G(n);<BR> d(3,3)=f22 * (l2m * G(n)^2 + mu * beta2^2 - mu * v^2);<BR> h(1,1)=-f21 * (lambda * beta2 - mu * beta1) * G(n);</P>
<P> h(2,1)=-f21 * (mu * G(n)^2 + l2m * beta1 * beta2 - mu * v^2);</P>
<P> h(3,1)=-f22 * beta2 * (lambda - mu) * G(n);</P>
<P>al(:,n)=d\h;</P>
<P>end;</P>
<P><BR>% new lame constants<BR>for m=1:2<BR> for n = 1 : 2<BR> lambdabb(m,n) = lambda * (1+2*H*(Beta(m)+Beta(n))*lambdab/lambda);<BR> mubb(m,n)=mu*(1+2*H*(Beta(m)+Beta(n))*mub/mu);<BR> l2mbb(m,n)=l2m*(1+2*H*(Beta(m)+Beta(n))*l2mb/l2m);<BR> rour(m,n)=(1+2*(Beta(m)+Beta(n))*H*roub/rou);<BR> end;<BR>end;</P>
<P><BR>% 基体厚度hhhh 电极厚度H (归一化后)<BR>% Integration of the thickness expansion functions for large depth h<BR>Fact1 = 1 - 1 / exp(2 * pi * (Beta1 + Beta1) * hhhh);<BR>Fact2 = 1 - 1 / exp(2 * pi * (Beta2 + Beta2) * hhhh);<BR>Fact12= 1 - 1 / exp(2 * pi * (Beta1 + Beta2) * hhhh);<BR>F11 = Fact1/ (Beta1 + Beta1);<BR>F12 = Fact12 / (Beta1 + Beta2);<BR>F21 = F12;<BR>F22 = Fact2/ (Beta2 + Beta2);</P>
<P>% Define the frequency matrix A(4, 4)<BR>syms K;<BR>AA(1, 1) = F11 * (l2mbb(1,1) * K^2 + mubb(1,1) * Beta1^2 - mu * v^2 * rour(1,1));<BR>AA(1, 2) = + F11 * Beta1 * (lambdabb(1,1) - mubb(1,1)) * K ;<BR>AA(1, 3) = F21 * (l2mbb(1,2) * K^2 + mubb(1,2) * Beta1 * Beta2 - mu * v^2 * rour(1,2));<BR>AA(1, 4) = + F21 * (lambdabb(1,2) * Beta2 - mubb(1,2) * Beta1) * K;<BR>AA(2, 2) = F11 * (mubb(1,1) * K^2 + l2mbb(1,1) * Beta1^2 - mu * v^2 * rour(1,1));<BR>AA(2, 3) = + F21 * (Beta1 * lambdabb(1,2) - Beta2 * mubb(1,2)) * K;<BR>AA(2, 4) = F21 * (mubb(1,2) * K^2 + l2mbb(1,2) * Beta1 * Beta2 - mu * v^2 * rour(1,2));<BR>AA(3, 3) = F22 * (l2mbb(2,2) * K^2 + mubb(2,2) * Beta2^2 - mu * v^2 * rour(2,2));<BR>AA(3, 4) = + F22 * Beta2 * (lambdabb(2,2) - mubb(2,2)) * K;<BR>AA(4, 4) = F22 * (mubb(2,2) * K^2 + l2mbb(2,2) * Beta2^2 - mu * v^2 * rour(2,2));<BR>AA(2, 1) = AA(1, 2);<BR>AA(3, 1) = AA(1, 3);<BR>AA(4, 1) = AA(1, 4);<BR>AA(4, 3) = AA(3, 4); <BR>AA(3, 2) = AA(2, 3);<BR>AA(4, 2) = AA(2, 4);<BR>yy = det(AA);<BR>% 并求振幅比AL(m,n)<BR>KK=solve(yy,K);<BR> GG(1)=numeric(real(KK(1)));<BR> GG(2)=numeric(real(KK(3)));<BR> GG(3)=numeric(imag(KK(5))*i);<BR> GG(4)=numeric(imag(KK(7))*i);</P>
<P>for n=1:4,<BR> dd(1,1)=F11 * (l2mbb(1,1) * GG(n)^2 + mubb(1,1) * Beta1^2 - mu * v^2 * rour(1,1));<BR> dd(1,2)=F11 * Beta1 * (lambdabb(1,1) - mubb(1,1)) * GG(n) ;<BR> dd(1,3)=F21 * (l2mbb(1,2) * GG(n)^2 + mubb(1,2) * Beta1 * Beta2 - mu * v^2 * rour(1,2));<BR> dd(2,1)=F11 * Beta1 * (lambdabb(1,1) - mubb(1,1)) * GG(n);<BR> dd(2,2)=F11 * (mubb(1,1) * GG(n)^2 + l2mbb(1,1) * Beta1^2 - mu * v^2 * rour(1,1));<BR> dd(2,3)=F21 * (Beta1 * lambdabb(1,2) - Beta2 * mubb(1,2)) * GG(n);<BR> dd(3,1)=F21 * (l2mbb(1,2) * GG(n)^2 + mubb(1,2) * Beta1 * Beta2 - mu * v^2 * rour(1,2));<BR> dd(3,2)=F21 * (Beta1 * lambdabb(1,2) - Beta2 * mubb(1,2)) * GG(n);<BR> dd(3,3)=F22 * (l2mbb(2,2) * GG(n)^2 + mubb(2,2) * Beta2^2 - mu * v^2 * rour(2,2));<BR> <BR> <BR> <BR> hh(1,1)=-F21 * (lambdabb(1,2) * Beta2 - mubb(1,2) * Beta1) * GG(n);</P>
<P> hh(2,1)=-F21 * (mubb(1,2) * GG(n)^2 + l2mbb(1,2) * Beta1 * Beta2 - mu * v^2 * rour(1,2));</P>
<P> hh(3,1)=-F22 * Beta2 * (lambdabb(2,2) - mubb(2,2)) * GG(n);</P>
<P><BR> Al(:,n)=dd\hh;</P>
<P> end;</P>
<P><BR>for n=1:4<BR> ddd(1,1)=F11 * (l2mbb(1,1) * GG(n)^2 + mubb(1,1) * Beta1^2 - mu * v^2 * rour(1,1));<BR> ddd(1,2)=-F11 * Beta1 * (lambdabb(1,1) - mubb(1,1)) * GG(n) ;<BR> ddd(1,3)=F21 * (l2mbb(1,2) * GG(n)^2 + mubb(1,2) * Beta1 * Beta2 - mu * v^2 * rour(1,2));<BR> ddd(2,1)=-F11 * Beta1 * (lambdabb(1,1) - mubb(1,1)) * GG(n);<BR> ddd(2,2)=F11 * (mubb(1,1) * GG(n)^2 + l2mbb(1,1) * Beta1^2 - mu * v^2 * rour(1,1));<BR> ddd(2,3)=-F21 * (Beta1 * lambdabb(1,2) - Beta2 * mubb(1,2)) * GG(n);<BR> ddd(3,1)=F21 * (l2mbb(1,2) * GG(n)^2 + mubb(1,2) * Beta1 * Beta2 - mu * v^2 * rour(1,2));<BR> ddd(3,2)=-F21 * (Beta1 * lambdabb(1,2) - Beta2 * mubb(1,2)) * GG(n);<BR> ddd(3,3)=F22 * (l2mbb(2,2) * GG(n)^2 + mubb(2,2) * Beta2^2 - mu * v^2 * rour(2,2));<BR> hhh(1,1)=F21 * (lambdabb(1,2) * Beta2 - mubb(1,2) * Beta1) * GG(n);</P>
<P> hhh(2,1)=-F21 * (mubb(1,2) * GG(n)^2 + l2mbb(1,2) * Beta1 * Beta2 - mu * v^2 * rour(1,2));</P>
<P> hhh(3,1)=F22 * Beta2 * (lambdabb(2,2) - mubb(2,2)) * GG(n);</P>
<P><BR> Bl(:,n)=ddd\hhh;<BR>end;</P>
<P><BR>% 3.验证边界条件<BR>for m=1:4<BR>zterm=pi*GG(m)*xhh;<BR>ztermm=pi*G(m)*xhh;<BR>ztermmm=pi*G(m)*(xh+xhh);<BR> = trigimag(zterm);<BR>=trigimag(ztermm);<BR>=trigimag(ztermmm);</P>
<P>T(1,m)=((F11*l2mbb(1,1)*Al(1,m)+F21*l2mbb(1,2)*Al(3,m))*GG(m)+(F11*lambdabb(1,1)*Beta1*Al(2,m)+F21*lambdabb(1,2)*Beta2))*cosQQ;<BR>T(1,m+4)=(-(F11*l2mbb(1,1)*Bl(1,m)+F21*l2mbb(1,2)*Bl(3,m))*GG(m)+(F11*lambdabb(1,1)*Beta1*Bl(2,m)+F21*lambdabb(1,2)*Beta2))*sinQQ;<BR>T(1,m+8)=-(l2m * (f11 * al(1, m) + f12 * al(3, m))*G(m) + lambda * (f11 * beta1 * al(2, m) + f12 * beta2)) * cosU;</P>
<P>T(2,m)=((-F11*mubb(1,1)*Al(2,m)-F21*mubb(1,2))*GG(m)+F11*mubb(1,1)*Beta1*Al(1,m)+F21*mubb(1,2)*Beta2*Al(3,m))*sinQQ;<BR>T(2,m+4)=((F11*mubb(1,1)*Bl(2,m)+F21*mubb(1,2))*GG(m)+F11*mubb(1,1)*Beta1*Bl(1,m)+F21*mubb(1,2)*Beta2*Bl(3,m))*cosQQ;<BR>T(2,m+8)=-mu*((-f11 * al(2, m)-f21)*G(m) + f11 * beta1 * al(1, m) + f21 * beta2* al(3, m)) * sinU;<BR> </P>
<P><BR>T(3,m)=((F21*l2mbb(1,2)*Al(1,m)+F22*l2mbb(2,2)*Al(3,m))*GG(m)+(F21*lambdabb(1,2)*Beta1*Al(2,m)+F22*lambdabb(2,2)*Beta2))*cosQQ;<BR>T(3,m+4)=(-(F21*l2mbb(1,2)*Bl(1,m)+F22*l2mbb(2,2)*Bl(3,m))*GG(m)+(F21*lambdabb(1,2)*Beta1*Bl(2,m)+F22*lambdabb(2,2)*Beta2))*sinQQ;<BR>T(3,m+8)=-(l2m *(f21 * al(1,m)+f22*al(3,m))*G(m)+lambda*(f21 * beta1 * al(2, m) + f22 * beta2)) * cosU;</P>
<P> </P>
<P>T(4,m)=((-F21*mubb(1,2)*Al(2,m)-F22*mubb(2,2))*GG(m)+F21*mubb(1,2)*Beta1*Al(1,m)+F22*mubb(2,2)*Beta2*Al(3,m))*sinQQ;<BR>T(4,m+4)=((F21*mubb(1,2)*Bl(2,m)+F22*mubb(2,2))*GG(m)+F21*mubb(1,2)*Beta1*Bl(1,m)+F22*mubb(2,2)*Beta2*Bl(3,m))*cosQQ;<BR>T(4,m+8)=-mu*((-f21 * al(2, m)-f22)*G(m) + f21 * beta1 * al(1, m) + f22 * beta2* al(3, m)) * sinU;</P>
<P><BR>T(5,m)=Al(1,m)*sinQQ;<BR>T(5,m+4)=Bl(1,m)*cosQQ;<BR>T(5,m+8)=-al(1,m)*sinU;</P>
<P><BR>T(6,m)=Al(2,m)*cosQQ;<BR>T(6,m+4)=Bl(2,m)*sinQQ;<BR>T(6,m+8)=-al(2,m)*cosU;</P>
<P>T(7,m)=Al(3,m)*sinQQ;<BR>T(7,m+4)=Bl(3,m)*cosQQ;<BR>T(7,m+8)=-al(3,m)*sinU;</P>
<P> <BR>T(8,m)=cosQQ;<BR>T(8,m+4)=sinQQ;<BR>T(8,m+8)=-cosU;</P>
<P>T(9,m)=0;<BR>T(9,m+4)=0;<BR>T(9,m+8)=-al(1,m)*sinQ;</P>
<P>T(10,m)=0;<BR>T(10,m+4)=0;<BR>T(10,m+8)=-al(3,m)*sinQ;</P>
<P>T(11,m)=0;<BR>T(11,m+4)=0;<BR>T(11,m+8)=-mu*((-f11 * al(2, m)-f21)*G(m) + f11 * beta1 * al(1, m) + f21 * beta2* al(3, m)) * sinQ;</P>
<P><BR>T(12,m)=0;<BR>T(12,m+4)=0;<BR>T(12,m+8)=-mu*((-f21 * al(2, m)-f22)*G(m) + f21 * beta1 * al(1, m) + f22 * beta2* al(3, m)) * sinQ;<BR>end </P>
<P>fp=numeric(det(T))</P> function =velsearch(nx,xhs,xhe,nc,cs,ce,xtol)<BR>%global x c c6;<BR> % set up the biscection search procedure.<BR>%xh0=20.0;<BR>%xtol=1.0e-10<BR>ir=0;<BR>% veldat=fopen('vel.dat','w');<BR> %nx=1;<BR>for i=1:nx,<BR> v=xhs+(i-1)*(xhe-xhs)/nx;<BR> % consider the at-cut of quartz (32.15 Y-X)<BR> % phi=pi*xh/180;<BR> % =elastranyx(phi);<BR> %nc=50<BR> %v=40<BR> %cs=0.9;ce=1.2;<BR> x=cs<BR> ds=(ce-cs)/nc;<BR> dso=ds;<BR> fo=(eqn_w3(x,v));<BR> if abs(real(fo))<abs(imag(fo));<BR> fo=imag(fo);<BR> else<BR> fo=real(fo);<BR> end;<BR> %=wwavenums1(xh);<BR> ir=0;<BR> for j=1:1000<BR> x=x+ds;<BR> if x>ce break;end;<BR> fp=(eqn_w3(x,v));<BR> if abs(real(fp))<abs(imag(fp));<BR> fp=imag(fp);<BR> else<BR> fp=real(fp);<BR> end;<BR> <BR> <BR> %=wwavenums1(xh);<BR> %fprintf(JJJ=%4i XXX=%13.10f OOO=%10.4e FFF=%10.4e DDD=%10.4e\n',...<BR> % j,x,fo,fp,ds)<BR> if (fo*fp>0)<BR> fo=fp;<BR> <BR> <BR> %elseif (ds<xtol &abs(fp)<xtol)<BR> elseif (ds<xtol)<BR> % 'XH= ',xh,'Root= ',x<BR> %fprintf('IRRR= %4i XH= %12.8f Root=%15.12f\n',ir,xh,x);<BR> %fprintf('Beta(1)=%15.12f\n real(2)=%15.12f\n Imag(2)=%15.12f\n',...<BR> % beta(1),real(beta(2)),abs(imag(beta(2))));<BR> %fp,fo<BR> %fprintf(veldat,' %10.4f %13.10f\n',xh,x);<BR> %break;<BR> ir=ir+1;<BR> xhr(i)=v<BR> vel(i,ir)=x<BR> save velxhr.mat xhr vel;<BR> fo=fp;<BR> ds=dso;<BR> <BR> %break;<BR> else<BR> %fprint('JJJ= %4iXXX=%13.10f OOO=%10.4e FFF=%10.4e DDD=%10.4e\n',...<BR> %j,x,fo,fp,ds)<BR> x=x-ds;<BR> ds=ds/2;<BR> <BR> end;<BR> end;<BR>end;<BR> %fclose(veldat);<BR>end <BR>end<BR> <P>function = trigimag(zterm)</P>
<P>cosl = 0;<BR>sinl = 0;</P>
<P>% No scaling is needed if the argument is real.<BR>if (imag(zterm) == 0)<BR> sinl = sin(zterm);<BR> cosl = cos(zterm);<BR> <BR>% Scale the sine and cosine functions in case of imagnary argument<BR>elseif (imag(zterm) > 0) %exp(imag(zterm))<BR> chl= 0.5 * (1 + exp( - 2 * imag(zterm)));<BR> shl= 0.5 * (1 - exp( - 2 * imag(zterm)));<BR> cosl = cos(real(zterm)) * chl - i * sin(real(zterm)) * shl;<BR> sinl = sin(real(zterm)) * chl + i * cos(real(zterm)) * shl;<BR> <BR>else %exp( - imag(zterm))<BR> chl= 0.5 * (1 + exp(2 * imag(zterm)));<BR> shl= 0.5 * (- 1 + exp(2 * imag(zterm)));<BR> cosl = cos(real(zterm)) * chl - i * sin(real(zterm)) * shl;<BR> sinl = sin(real(zterm)) * chl + i * cos(real(zterm)) * shl;<BR>end;<BR>clear chl shl </P> <P>vpa命令也都用了,还是不行<br><br>现在就是仅仅在xh=0.25这个点在0.90---0.905之间来搜根,一般会搜出4,5个根左右,但是我同一个程序还是同一个点,搜索范围也一样,什么都不变,,前后两次运行同一个程序,居然搜索出来的根跟前一次不一样,超级郁闷,应该是计算程序的精度问题,但是小女子实在找不出错误在哪里,麻烦哪位好心的大哥一定费心帮我看看,我现在搞的是毕业论文,咳,要是弄不出来就毕业不了啊,郁闷:( 麻烦各位了,定有报答:)</P>
[此贴子已经被作者于2006-4-16 15:32:06编辑过]
顶。。。。。。。。最后算出的det(T)值很大啊,有10的60次方,我想把程序中比较大的数都除以一个十的几次方什么的。<BR>按理说我前面把精度提高到了150,应该可以算出相同的结果啊,结果还是不行<BR>郁闷。。。。。。。。。。。
回复:(stu000)请教!请教!精度问题
<P>个人认为,你这个问题不是精度的问题,还是算法或者程序的问题<BR>即使精度不够高,但是两次计算你没有改变任何东西且程序中不存在随机性因素在里边<BR>那迭代也不会出现两个不同的解。</P><P>程序比较长,没时间帮你检查,我想这个工作应该你自己来完成</P>
<P>所以建议还是好好查查程序,看程序中是否存在随机性因素,可以用一个简单的模型验证一下程序的正确性</P>
<P>另外根据你的说法,这个方程应该是个病态方程,可以考虑除以个什么数,当然也可以换个算法,比如超松弛迭代之类的。</P> 谢谢happy老师,我再仔细检查一下!!!
...
有点晕呐
页:
[1]