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

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 commentaires

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.
Note that when you request linear, which is the default, then extrapolation is not automatically turned on.

Connectez-vous pour commenter.

Réponses (1)

Aditya Srikar
Aditya Srikar le 30 Mar 2023
Modifié(e) : Aditya Srikar le 30 Mar 2023
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 commentaire

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.

Connectez-vous pour commenter.

Catégories

Produits

Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by