Saving values of a variable in while loop
3 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
VBBV
on 12 Mar 2022
errorVal(n,:)=sum(error(:));
3 Comments
Torsten
on 12 Mar 2022
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 ?
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!