Matrix Operation Returns NaN

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

回答(1 个)

Jan
Jan 2015-7-12
编辑:Jan 2015-7-12
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 个评论

Thank you for the input, I did try using the debugger and could not figure it out. I guess a question I have is, is there anything inherently wrong using
TF(i) = T(i)-2*T(i)+T(i-1); to accomplish this?
This is definitely where the problem occurs. I know the deltax value is not zero, it is checked upstream in the code. I also understand that without all of the code, it is difficult to diagnose/cannot run. It is rather lengthy, and has some information I am not necessarily comfortable distributing.
So long as you’re in the loop, no. But it could be simplified to:
TF(i) = T(i-1) - T(i);

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by