How to use Matlab to find a pressure in a temperature and density spreadsheet using the nearest density value.
12 views (last 30 days)
Show older comments
I have a pressure and temperature spreadsheet where pressure is the first column from 14.7 - 2414.7 and temperature is the first row from 65 - 80 and the associated density fills in the rest of the matrix. I am having issues using Matlab to find the pressure associated with an inputed Temperature and Density and I think its due to not having the exact density value. I've tried using an interpolate function and a find function with no luck. If anyone could help solve this issue that would be much appreciated! I havent used Matlab in the last 3 years and am struggling getting back into it. Thanks!
Interpolate code:
newDensity = ((den * V) + ((mdot/(60*60))*ts)/V); %new mass&density in bottle after time step%
%read pressure, Temperature, and density Excel Spread Sheet%
Table = readtable('GBUgasTPDdata.xlsx');
temperature = Table{1,2:end};
pressure = Table{2:end,1};
newPressure = interp1(temperature,pressure,newDensity);
Find Code:
newDensity = ((den * V) + ((mdot/(60*60))*ts)/V); %new mass&density in bottle after time step%
%read pressure, Temperature, and density Excel Spread Sheet%
Table = readtable('GBUgasTPDdata.xlsx');
temperature = Table{1,2:end};
pressure = Table{2:end,1};
xvar = find(temperature==T);
yvar = find(pressure==Pb);
Int = Table{yvar+1, xvar+1}
2 Comments
dpb
on 20 Sep 2024
The problem is you need reverse lookup of the P,T intersection of the new density -- presuming that calculation is the reference real new condition.
Is the table by any chance the steam tables data? If so, I'd suggest using the <File Exchange XSteam> submission that has the ASME standard steam tables correlations builtin and can return any desired property given the others.
If it is some other fluid, code for many common ones is also available.
Answers (3)
Star Strider
on 20 Sep 2024
It would help to have your data.
Use the scatteredInterpolant function to find (and if desired, plot) any value you want within the ranges of your data.
Try this —
t = linspace(10,30, 25);
p = linspace(1, 20, 20);
[T,P] = ndgrid(t,p);
Z = exp(-((T-20).^2+(P-10).^2)*0.01);
TPfcn = scatteredInterpolant(T(:),P(:),Z(:));
TP_15_10 = TPfcn(15,10) % Find ‘Z’ Value For T = 15, P = 10
figure
surf(T, P, Z)
hold on
plot3(15, 10, TP_15_10, 'pc', 'MarkerFaceColor','c', 'MarkerSize',25)
hold off
grid on
xlabel('T')
ylabel('P')
colormap(turbo)
.
4 Comments
cr
on 20 Sep 2024
Edited: cr
on 20 Sep 2024
This should be an interpolation in 2 dimensions and should work in a straight forward way unless I missed a key detail in your description.
InterpolatedDensity = interp2(temperature, pressure, densityMatrix,NewTemp,NewPres)
temperature and pressure are the row and column from excel sheet. densityMatrix is the full matrix of density values. NewTemp and NewPres are the temperature and pressure where you want to query the density. There are interp options like nearest, linear, etc that can be used.
5 Comments
dpb
on 20 Sep 2024
It would be most interesting to see the actual data -- how nonlinear is it? It might be feasible to simply replace the whole table with a response surface from which you could calculate directly...
Torsten
on 20 Sep 2024
Edited: Torsten
on 20 Sep 2024
%Generate artificial data
t = linspace(10,30, 25);
p = linspace(1, 20, 20);
[T,P] = ndgrid(t,p);
d = exp(-((T-20).^2+(P-10).^2)*0.01);
%Inverse interpolation
tnew = 22.5;
dnew = 0.35;
dcut = interp2(t,p,d.',tnew,p);
%fun = @(x)(interp1(p,dcut,x,'pchip')-dnew)^2;
%pnew = fsolve(fun,mean(p))
[~,idx] = min(abs(dcut-dnew));
pnew = p(idx);
%Compare result
interp2(t,p,d.',tnew,pnew)
dnew
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!