iteration method numerical analysis - matrix
이전 댓글 표시
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
mehmet salihi
2021년 5월 31일
% Initialize T_old to the initial guess values
T_old = ...;
T_new = T_old;
error = 1.0;
eps = 1e-4;
while error > eps
% calculate T_new in the double loop (write T_new everywhere in your code where you now use T_old)
error = max(abs(T_old-T_new),[],'all')
T_old = T_new;
end
mehmet salihi
2021년 5월 31일
편집: Jan
2021년 5월 31일
- Delete the double loop before the while loop. T_old is already initialized to 0 in the interior.
- You now programmed Jacobi iteration in the while loop, not Gauss-Seidel (also the T_old's on the right-hand side of the equation have to be replaced by T_new.
- a = round(a,3) rounds a to 3 decimal places.
- I don't know how you are told to calculate Errormax. Maybe the relative error is better suited than the absolute error: Errormax = max(abs((T_old-T_new)./T_old,[],'all'))
mehmet salihi
2021년 5월 31일
Torsten
2021년 5월 31일
Why not ? Three decimal places everywhere ...
mehmet salihi
2021년 5월 31일
Torsten
2021년 5월 31일
I think it should be no problem for you to do the remaining cosmetic changes on your own.
mehmet salihi
2021년 5월 31일
Torsten
2021년 5월 31일
Please show your actual code.
What do you mean by "the iteration comes 0 or 1" ?
mehmet salihi
2021년 5월 31일
Torsten
2021년 5월 31일
- You did not give a value to Error before entering the while loop (should of course be larger than Errormax)
- while Error > Errormax instead of while Error < Errormax
- error = sum(sum(abs(T_old-T_new),2)) instead of your loop
mehmet salihi
2021년 5월 31일
mehmet salihi
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
2021년 5월 31일
편집: mehmet salihi
2021년 5월 31일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

