how can I save my error term in an array within while loop?

조회 수: 1 (최근 30일)
mehmet salihi
mehmet salihi 2021년 6월 6일
댓글: SALAH ALRABEEI 2021년 6월 6일
I am trying to save my Error term within While loop for this code in a list, could you please help me
thank you
clear
close all, clc
%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
y=2:-deltay:0; x=0:deltax:L;
% initialize T_old to the initial guess values
T_old=zeros(jn+1,im+1);
% set boundary conditions
T_old(1,1:im+1)=T1; T_old(jn+1,1:im+1)=T3; T_old(2:jn+1,1)=T2; T_old(2:jn+1,im+1)=T4; T_new = T_old;
Error = 0.011; % could be any number that is greater than Errormax
Errormax=0.01;
iter=0;
while Error > Errormax
iter=iter+1;
for i=2:jn
for j=2:im
T_new(i,j)=(1/(2*(1+Beta2)))* (T_new(i-1,j)+T_new(i+1,j)+Beta2*(T_new(i,j+1)+T_new(i,j-1)) ) ;
end
end
Error = sum(sum(abs(T_old-T_new),2)) ;
T_old = T_new;
end
iter
  댓글 수: 3
mehmet salihi
mehmet salihi 2021년 6월 6일
thats sounds good, could you please tell how to save iterations also
my aim is to draw the iterations versus error history.
when i write the below command, i can see the error for each iterations,
disp([Error iter])
i want to plot them
thank you in advance
SALAH ALRABEEI
SALAH ALRABEEI 2021년 6월 6일
the iteration starts from 1 and keeps increasing +1. So when the while iteration stops. Thus iter will be the maximum number of interations. So to plot the error,
%
plot(1:iter,error)

댓글을 달려면 로그인하십시오.

답변 (1개)

Scott MacKenzie
Scott MacKenzie 2021년 6월 6일
편집: Scott MacKenzie 2021년 6월 6일
I haven't examined your code in any detail. However, if I understand your question correctly, you simply want to save all the error terms calculated in your loop in a list, probably so you can process them in some way after the loop finishes. In this case...
errorSave = [];
while ...
Error = ...
errorSave = [errorSave Error];
end
% all the computed errors are in the vector errorSave
  댓글 수: 1
mehmet salihi
mehmet salihi 2021년 6월 6일
thank you very much, that helped me for saving the iteration parameters also

댓글을 달려면 로그인하십시오.

카테고리

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