简体   繁体   中英

How can I convert from cylindrical to cartesian coordinates in 3D for plotting in Matlab?

I am trying to define a function in 3D cylindrical coorindates in Matlab, and then to convert it to 3D cartesian for plotting purposes.

在此处输入图像描述

For example, if my function depends only on the radial coordinate r (let's say linearly for simplicity), I can plot a 3D isosurface at the value f = 70 like the following:

x = linspace(-10,10,100);
y = linspace(-10,10,100);
z = linspace(-2,2,100);

[X,Y,Z] = meshgrid(x,y,z);
R = sqrt( X.^2 + Y.^2 ); % Calculate the radius for each [x,y] coordinate pair

f = 100*R; % Calculate the function f, dependent only on the radius

isosurface(X,Y,Z,f,70);

在此处输入图像描述

However, since the function depends only on r, I should be able to instead define a vector for the r coordinate only, and calculate f based on that instead:

r = 0:0.1:1 ;   % define radial coordinate
f = 100*r ;     % make the function linearly dependent on radial coorindate for simplicity

My aim is now to plot an isosurface based only on knowing r and f. I would like to specify a vector of z and theta coordinates, maybe like this

z = linspace(-2,2,100);
theta = linspace(0,2*pi,50);

and use these together with r to generate [x,y,z] coordinates. In addition, f now is a 1D array instead of a 3D array like the first example, so somehow f needs to be reshaped?

The reason for this is because I am trying to do manipulations on f inside a loop many times, and with the first case this results in many operations over a 3D matrix, which is too slow. I would like to instead perform operations on the 1D vector, and generate 3D version only for plotting.

I hope my question is clear. I don't think the pol2cart function does what I want, but I could be wrong.

It is a rather dificult task to represent 4D data. You mention you want to visualize 4D data with the isosurface function. Another alternative is to represent the 4th variable using a color scale, with the slice function.

The problem with these functions is that they work for the cartesian coordinates [X,Y,Z]. What you have to do is to interpolate your data to your domain in cartesian coordinates, before using the functions to represent these 4D data, as in here or here .

Essentially, what you end up with is:

% generate mesh in cylindrical coordinates
theta = linspace(0,2*pi,50);
r = 0:0.1:1;
z = linspace(-2,2,100);

[Theta,R,Z] = meshgrid(theta,r,z);

% Evaluate function in cylindrical coordinates
f = 100*r; % f is function only of radial coordinate
f = f'; % make sure f is column vector
f = repmat(f,1,length(theta),length(z)); % f must be 3D matrix to plot surface

% transform to cartesian coordinates
[X,Y,Z] = pol2cart(Theta,R,Z)

[xg, yg, zg] = meshgrid(linspace(-1,1,20),linspace(-1,1,20),linspace(-2.1,2.1,20));

Px = squeeze(X);
Py = squeeze(Y);
Pz = squeeze(Z);
v = squeeze(f);
F = scatteredInterpolant([Px(:) Py(:) Pz(:)],v(:),'linear','nearest');

f_interp = F(xg,yg,zg);

And now you can use whatever function you want to visualize the 4D data:

isosurface(xg,yg,zg,f_interp,70);

You can interpolate your data with either the scatteredinterpolant function or the griddata function, whatever fits best to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM