TM11
仅供参考嗷!!
clc;
clear;
count = input(‘迭代次数:’);%迭代次数
w = 2/(1+sin(pi/(count-1)));%收敛因子
h = pi/7;a = pi/11;%矩形波导尺寸
xa = input(‘单边节点数:’);%单边节点
tol = 10e-3;
%初始化
u=zeros(xa,xa);
%内点赋值
for i = 2:(xa - 1)
for j = 2:(xa - 1)
u(i , j) = 1;
end
end
%边界赋值
u(1,:) = 0; u(xa,:)= 0;
u(:,1) = 0; u(:,xa)= 0;
%kc初始化
kc = 0.1;
%迭代初始化
k = 0;
sum1 = 0; sum2 = 0; sum = 0;
%差分(超松弛)
while k < count
err = 0;
temp = u(i,j);
for i = 2 : xa - 1
for j = 2: xa - 1
u(i,j)=u(i,j)+w*((((u(i+1,j)+u(i,j+1)+u(i-1,j)+u(i,j-1)))/(4-(kc*h)^2))-u(i,j));
err = err + abs(temp - u(i,j));
end
end
k = k + 1;
%对kc进行迭代
for i = 2: xa-1
for j = 2 : xa-1
sum1 = sum1 + u(i,j)(u(i+1,j)+u(i,j+1)+u(i-1,j)+u(i,j-1)-4u(i,j));
sum2 = sum2 + u(i,j)^2;
end
end
kc = (sqrt(-sum1/sum2))/h;
%保证精确度
if(err
break
end
end
surf(u);
title(‘TM11’);