Saving values of a variable in while loop

I have a "while" loop for iteration and also I'm calculating error values of it. You can find it below;
while (errorVal>errorMax)
T_old=T;
for j=2:ny-1
for i=2:nx-1
T(j,i)=(1/(2*(1+beta^2)))*(T(j,i+1)+T(j,i-1)+beta^2*(T(j+1,i)+T(j-1,i)));
error(j,i)=abs(T(j,i)-T_old(j,i));
end
end
errorVal=sum(error(:));
n=n+1;
end
Here I want to save "errorVal" variable but I don't know the exact size of iteration so I cannot predefine an array for it. How can I save "errorVal" for each iteration?

답변 (2개)

VBBV
VBBV 2022년 3월 12일

0 개 추천

errorVal(n,:)=sum(error(:));

댓글 수: 3

It didn't work unfortunately. I want to save the value of "errorVal" at each step but this modification returns only one value.
VBBV
VBBV 2022년 3월 12일
편집: VBBV 2022년 3월 12일
errorVal=sum(error(:));
K(n) = errorVal;
n = n+1;
You can try storing in a variable.
Shouldn't it read
T(j,i)=(1/(2*(1+beta^2)))*(T_old(j,i+1)+T_old(j,i-1)+beta^2*(T_old(j+1,i)+T_old(j-1,i)));
Or where does T_old come into play ?

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

Torsten
Torsten 2022년 3월 12일
편집: Torsten 2022년 3월 12일

0 개 추천

errorVal(n) = sum(error(:))/((nx-2)*(ny-2));

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2022년 3월 12일

댓글:

2022년 3월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by