iteration method numerical analysis - matrix

조회 수: 3 (최근 30일)
mehmet salihi
mehmet salihi 2021년 5월 31일
편집: mehmet salihi 2021년 5월 31일
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