Clear Filters
Clear Filters

Transform Cylindrical polar coordinates to Cartesan but equally distributed

5 views (last 30 days)
I have polar coordinates in R Phi and Z.
R and Phi were constructed by
R_lin=linspace(0,1,100)
Phi_lin=linspace(0,2*pi,100)
[r_mesh,phi_mesh]=meshgrid(R_lin,Phi_lin)
, so for every value to the next there is a equally difference in Radius an Phi. (The bigger your R gets, the less values you have on your circle.)
R,Phi,Z are all Matrixes 100x100. (Z was created by some loops, where the values were entered du to the position of R and Phi).
So if we combind the three values of R,Phi,Z on the same index we get the position of a point.
If I now use pol2cat i get three Matrixes all 100 by 100 for x,y,z. Just for plotting this works fine, but I now want to work further with that data in cartesian coordinates.
How can I change my Matrixes so that X goes from -1 to 1 row-wise and y from -1 to 1 colum-wise and the z values get sorted corresponded to the new indexes in x,y. Even better would be if i could apply some kind of interpolation to make x and y even distributed (same difference between points).
So i can work with the data as if I would have created it with a meshgrid.

Accepted Answer

Namnendra
Namnendra on 16 Apr 2024
Hi Oliver,
To convert your polar coordinate matrices (`R`, `Phi`, and `Z`) into Cartesian coordinate matrices (`X`, `Y`, and `Z`) and then reorganize them so that `X` and `Y` are evenly distributed (as they would be if created directly with `meshgrid`), you can follow these steps. This process also involves interpolating the `Z` values to correspond to the new `X` and `Y` grid.
Here's how you can approach it:
Step 1: Convert Polar to Cartesian Coordinates
First, you need to convert your polar coordinates to Cartesian coordinates. The `pol2cart` function does this conversion. Note that you mentioned `pol2cat`, which seems to be a typo.
% Assuming R, Phi, and Z are your polar coordinates
[X_polar, Y_polar] = pol2cart(phi_mesh, r_mesh);
This gives you `X_polar` and `Y_polar` matrices corresponding to your original polar grid.
Step 2: Create a New Evenly Distributed Cartesian Grid
Next, create a new Cartesian grid where `X` and `Y` are evenly distributed. This grid will be used for interpolation.
x_lin = linspace(-1, 1, 100); % Adjust the number of points as needed
y_lin = linspace(-1, 1, 100); % Adjust the number of points as needed
[X_cart, Y_cart] = meshgrid(x_lin, y_lin);
Step 3: Interpolate Z Values onto the New Grid
Now, interpolate the `Z` values from the polar-derived Cartesian coordinates (`X_polar`, `Y_polar`) onto the new, evenly distributed Cartesian grid (`X_cart`, `Y_cart`). The `griddata` function is suitable for this task.
Z_cart = griddata(X_polar, Y_polar, Z, X_cart, Y_cart, 'linear');
Here, `Z_cart` contains the `Z` values interpolated onto your new Cartesian grid. You can use different methods with `griddata` (`'linear'`, `'cubic'`, `'nearest'`, etc.) depending on your data and requirements.
Optional: Handling NaN Values
Interpolation might result in `NaN` values at the edges or outside the original data range. Depending on your application, you might need to handle these, for example, by setting them to a default value or using extrapolation.
Step 4: Work Further with the Data
Now, you have `X_cart`, `Y_cart`, and `Z_cart`, which are matrices of Cartesian coordinates with `X` and `Y` evenly distributed, and `Z` values interpolated accordingly. You can proceed with further analysis or visualization using these matrices.
Visualization
For verification, you might want to visualize the original and interpolated data to ensure the transformation and interpolation went as expected.
figure;
surf(X_polar, Y_polar, Z); % Original data in polar-derived Cartesian coordinates
title('Original Data');
figure;
surf(X_cart, Y_cart, Z_cart); % Interpolated data on the new Cartesian grid
title('Interpolated Data');
This approach allows you to convert and interpolate your data from a polar coordinate system to a Cartesian system, with `X` and `Y` evenly distributed, facilitating further analysis or visualization in Cartesian space.
I hope the above steps help resolve the issue.
Thank you.

More Answers (0)

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!