Interp2 Function error using meshgrids

Hi! i got a question concerning the interpolation 2 function. I am trying to interpolate some data matrix wich is in respect to a cartesian grid. i have to interpolate the data to a polar grid (not with cart2pol). Yet when i create my new grid and use the function interp2 or griddata i only get 1 column of interpolated matrix and a whole lot of NaN's.
Here is a simplified version of my code:
zpoints=1024;
xpoints=512;
zmax=10e-6
w0=0.6e-6;
rmax=10*w0;
z=linspace(-zmax,zmax,zpoints);
r=linspace(0,rmax,xpoints);
[R,Z]=meshgrid(r,z);
I=peaks(R,Z);
pp=surf(R,Z,I)
rmax=1;
rpoints=512;
thetapoints=1024;
asmall=linspace(0,rmax,rpoints);
Theta=linspace(0,2*pi,thetapoints);
[a,TH]=meshgrid(asmall,Theta);
X=a.*cos(TH);
Y=a.*sin(TH);
I2=interp2(R,Z,I,X,Y);
% % figure
% % pp=surf(X,Y,I);
set(pp,'edgecolor','none');
Can anyone help to see my problem here?
thanks!

1 Comment

How did you end up fixing it? I'm in a similar situation

Sign in to comment.

Answers (1)

The problematic line is,
rmax=1;
This means that X,Y will mostly be located well outside the R,Z grid where they cannot be interpolated. Removing it gives reasonable results:
zpoints=1024;
xpoints=512;
zmax=10e-6;
w0=0.6e-6;
rmax=10*w0;
z=linspace(-zmax,zmax,zpoints);
r=linspace(0,rmax,xpoints);
[R,Z]=meshgrid(r,z);
I=peaks(R,Z);
pp=surf(R,Z,I,'EdgeColor','none'); view (-70,70)
rpoints=512;
thetapoints=1024;
asmall=linspace(0,rmax,rpoints);
Theta=linspace(0,2*pi,thetapoints);
[a,TH]=meshgrid(asmall,Theta(1:end-1));
X=a.*cos(TH);
Y=a.*sin(TH);
I2=interp2(R,Z,I,X,Y);
trisurf(delaunay(X(:),Y(:)),X(:),Y(:),I2(:),'EdgeColor','none'); view (-70,70)
Warning: Duplicate data points have been detected and removed.
Some point indices will not be referenced by the triangulation.

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Release

R2017b

Asked:

on 10 Apr 2019

Edited:

on 31 Dec 2022

Community Treasure Hunt

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

Start Hunting!