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일

0 개 추천

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

Daniel Platt
Daniel Platt 2015년 7월 12일
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에 대해 자세히 알아보기

질문:

2015년 7월 12일

댓글:

2015년 7월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by