Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Extrapolation of non-rectangular shaped surface to rectangular shape

Subject: Extrapolation of non-rectangular shaped surface to rectangular shape

From: Stan

Date: 25 Nov, 2013 02:52:09

Message: 1 of 3

Hello,
I am trying to extrapolate a 2D surface. The full code, including raw data, that I am working with is at the end of this message.

Extrapolation:
I used griddedinterpolant because MATLAB 2013 documentation indicates that it performs the extrapolation automatically (only for 2013):
http://www.mathworks.com/help/matlab/ref/griddedinterpolantclass.html

I am hoping to perform the extrapolation over the range of co-ordinates indicated by [extrpl_range_x, extrpl_range_y]. For generating the rectangular grid, I have used meshgrid or ndgrid:
Meshgrid: http://www.mathworks.com/help/matlab/ref/meshgrid.html
Ndgrid: http://www.mathworks.com/help/matlab/ref/ndgrid.html

You can copy and paste all the code into a script window and run the code to see for yourself what the problem is. MATLAB 2013 is required for this.

PROBLEM:
a. I am not getting extrapolation in the corners. The original data was missing the corners, but I was hoping that extrapolated mesh would give a rectangular shape to fill in these corners. This does not appear to be happening
b. It seems like ndgrid is producing a rotated map.

Questions:
For a: Is there a problem with the rectangular co-ordinates - ndgrid?
For b: Is there a mistake with the extrapolation commands?
-----------------------X-----------------------
CODE:
num_linrly_spcd_pts=33; %linearly spaced points for linspace command
methd='natural'; %method of griddata interpolation; options: v4, cubic, natural, linear
methd2='nearest'; %method of griddedInterpolant interpolation; options: spline, cubic, nearest, linear
extrpl_range_x=-8:50; %x-range over which extrapolation is to be performed
extrpl_range_y=10:50; %y-range over which extrapolation is to be performed

% Loading data:
X(:,1) = [7 17 29 41 0 45 7 17 29 41 0 45 7 17 29 41]; %X co-ordinates of map
X(:,2) = [17 17 17 17 23 23 28 28 28 28 33 33 38 38 38 38]; %Y co-ordinates of map
V = [1659 1160 2147 1392 1568 2444 1264 1505 1819 1790 1347 708 1404 1680 845]; %Values of 2D function

xlin = linspace(min(X(:,1)),max(X(:,1)),num_linrly_spcd_pts); %generate equally spaced x co-ordinate vector with 33 equally spaced points
ylin = linspace(min(X(:,2)),max(X(:,2)),num_linrly_spcd_pts); %generate equally spaced y co-ordinate vector with 33 equally spaced points
[Xlin,Ylin] = meshgrid(xlin,ylin); %Produce a full grid of x and y co-ordinates, which are the 33 generated equally spaced points

% Performing interpolation:
ZZ = griddata(X(:,1),X(:,2),V,Xlin,Ylin,methd); %perform griddata interpolation using specified method

% Plot interpolated mesh:
fig2=figure('Color',[1 1 1]);
efg=mesh(Xlin,Ylin,ZZ); %plot the interpolated data
axis tight; hold on
axis([10 50 10 40 min(V) 30000]);
hold on
az = 0; el = 90;view(az, el);
colorbar('location', 'EastOutside')

% % EXTRAPOLATION SECTION:
% % NDGRID APPROACH
% [Xlin3,Ylin3] = ndgrid(xlin,ylin);
% F = griddedInterpolant(Xlin3,Ylin3,ZZ,methd2);
% [x_qq,y_qq] = ndgrid(extrpl_range_x,extrpl_range_y);
% Vqq2 = F(x_qq,y_qq);

% % MESHGRID APPROACH
Xlin2=Xlin';Ylin2=Ylin';ZZ=ZZ';
F = griddedInterpolant(Xlin2,Ylin2,ZZ,methd2); %spline, cubic, linear, nearest
[x_q,y_q] = meshgrid(extrpl_range_x,extrpl_range_y);
x_q = x_q'; y_q = y_q';
Vqq1 = F(x_q,y_q);
 
% % Plotting extrapolated mesh:
fig4=figure('Color',[1 1 1]);
efg=mesh(x_q,y_q,Vqq1); %plot the extrapolated mesh - MESHGRID APPROACH
%efg=mesh(x_qq,y_qq,Vqq2); %plot the extrapolated mesh - NDGRID APPROACH
axis tight; hold on
axis([10 50 10 40 min(V) 40000]);
hold on
az = 0; el = 90;view(az, el);
colorbar('location', 'EastOutside')

Subject: Extrapolation of non-rectangular shaped surface to rectangular shape

From: John D'Errico

Date: 25 Nov, 2013 04:41:05

Message: 2 of 3

"Stan" wrote in message <l6ue0p$roh$1@newscl01ah.mathworks.com>...
> Hello,
> I am trying to extrapolate a 2D surface. The full code, including raw data, that I am working with is at the end of this message.
>
> Extrapolation:
> I used griddedinterpolant because MATLAB 2013 documentation indicates that it performs the extrapolation automatically (only for 2013):
> http://www.mathworks.com/help/matlab/ref/griddedinterpolantclass.html
>
> I am hoping to perform the extrapolation over the range of co-ordinates indicated by [extrpl_range_x, extrpl_range_y]. For generating the rectangular grid, I have used meshgrid or ndgrid:

The tool you are using uses a triangulation. It will not
extrapolate past the convex hull, so it fills to do at all
what you need.

Use gridfit. Look on the file exchange.

John

Subject: Extrapolation of non-rectangular shaped surface to rectangular shape

From: Stan

Date: 7 Dec, 2013 03:33:17

Message: 3 of 3

Hi,

This is the correct code that I was looking for. Thank you for the suggestion.

However, I noticed that it performs smoothing. Is there a way to avoid over smoothing?

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us