Search code examples
matlabinterpolationpolar-coordinates

How to interpolate using in polar coordinate


I have polar coordinates, radius 0.05 <= r <= 1 and 0 ≤ θ ≤ 2π. The radius r is 50 values between 0.05 to 1, and polar angle θ is 24 values between 0 to 2π.

How do I interpolate r = 0.075 and theta = pi/8?


Solution

  • Based on comments you have the following information

    %the test point
    ri=0.53224;
    ti = pi/8;
    
    %formula fo generation of Z
    g=9.81
    z0=@(r)0.01*(g^2)*((2*pi)^-4)*(r.^-5).*exp(-1.25*(r/0.3).^-4);
    D=@(t)(2/pi)*cos(t).^2; 
    z2=@(r,t)z0(r).*D(t) ;
    
    %range of vlaues of r and theta
    r=[0.05,0.071175,0.10132,0.14422,0.2053, 0.29225,0.41602,0.5922,0.84299,1.2]; 
    t=[0,0.62832,1.2566,1.885, 2.5133,3.1416,3.7699,4.3982,5.0265,5.6549,6.2832];
    

    and you want interplation of the test point.

    When you sample some data to use them for interpolation you should consider how to sample data according to your requirements. So when you are sampling a regular grid of polar coordinates ,those coordinates when converted to rectangular will form a circular shape that most of the points are concentrated in the center of the cricle and when we move from the center to outer regions distance between the points increased.

    %regular grid generated for r and t
    [THETA R] = meshgrid(t ,r);
    % Z for polar grid
    Z=z2(R,THETA);
    %convert coordinate from polar to cartesian(rectangular):
    [X, Y] = pol2cart (THETA, R);
    %plot points
    plot(X, Y, 'k.');
    axis equal
    

    enter image description here

    So when you use those point for interpolation the accuracy of the interpolation is greater in the center and lower in the outer regions where the distance between points increased.
    In the other word with this sampling method you place more importance on the center region related to outer ones. To increase accuracy density of grid points (r and theta) should be increased so if length of r and theta is 11 you can create r and theta with size 20 to increase accuracy.
    In the other hand if you create a regular grid in rectangular coordinates an equal importance is given to each region . So accuracy of the interpolation will be the same in all regions.
    For it first you create a regular grid in the polar coordinates then convert the grid to rectangular coordinates so you can calculate the extents (min max) of the sampling points in the rectangular coordinates. Based on this extents you can create a regular grid in the rectangular coordinates Regular grid of rectangular coordinates then converted to polar coordinated to get z for grid points using z2 formula.

    %get the extent of points
    extentX = [min(X(:)) max(X(:))];
    extentY = [min(Y(:)) max(Y(:))];
    
    %sample 100 points(or more or less) inside a region specified be the extents
    X_samples = linspace(extentX(1),extentX(2),100);
    Y_samples = linspace(extentY(1),extentY(2),100);
    %create regular grid in rectangular coordinates
    [XX YY] = meshgrid(X_samples, Y_samples);
    [TT RR] = cart2pol(XX,YY);
    Z_rect = z2(RR,TT);
    

    For interpolation of a test point say [ri ti] first it converted to rectangular then using XX ,YY value of z is interpolated

    [xi yi] = pol2cart (ti, ri);
    z=interp2(XX,YY,Z_rect,xi,yi);
    

    If you have no choice to change how you sample the data and only have a grid of polar points as discussed with @RodyOldenhuis you can do the following:

    1. Interpolate polar coordinates with interp2 (interpolation for gridded data) this approach is straightforward but has the shortcoming that r and theta are not of the same scale and this may affect the accuracy of the interpolation.

      z = interp2(THETA, R, Z, ti, ri)

    2. convert polar coordinates to rectangular and then apply an interpolation method that is for scattered data. this approach requires more computations but result of it is more reliable. MATLAB has griddata function that given scattered points first generates a triangulation of points and then creates a regular grid on top of the triangles and interpolates values of grid points. So if you want to interpolate value of point [ri ti] you should then apply a second interpolation to get value of the point from the interpolated grid.

    With the help of some information from spatialanalysisonline and Wikipedia linear interpolation based on triangulation calculated this way (tested in Octave. In newer versions of MATLAB use of triangulation and pointLocation recommended instead of delaunay and tsearch ):

    ri=0.53224;
    ti = pi/8;
    [THETA R] = meshgrid(t ,r);
    [X, Y] = pol2cart (THETA, R);
    [xi yi] = pol2cart (ti, ri);
    %generate triangulation
    tri = delaunay (X, Y);
    %find the triangle that contains the test point
    idx = tsearch (X, Y, tri, xi, yi);
    pts= tri(idx,:);
    %create a matrix that repesents equation of a plane (triangle) given its 3 points
    m=[X(pts);Y(pts);Z(pts);ones(1,3)].';
    %calculate z based on det(m)=0;
    z= (-xi*det(m(:,2:end)) + yi*det([m(:,1) m(:,3:end)]) + det(m(:,1:end-1)))/det([m(:,1:2) m(:,end)]);
    

    More refinement: Since it is known that the search point is surrounded by 4 points we can use only those point for triangulation. these points form a trapezoid. Each diagonal of trapezoid forms two triangles so using vertices of the trapezoid we can form 4 triangles, also a point inside a trapezoid can lie in at least 2 triangles. the previous method based on triangulation only uses information from one triangle but here z of the test point can be interpolated two times from data of two triangles and the calculated z values can be averaged to get a better approximation.

    %find 4 points surrounding the test point
    ft= find(t<=ti,1,'last');
    fr= find(cos(abs(diff(t(ft+(0:1))))/2) .* r < ri,1,'last');
    [T4 R4] = meshgrid(t(ft+(0:1)), r(fr+(0:1)));
    [X4, Y4] = pol2cart (T4, R4);
    Z4 = Z(fr+(0:1),ft+(0:1));
    %form 4 triangles
    tri2= nchoosek(1:4,3);
    %empty vector of z values that will be interpolated from 4 triangles
    zv = NaN(4,1);
    for h = 1:4
        pts = tri2(h,:);
        % test if the point lies in the triangle
        if ~isnan(tsearch(X4(:),Y4(:),pts,xi,yi))
            m=[X4(pts) ;Y4(pts) ;Z4(pts); [1 1 1]].';
            zv(h)= (-xi*det(m(:,2:end)) + yi*det([m(:,1) m(:,3:end)]) + det(m(:,1:end-1)))/det([m(:,1:2) m(:,end)]);
        end
    end
    z= mean(zv(~isnan(zv)))
    

    Result:

    True z: 
    (0.0069246)
    Linear Interpolation of (Gridded) Polar Coordinates : 
    (0.0085741)
    Linear Interpolation with Triangulation of Rectangular Coordinates: 
    (0.0073774 or 0.0060992)  based on triangulation
    Linear Interpolation with Triangulation of Rectangular Coordinates(average):
    (0.0067383)
    

    Conclusion:

    Result of interpolation related to structure of original data and the sampling method. If the sampling method matches pattern of original data result of interpolation is more accurate, so in cases that grid points of polar coordinates follow pattern of data result of interpolation of regular polar coordinate can be more reliable. But if regular polar coordinates do not match the structure of data or structure of data is such as an irregular terrain, method of interpolation based on triangulation can better represent the data.