Interpolating data to get values at specific depth

6 views (last 30 days)
Dear experts, please help. I was searching and trying for anentire day and I couldn't figure it out by my own.
I have X, Y, Z, data.
X is longitude, Y is latitude, Z is depth, and data is temperature for example.
X and Y are 2D matrices (224x164). Z and data are 3D matrices (224x164x37).
The depth is unevenly spaced, for exemple, 1.0, 2.3, 5.6, 8.3 and so on, but I need the values of temperature exactly at 5m for example.
What I need is to get the data values at a specif depth all over the domain to plot a colored 2D map.
Please, could someone help me on how is such interpolation?
P.S. I know how to plot, I did it by plotting the surface Z(:,:,1), but as I mentioned, I need other depth layer.
Thank you so much in advance!

Accepted Answer

Torsten
Torsten on 23 Feb 2024
Edited: Torsten on 23 Feb 2024
Make X and Y also 224x164x37 (just by repeating the planar profile 37 times) and use interp3 to interpolate to the depth of your choice.
Then make your contourf or surf plot.
  5 Comments
Torsten
Torsten on 23 Feb 2024
Edited: Torsten on 23 Feb 2024
I guess your data will be well-behaved.
This code only takes the temperature data at the point (X(i,j),Y(i,j)) over the 37 heights and makes a 1d-interpolation to the required height (0.5 m) for all these points. I don't know if the results get better if you include temperatures in neighbouring points, but the interpolation will become more complicated. You will most probably have to use "ScatteredInterpolant".
X = rand(224,164); % random X-coordinates
Y = rand(224,164); % random Y-coordinates
Z = 10*rand(224,164,37); % random heights at points (X,Y)
data = 20 + rand(224,164,37)*20; % random temperatures at points (X,Y,Z)
h = 5; % interpolation level
for i = 1:224
for j = 1:164
[z,idx] = sort(squeeze(Z(i,j,:)));
d = squeeze(data(i,j,idx));
temp_5(i,j) = interp1(z,d,h);
end
end
surf(X,Y,temp_5)
Luís Henrique Bordin
Luís Henrique Bordin on 23 Feb 2024
Now it worked perfectly. Thank you very much, Torsten! Best!

Sign in to comment.

More Answers (1)

William Rose
William Rose on 23 Feb 2024
x=(0:10); y=(0:11)'; z=[1.0, 2.3, 5.6, 8.3]; % vectors x,y,z
[X,Y]=meshgrid(x,y);
%Make data() with desired dimensions, same at each depth
data=repmat(sin(2*pi*y/11)*cos(2*pi*x/10),[1,1,length(z)]);
% Make data different at different depths
for k=1:length(z), data(:,:,k)=data(:,:,k)+z(k)^2/10; end
% check that the array sizes are as desired
fprintf('Size X, Y, Z= %dx%d, %dx%d, %dx%dx%d.\n',size(X),size(Y),size(data))
Size X, Y, Z= 12x11, 12x11, 12x11x4.
% Plot data(x,y,z=5.6), as a test
surf(X,Y,data(:,:,3));
grid on; xlabel('X'); ylabel('Y'), zlabel('Data'); title('Data(:,:,z=5.6)')
data5=zeros(size(X)); % allocate array for data interpolated to z=5
for i=1:length(y), for j=1:length(x)
data5(i,j)=interp1(z,squeeze(data(i,j,:)),5);
end, end
% Plot data(x,y,z=5)
surf(X,Y,data5);
grid on; xlabel('X'); ylabel('Y'), zlabel('Data');
title('Data, Interpolated to z=5')
OK

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!