Error in interpolating: Actual data does not match with interpolated data

3 views (last 30 days)
Hi,
I am trying to interpolate my gridded data of dimension (longitude x latitude x altitude) at specific latitude and altitude which are not included in the dataset. To check my code and verify if my interpolated data is correct, I tried to interpolate at the point (Lat = 15 Degree & Alt = 100 km) which is included in the original data. By surpise, the plots for interpolated data and original data didnt match. I am not sure what I am doing wrong so requesting if you could help me out.
Below is my matlab code (attached) and link for my data:
%% read data
file1='data_nc\data_10x10\data.nc'; %76x94x54
Temp1_4d = ncread(file1,'Temperature');
Temp1_4d =Temp1_4d(3:38,3:20,:);
Lat1_4d = ncread(file1,'Latitude')*(180/pi);
Lat1_4d = Lat1_4d(3:38,3:20,:);
Long1_4d = ncread(file1, 'Longitude')*(180/pi);
Long1_4d =Long1_4d(3:38,3:20,:);
Alt1_4d = ncread(file1,'Altitude');
Alt1_4d = Alt1_4d(3:38,3:20,:);
Temp1_4d = double(Temp1_4d);% converts single data to double
Temp1a_4d = Temp1_4d(19:36,:,:);Temp1b_4d =Temp1_4d(1:18,:,:);Temp1c_4d = [Temp1a_4d;Temp1b_4d];
Long1_4d =double(Long1_4d);Long1_4d = wrapTo180(Long1_4d);
Long1a_4d = Long1_4d(19:36,:,:);Long1b_4d = Long1_4d(1:18,:,:);Long1c_4d = [Long1a_4d;Long1b_4d];
Lat1_4d = double(Lat1_4d);
Lat1a_4d = Lat1_4d(19:36,:,:);Lat1b_4d = Lat1_4d(1:18,:,:);Lat1c_4d = [Lat1a_4d;Lat1b_4d];
Alt1_4d = double(Alt1_4d);
Alt1a_4d = Alt1_4d(19:36,:,:);Alt1b_4d = Alt1_4d(1:18,:,:);Alt1c_4d = [Alt1a_4d;Alt1b_4d];
Long1 = Long1c_4d(:,1,1); %reads vector of Long
Lat1 = Lat1c_4d(1,:,1); %reads vector of Long
Alt1 = Alt1c_4d(1,1,:);%reads vector of Alt
Alt1 = permute(Alt1,[1 3 2]);
%% interpolate
LUT1=griddedInterpolant({Long1,Lat1,Alt1}, Temp1_4d); %interpolation object 2
TempvLong1=LUT1({Long1,15,100000}); %slices
TempvLong1b=LUT1({Long1,15,100000}); %slices
%%Plot
tiledlayout(1,1)
nexttile
hold on;
plot(Long1(:),TempvLong1(:),'-hexagram','Color','k','MarkerSize',2,'MarkerEdgeColor','k','MarkerFaceColor','k');
plot(Long1c_4d(:,11,3),Temp1c_4d(:,11,3),'-hexagram','Color','b','MarkerSize',2,'MarkerEdgeColor','b','MarkerFaceColor','b');
xlabel 'Longitude';
ylabel 'Temperature'
title 'Lat = 15^o & Alt = 100 km';
%legend ('72x54 cells (5 long x 3.33 lat)','72x90 cells (5 long x 2 lat)','72x180 cells (5 long x 1 lat)','120x90 cells (3 long x 2 lat)');
%legend('location','north','box','on','FontSize',7);
legend('location','north','box','on','FontSize',7);
legend('interpolated data', 'origina data');
xlim([-180 180]);
ax = gca;
set(ax,'Box','on');
ax.XTick = -180:60:180;
hold off;
  7 Comments
Mark
Mark on 29 Nov 2022
Edited: Mark on 29 Nov 2022
Hi @Matt J, did you see my LUT1 data file in .mat format. If not then can you help me get to save LUT in a right format please.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 29 Nov 2022
Edited: Matt J on 29 Nov 2022
load inputData
i=11; j=3; %query points (Lat and Alt)
%% interpolate
LUT1=griddedInterpolant(Long1c_4d,Lat1c_4d,Alt1c_4d, Temp1c_4d); %interpolation object
[Long1,Lat1,Alt1]=deal(LUT1.GridVectors{:}); %1D versions of the coordinates
TempvLong1=LUT1({Long1,Lat1(i),Alt1(j)}); %slices
%%Plot
tiledlayout(1,1)
nexttile
hold on;
plot(Long1(:),TempvLong1(:),'-hexagram','Color','k','MarkerSize',2,'MarkerEdgeColor','k','MarkerFaceColor','k');
plot(Long1c_4d(:,i,j),Temp1c_4d(:,i,j),'o','Color','r','MarkerSize',8,'MarkerEdgeColor','r');
xlabel 'Longitude';
ylabel 'Temperature'
title(sprintf('Lat = %.2f^o & Alt = %.2f km',Lat1(i),Alt1(j)/1000));
legend('location','northeast','box','on','FontSize',12);
legend('interpolated data', 'original data');
xlim([-180 180]);
ax = gca;
set(ax,'Box','on');
ax.XTick = -180:60:180;
hold off;

More Answers (0)

Categories

Find more on Interpolation 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!