interp2 using iterated values in a loop - NaN values returned for subsequent iterations after defined initial condition

조회 수: 1 (최근 30일)
For my script I am tyrying to get interp2 to read values from previous lines completed in an iterative loop. Please see the code:
for i=1:n
m(i+1) = m(i) + dt*mdot;
T(i+1) = T(i) + dt*mdot*(T(i)*V/mmol+he-h(i))/(m(i)*cp(i)-m(i)*V/mmol);
P(i+1) = P(i) + dt*(mmol*m(i)*(T(i+1)-T(i))/dt+T(i)*mmol*m(i));
h(i+1) = interp2(Ph,Th,Vhn,P(i+1)/10^6,T(i+1),"linear"); %h(i+1) values calculated using (P(i+1) and T(i+1)
cp(i+1) = interp2(Pcp,Tcp,Vcpn,P(i+1)/10^6,T(i+1),"linear"); %see note above for cp(i+1)
end
The code, with the rest of the scrip runs without error, but the loop is not able to calculate values for h(i+1) and cp(i+1). Instead it returns NaN values for subsequent values of h and cp when opening the array in the workspace. I understand that NaN values could indicate that the values for Xp or Yq are outside of the limits of X or Y respectively, however this is not the case. I think the issue lies in the use of interp2 in an iterative loop. I am hoping that this can be solved so h(i+1) and cp(i+1) can be solved for at each iteration loop from the first to the nth run. Please can anyone offer a way to resolve this issue.
  댓글 수: 2
Torsten
Torsten 2023년 1월 11일
There cannot be another reason for h(i+1) and/or cp(i+1) to be NaN values as
a) the inputs contain NaN/Inf values or
b) Xq and/or Yq are out of range.
Walter Roberson
Walter Roberson 2023년 1월 11일
Note that when you request linear, which is the default, then extrapolation is not automatically turned on.

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

답변 (1개)

Aditya Srikar
Aditya Srikar 2023년 3월 30일
편집: Aditya Srikar 2023년 3월 30일
Hi George
The function interp2 returns interpolated values of a function at specific query points using linear interpolation. It might return NaN values if the input contains NaN or Inf values.
Consider these statements
h(i+1) = interp2(Ph,Th,Vhn,P(i+1)/10^6,T(i+1),"linear");
cp(i+1) = interp2(Pcp,Tcp,Vcpn,P(i+1)/10^6,T(i+1),"linear");
Suppose if any of the values P(i+1) or T(i+1) become NaN or INF in current iteration, then the output returned will be NaN and it will effect all the subsequent iterations.
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 3월 30일
Much more common of a problem, though, is requesting to interpolate at a point that is out of the defined range when you have not turned extrapolation on.

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by