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
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
2023-1-11
Note that when you request linear, which is the default, then extrapolation is not automatically turned on.
回答(1 个)
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
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 Exchange 中查找有关 Descriptive Statistics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!