Matrix Operation Returns NaN
Mostra commenti meno recenti
Hello, I am working on a project where I analyze temperature change with a few different methods. One of them calls for a set of equations to be done repetitively, using a matrix. My code that is relavant is below:
while flag == 0
time=1;
for i=2:1:n
if i==n
%This can be ignored for now, the else statement below is the issue.
%Tnew(i) = T(i) + (alpha).*(0.01).*(-1/deltax).*(((T(i)-T(i-1)./(deltax))-b+ (h/k.*deltax).*(T(i)-TAir)));
else
TF(i) = T(i)-2*T(i)+T(i-1);
Tnew(i) = T(i) + ((alpha).*(0.01)).*((TF(i)/deltax)- b.*(T(i)-TAir));
end
error(i) = abs((Tnew(i)-T(i))/T(i))*100;
end
for i=1:1:n
T(i) = Tnew(i);
end
time = time + 1;
if max(error)<0.01
flag=1;
end
if time>100
disp('Error');
break
end
end
The code in the if else statement is giving me trouble. I pulled the TF part out, to analyze it. For some reason, it returns NaN for every member of the matrix. I may be missing something small, but after looking through it for hours, I am unsure where the error lies.
Thank you for any help
Risposte (1)
Use the debugger to finde the problem. Set a breakpoint at the line, which defines TF and inspect the parts in the command window. Find out, which term creates the NaNs. Perhaps deltax is zero?
We cannot do this for you, because we do not have your data and the code does not run by copy&paste.
Notes:
- Do not shadow the important function error by a variable.
- This code:
for i=1:1:n
T(i) = Tnew(i);
end
should be beautified:
T(1:n) = Tnew(1:n);
or if applicable:
T = Tnew;
2 Commenti
Daniel Platt
il 12 Lug 2015
Star Strider
il 12 Lug 2015
So long as you’re in the loop, no. But it could be simplified to:
TF(i) = T(i-1) - T(i);
Categorie
Scopri di più su Operating on Diagonal Matrices in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!