drawing 3d contour plot from 3d vector

2020-02-15 07:50发布

I want to draw a contour plot for 3D data.

I have a force in x,y,z directions I want to plot the contour3 for that

the dimensions of the Fx = 21x21X21 same for Fy and Fz

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

This is the error I am getting Error using contour3 (line 129) When Z is a vector, X and Y must also be vectors.

2条回答
爷、活的狠高调
2楼-- · 2020-02-15 08:24

I'm not sure if this solution is what you want.

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".

For representation of vector fields there is quiver, quiver3, streamslice and streamline functions.

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.

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!

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!

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.

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.

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:

  • black for (-1,-1,-1),
  • red for (1,-1,-1),
  • grey for (0,0,0)

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)
查看更多
Viruses.
3楼-- · 2020-02-15 08:37

Your problem is that FS is not the same shape as X and Y.

Lets illustrate with a simple example:

X=[1 1 1
   2 2 2
   3 3 3];
Y=[1 2 3
   1 2 3
   1 2 3];
Z=[ 2 4 5 1 2 5 5 1 2];

Your data is probably something like this. How does Matlab knows which Z entry corresponds to which X,Y position? He doesnt, and thats why he tells you When Z is a vector, X and Y must also be vectors.

You could solve this by doing reshape(FS,size(X,1),size(X,2)) and will probably work in your case, but you need to be careful. In your example, X and Y don't seem programatically related to FS in any way. To have a meaningful contour plot, you need to make sure that FS(ii,jj,k)[ 1 ] corresponds to X(ii,jj), else your contour plot would not make sense.

Generally you'd want to plot the result of FS against the variables your are using to compute it, such as ii, jj or k, however, I dont know how these look like so I will stop my explanation here.

[ 1 ]: DO NOT CALL VARIABLES i and j IN MATLAB!

查看更多
登录 后发表回答