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

2 次查看(过去 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.

回答(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.

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by