iteration method numerical analysis - matrix

2 次查看(过去 30 天)
hello every one, i am trying to form an iiteration loop to converge the best solution
my code is here, could you please tell me how to put this in iteration loop and can control the iteration number according to various conditions
%%% Point Gauss Seidel (PGS)
close all, clc
format compact
%Defining the constants
L=1; % length
H=2; % Height
deltax=0.05
deltay=0.05
Beta=deltax/deltay
Beta2=Beta^2
jn=H/deltay % Maximum number of grid points along y
im=L/deltax % Maximum number of grid points along x
T1=100; T2=0; T3=0; T4=0; % boundary conditions
Errormax=0.01;
y=2:-deltay:0;
x=0:deltax:L;
Told=zeros(jn+1,im+1);
% set boundary conditions
Told(1,1:im+1)=T1;
Told(jn+1,1:im+1)=T3;
Told(2:jn+1,1)=T2;
Told(2:jn+1,im+1)=T4;
% Iteration 1
for i=2:jn
for j=2:im
Told(i,j)=(1/(2*(1+Beta2)))* (Told(i-1,j)+Told(i+1,j)+Beta2*(Told(i,j+1)+Told(i,j-1)) ) ;
end
end
% Iteration 2
for i=2:jn
for j=2:im
Told(i,j)=(1/(2*(1+Beta2)))* (Told(i-1,j)+Told(i+1,j)+Beta2*(Told(i,j+1)+Told(i,j-1)) ) ;
end
end
% Iteration 3
for i=2:jn
for j=2:im
Told(i,j)=(1/(2*(1+Beta2)))* (Told(i-1,j)+Told(i+1,j)+Beta2*(Told(i,j+1)+Told(i,j-1)) ) ;
end
end
Told=flip(Told);
disp(' ');disp(' ');
disp([y' Told(:,find(abs(x-0.0) < 0.001)) Told(:,find(abs(x-0.2) < 0.001))...
Told(:,find(abs(x-0.4) < 0.001)) Told(:,find(abs(x-0.6) < 0.001))...
Told(:,find(abs(x-0.8) < 0.001)) Told(:,find(abs(x-1.0) < 0.001))])
  17 个评论
Jan
Jan 2021-5-31
编辑:Jan 2021-5-31
@mehmet salihi: Please use the button to format your code. This improves the readability. I've sone this tody for you.
for i=2:jn
for j=2:im
Error = sum(sum(abs(T_old-T_new)),2);
end
end
This piece of code repeats the calculation of Error jn*im times. This is a waste of time. Omit the loops.
mehmet salihi
mehmet salihi 2021-5-31
编辑:mehmet salihi 2021-5-31
yes dear @Jan, i removed the double loop,
ok i find the button. thanks

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by