フィルターのクリア

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.
  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 Center および File ExchangeMultidimensional Arrays についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by