设有一个长直接地金属矩形槽,长a=40,宽b=20,其侧壁与底面电位均为零,顶盖电位为100V(相对值),求槽内电位分布。
利用高斯迭代求解代码如下(相邻两次迭代值最大允许误差为0.001):
a=zeros(21,41);
a(1,:)=100;
b=zeros(19,39);
c=eye(19,39);
count=1;
d=0;
while(count==1)
m=0;
for i=2:1:20
for j=2:1:40
b(i-1,j-1)=a(i,j);
a(i,j)=0.25*(a(i-1,j)+a(i+1,j)+a(i,j-1)+a(i,j+1));
c(i-1,j-1)=abs(a(i,j)-b(i-1,j-1));
if(c(i-1,j-1)<0.001)
for k=1:1:19
for n=1:1:39
if(c(k,n)<0.00001)
m=m+1;
else
m=0;
break;
end
end
if(m==0)
break;
end
if(m==741)
count=0;
end
end
end
if(count==0)
break;
end
end
if(count==0)
break;
end
end
d=d+1;
end
d
a
利用超松弛法程序如下:
d=zeros(1,10);
h=0;
for e=1:0.1:1.9
a=zeros(21,41);
a(1,:)=100;
b=zeros(19,39);
c=eye(19,39);
count=1;
g=0;
while(count==1)
m=0;
for i=2:1:20
for j=2:1:40
b(i-1,j-1)=a(i,j);
a(i,j)=a(i,j)+e*0.25*(a(i-1,j)+a(i+1,j)+a(i,j-1)+a(i,j+1)-4*a(i,j));
c(i-1,j-1)=abs(a(i,j)-b(i-1,j-1));
if(c(i-1,j-1)<0.001)
for k=1:1:19
for n=1:1:39
if(c(k,n)<0.001)
m=m+1;
else
m=0;
break;
end
end
if(m==0)
break;
end
if(m==741)
count=0;
end
end
end
if(count==0)
break;
end
end
if(count==0)
break;
end
end
g=g+1;
end
h=h+1;
e
d(1,h)=g;
end
d
觉得有用就赞一个~有问题可以在下面留言,我尽量回复~