1实验一:有限差分法研究静电场边值问题实验报告人:年级和班级:学号:1.实验用软件工具:Matlab2.实验原理:电磁场课本P36-381)差分方程2)差分方程组的解简单迭代法高斯-赛德尔迭代法逐次超松弛法3.实验步骤:1)简单迭代法程序:hx=41;hy=21;v1=zeros(hy,hx);v1(hy,:)=zeros(1,hx);v1(1,:)=ones(1,hx)*100;v1(:,1)=zeros(hy,1);v1(:,hx)=zeros(hy,1);v1v2=v1;maxt=1;t=0;k=0;while(maxt1e-5)k=k+1;maxt=0;fori=2:hy-1forj=2:hx-1v2(i,j)=(v1(i,j+1)+v1(i+1,j)+v1(i-1,j)+v1(i,j-1))/4;t=abs(v2(i,j)-v1(i,j));if(tmaxt)maxt=t;endendendv1=v2;endv2kclfsubplot(1,2,1),mesh(v2)axis([0,41,0,21,0,100])subplot(1,2,2),contour(v2,15)holdon2axis([-1,42,-1,25])plot([1,1,hx,hx,1],[1,hy+1,hy+1,1,1],'r')text(hx/2,0.3,'0V','fontsize',11);text(hx/2-0.5,hy+0.5,'100V','fontsize',11);text(-0.5,hy/2,'0V','fontsize',11);text(hx+0.3,hy/2,'0V','fontsize',11);holdoff当W=1e-5,迭代次数:1401次2)高斯-赛德尔迭代法程序:hx=41;hy=21;v1=ones(hy,hx);v1(hy,:)=zeros(1,hx);v1(1,:)=ones(1,hx)*100;v1(:,1)=zeros(hy,1);v1(:,hx)=zeros(hy,1);v2=v1;maxt=1;t=0;k=0;while(maxt1e-5)k=k+1;maxt=0;fori=2:hy-1forj=2:hx-1v2(i,j)=(v1(i,j+1)+v1(i+1,j)+v2(i-1,j)+v2(i,j-1))/4;t=abs(v2(i,j)-v1(i,j));if(tmaxt)maxt=t;endendendv1=v2;endv2kclfsubplot(1,2,1),mesh(v2)axis([0,41,0,21,0,100])subplot(1,2,2),contour(v2,15)holdonaxis([-1,42,-1,25])plot([1,1,hx,hx,1],[1,hy+1,hy+1,1,1],'r')text(hx/2,0.3,'0V','fontsize',11);text(hx/2-0.5,hy+0.5,'100V','fontsize',11);text(-0.5,hy/2,'0V','fontsize',11);text(hx+0.3,hy/2,'0V','fontsize',11);holdoff3当W=1e-5,迭代次数:740次3)逐次超松弛法程序:hx=41;hy=21;v1=zeros(hy,hx);v1(hy,:)=zeros(1,hx);v1(1,:)=ones(1,hx)*100;v1(:,1)=zeros(hy,1);v1(:,hx)=zeros(hy,1);v1v2=v1;maxt=1;t=0;alpha=input('pleaseinputthevalueofalpha(alpha=1&&alpha2):');k=0;while(maxt1e-5)k=k+1;maxt=0;fori=2:hy-1forj=2:hx-1v2(i,j)=v1(i,j)+(v1(i,j+1)+v1(i+1,j)+v2(i-1,j)+v2(i,j-1)-4*v1(i,j))*alpha/4;t=abs(v2(i,j)-v1(i,j));if(tmaxt)maxt=t;endendendv1=v2;endv2kclfsubplot(1,2,1),mesh(v2)axis([0,41,0,21,0,100])subplot(1,2,2),contour(v2,15)holdonaxis([-1,42,-1,25])plot([1,1,hx,hx,1],[1,hy+1,hy+1,1,1],'r')text(hx/2,0.3,'0V','fontsize',11);text(hx/2-0.5,hy+0.5,'100V','fontsize',11);text(-0.5,hy/2,'0V','fontsize',11);text(hx+0.3,hy/2,'0V','fontsize',11);holdoff当W=1e-5,alpha取不同值时迭代次数4alpha1.01.11.21.31.41.51.61.71.81.9迭代次数742616509417336263197133801614)画三维曲面图和等位线图(逐次超松弛法最佳迭代次数时)程序:hx=41;hy=21;v1=zeros(hy,hx);v1(hy,:)=zeros(1,hx);v1(1,:)=ones(1,hx)*100;v1(:,1)=zeros(hy,1);v1(:,hx)=zeros(hy,1);v1v2=v1;maxt=1;t=0;alpha=1.8;k=0;while(maxt1e-5)k=k+1;maxt=0;fori=2:hy-1forj=2:hx-1v2(i,j)=v1(i,j)+(v1(i,j+1)+v1(i+1,j)+v2(i-1,j)+v2(i,j-1)-4*v1(i,j))*alpha/4;t=abs(v2(i,j)-v1(i,j));if(tmaxt)maxt=t;endendendv1=v2;endv2kclfsubplot(1,2,1),mesh(v2)axis([0,41,0,21,0,100])subplot(1,2,2),contour(v2,15)holdonaxis([-1,42,-1,25])plot([1,1,hx,hx,1],[1,hy+1,hy+1,1,1],'r')text(hx/2,0.3,'0V','fontsize',11);text(hx/2-0.5,hy+0.5,'100V','fontsize',11);5text(-0.5,hy/2,'0V','fontsize',11);text(hx+0.3,hy/2,'0V','fontsize',11);holdoff贴图:4.实验结论(1)matlab软件在使用有限差分法研究静电场边值问题中有着重要的作用,它能够快捷有效并且准确的解决边值问题,是解决计算相对复杂问题的有效工具。(2)从各个方法的迭代次数可以看出:在给定相同的最大允许误差W的条件下,简单迭代法使用的次数最多,为1401次;高斯赛德尔迭代法可以明显减少迭代次数,其迭代次数为740次;如果应用逐次超松弛法,则迭代次数与加速收敛因子有关,且在加速收敛因子选取合适值时,可以使迭代次数相对于其它迭代方法更加显著减少。如在加速收敛因子为1.8时,其迭代次数尽为80次。(3)在以后应用迭代法解决边值问题时,应最好选用逐次超松弛法,且在使用时,应注意选取合适的加速收敛因子的值。