Interp2 Function error using meshgrids
Show older comments
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
Rohan Kotecha
on 31 Dec 2022
How did you end up fixing it? I'm in a similar situation
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)
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!
