如何将 3D 中的圆柱坐标转换为笛卡尔坐标以便在 Matlab 中绘图?

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

我正在尝试在 Matlab 中定义 3D cylindrical coorindates 中的函数,然后将其转换为 3D 笛卡尔坐标以用于绘图目的。

例如,如果我的函数仅依赖于径向坐标 r(为简单起见,我们假设为线性),我可以在值 f = 70 处绘制一个 3D 等值面,如下所示:

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);

但是,由于该函数只依赖于 r,我应该能够只为 r 坐标定义一个向量,并以此为基础计算 f:

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

我现在的目标是仅根据已知的 r 和 f 绘制等值面。我想指定一个 z 和 theta 坐标的向量,可能像这样

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

并将它们与 r 一起使用以生成 [x,y,z] 坐标。此外,f 现在是一维数组,而不是像第一个示例那样的 3 维数组,所以 f 需要以某种方式重新整形?

这是因为我试图多次在循环内对 f 进行操作,而在第一种情况下,这会导致对 3D 矩阵进行多次操作,这太慢了。我想改为对 1D 向量执行操作,并生成仅用于绘图的 3D 版本。

我希望我的问题很清楚。我不认为 pol2cart 函数可以满足我的要求,但我可能是错的。

表示 4D 数据是一项相当困难的任务。您提到要使用 isosurface function. Another alternative is to represent the 4th variable using a color scale, with the slice 函数可视化 4D 数据。

这些函数的问题在于它们适用于笛卡尔坐标 [X,Y,Z]。 在使用函数表示这些 4D 数据之前,您需要做的是将数据插入笛卡尔坐标系中的域,如 here or here.

基本上,你最终得到的是:

% 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);

现在您可以使用任何您想可视化 4D 数据的函数:

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

您可以使用最适合您的 scatteredinterpolant function or the griddata 函数对数据进行插值。