# 2-d Interpolation from cartesian to polar coordinates

Discussion in 'MATLAB' started by Kevin Gartz, Aug 27, 2005.

1. ### Kevin GartzGuest

Does anyone have a routine that can take data values for points on
regualrly space cartesian grid and interpolate them onto polar grid ?
I can go from polar to rectangular with griddata but can't figure
out how to go from rectangular to polar.

Kevin Gartz, Aug 27, 2005

2. ### John D'ErricoGuest

Why can't you just use interp2?

HTH,
John D'Errico

John D'Errico, Aug 28, 2005

3. ### Michael KlederGuest

Hello Kevin,

It sounds like, for the problem you have already solved, that you
have a function (or dataset) of the form
z=f(r,theta), and you've computed or obtained the z values for some
set of (r,theta) coordinates. I suspect you then defined a grid of
(x,y) coordinates and sought the z values for them. It sounds like
you used something like the following, where for example your chosen
x and y coordinates go from -5 to +5:

[x,y]=ndgrid(-5:.1:5,-5:.1:5);
zcart=griddata(r.*cos(theta),r.*sin(theta),z,x,y);

Initially, one might suppose that the following might solve the
inverse problem, where for example you already have z=f(x,y) and you
choose r to run from 0 to 5:

[r,th]=ndgrid(0,.1:5,-pi i/30 i);
zpolar=griddata(sqrt(x.^2+y.^2),atan2(y,x),z,r,th);

However, a problems occurs. First, the zpolar values at coordinates
(r,-pi) and (r,+pi) may contain NaN values because there is no data
outside those angular boundaries from which to interpolate. (atan2
returns angles within
[-pi,pi].)

My suggestion is to create legitimate but redundant data "outside"
the angular range [-pi,pi] as a buffer zone for your interpolation.
For example, again starting with z=f(x,y) data where x and y both run
from -5 to 5:

[r,theta]=ndgrid(0:.1:5,-pi i/30 i);
rdata=sqrt(x.^2+y.^2);
thdata=atan2(y,x);
rdata=[rdata );rdata );rdata )];
thdata=[thdata )-2*pi;thdata );thdata )+2*pi];
zpolar=griddata(rdata,thdata),z,r,theta);

Now, you should have the (r,theta) grid that you seek, together with
corresponding zpolar values.

I hope this helps. I don't have Matlab running on the machine I am
using at this moment, so I wasn't able to run the code to check for
mistakes. Hopefully bugs didn't creep in.

Cheers,
Mike
____________________

Does anyone have a routine that can take data values
for points on regularly spaced cartesian grid and
interpolate them onto polar grid? I can go from polar to
rectangular with griddata but can't figure out how to go
from rectangular to polar.

Michael Kleder, Aug 28, 2005