iteration method numerical analysis - matrix

4 Ansichten (letzte 30 Tage)
mehmet salihi
mehmet salihi am 31 Mai 2021
Bearbeitet: mehmet salihi am 31 Mai 2021
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 Kommentare
Jan
Jan am 31 Mai 2021
Bearbeitet: Jan am 31 Mai 2021
@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 am 31 Mai 2021
Bearbeitet: mehmet salihi am 31 Mai 2021
yes dear @Jan, i removed the double loop,
ok i find the button. thanks

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by