从3d矢量绘制3d等高线图 [英] drawing 3d contour plot from 3d vector
问题描述
我想为3D数据绘制轮廓图.
I want to draw a contour plot for 3D data.
我要在x,y,z方向上施加力,因此我想为此绘制轮廓3
I have a force in x,y,z directions I want to plot the contour3 for that
Fy和Fz的Fx的尺寸= 21x21X21
the dimensions of the Fx = 21x21X21 same for Fy and Fz
我发现力= f * vector(x,y,z) 然后
I am finding force = f*vector(x,y,z) Then
Fx(x,y,z) = force(1)
Fy(x,y,z) = force(2)
Fz(x,y,z) = force(3)
我做了以下事情,但是它对我不起作用?为什么以及如何绘制该图
I did the following but it is not working with me ?? why and how can I plot that
FS = sqrt(Fx.^2 + Fy.^2 + Fz.^2);
x = -10:1:10;
[X,Y] = meshgrid(x);
for i=1:length(FS)
for j = 1:length(FS)
for k=1:length(FS)
contour3(X,Y,FS(i,j,k),10)
hold on
end
end
end
这是我遇到的错误 使用轮廓3时出错(第129行) 当Z是向量时,X和Y也必须是向量.
This is the error I am getting Error using contour3 (line 129) When Z is a vector, X and Y must also be vectors.
推荐答案
我不确定该解决方案是否是您想要的.
I'm not sure if this solution is what you want.
您的问题是contour
和contour3
是表示2D对象中标量场的图.请注意,球是2D对象-每个点都由角度theta和phi定义-即使它是空间"中的对象,也不是平面"中的对象.
Your problem is that contour
and contour3
are plots to represent scalar field in 2D objects. Note that ball is 2D object - every single point is defined by angles theta and phi - even it is an object in "space" not in "plane".
用于表示矢量场的有quiver
,quiver3
,streamslice
和streamline
函数.
For representation of vector fields there is quiver
, quiver3
, streamslice
and streamline
functions.
如果要使用轮廓图,则必须将数据从矢量场转换为标量场.因此,必须将格式为 F = f(x,y,z)的数据转换为H = f(x,y)的形式.在那种情况下,H是MxN矩阵,x和y分别是Mx1和Nx1向量.然后contour3(x,y,H)
将起作用,从而生成所谓的3D图形.
If you want to use contour plot, you have to transform your data from vector field to scalar field. So your data in form F = f(x,y,z) must be transformed to form of H = f(x,y). In that case H is MxN matrix, x and y are Mx1 and Nx1 vectors, respectively. Then contour3(x,y,H)
will work resulting in so-called 3D graph.
如果依赖向量字段,则必须指定6个向量/矩阵,它们的大小与x,y,z坐标和Fx,Fy,Fz向量值的大小相同. 在那种情况下,quiver3(x,y,z,Fx,Fy,Fz)将起作用,从而生成6D图形. 明智地使用它!
If you rely on vector field You have to specify 6 vectors/matrices of the same size of corresponding x, y, z coordinates and Fx, Fy, Fz vector values. In that case quiver3(x,y,z,Fx,Fy,Fz) will work resulting in 6D graph. Use it wisely!
当我评论Ander的答案时,您可以使用色彩空间获取更多尺寸,因此您可以创建5D或理论上为6D,因为您有x,y,z坐标用于位置,而R,G,B坐标用于价值观.我建议为5D图形使用静态(x,y,R,G,B),为6D使用动画(x,y,t,R,G,B). 明智地使用它!
As I comment the Ander's answer, you can use colourspace to get more dimensions, so You can create 5D or, theoretically, 6D, because you have x, y, z coordinates for position and R, G, B coordinates for the values. I'd recommend using static (x,y,R,G,B) for 5D graph and animated (x,y,t,R,G,B) for 6D. Use it wisely!
在示例中,我显示了上面提到的所有方法.我选择了重力场并计算出比重心低0.25个单位的平面.
In the example I show all approaches mentioned above. i chose gravity field and calculate the plane 0.25 units below the centre of gravity.
假设在极坐标中定义的力场为 F =- r /r ^ 3; F = 1/r ^ 2. 这里 x 和 y 都在-1; 1范围内,并且大小相同N. F 是MxMx3矩阵,其中 F (ii,jj)是对应于 x (ii)和 y (jj). 矩阵 H (ii,jj)是 F (ii,jj)和 X , Y 和 Z 是坐标矩阵.
Assume a force field defined in polar coordinates as F=-r/r^3; F=1/r^2. Here both x and yare in range of -1;1 and same size N. F is the MxMx3 matrix where F(ii,jj) is force vector corresponding to x(ii) and y(jj). Matrix H(ii,jj) is the norm of F(ii,jj) and X, Y and Z are matrices of coordinates.
最后一个命令可确保F值在(-1; 1)范围内. F./2+0.5
移动F的值,使其适合RGB范围.颜色含义为:
Last command ensures that F values are in (-1;1) range. The F./2+0.5
moves values of F so they fit into RGB range. The colour meaning will be:
- 黑色代表(-1,-1,-1)
- 红色代表(1,-1,-1)
- 灰色代表(0,0,0)
取消注释要查看的绘图类型.对于颤抖,请使用resolution
为0.1,对于其他情况,请使用0.01.
Un-comment the type of plot You want to see. For quiver use resolution
of 0.1, for other cases use 0.01.
clear all,close all
% Definition of coordinates
resolution=0.1;
x=-1:resolution:1;
y=x;
z=-.25;
%definition of matrices
F=zeros([max(size(x))*[1 1],3]); % matrix of the force
X=zeros(max(size(x))*[1 1]); % X coordinates for quiver3
Y=X; % Y coordinates for quiver3
Z=X+z; % Z coordinates for quiver3
% Force F in polar coordinates
% F=-1/r^2
% spherical -> cartesian transformation
for ii=1:max(size(x))
for jj=1:max(size(y))
% temporary variables for transformations
xyz=sqrt(x(ii)^2+y(jj)^2+z^2);
xy= sqrt(x(ii)^2+y(jj)^2);
sinarc=sin(acos(z/xyz));
%filling the quiver3 matrices
X(ii,jj)=x(ii);
Y(ii,jj)=y(jj);
F(ii,jj,3)=-z/xyz^2;
if xy~=0 % 0/0 error for x=y=0
F(ii,jj,2)=-y(jj)/xyz/xy*sinarc;
F(ii,jj,1)=-x(ii)/xyz/xy*sinarc;
end
H(ii,jj)=sqrt(F(ii,jj,1)^2+F(ii,jj,2)^2+F(ii,jj,3)^2);
end
end
F=F./max(max(max(F)));
% quiver3(X,Y,Z,F(:,:,1),F(:,:,2),F(:,:,3));
% image(x,y,F./2+0.5),set(gca,'ydir','normal');
% surf(x,y,Z,F./2+.5,'linestyle','none')
% surf(x,y,H,'linestyle','none')
surfc(x,y,H,'linestyle','none')
% contour3(x,y,H,15)
这篇关于从3d矢量绘制3d等高线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!