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.
I'm not sure if this solution is what you want.
Your problem is that
contour
andcontour3
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
andstreamline
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:Un-comment the type of plot You want to see. For quiver use
resolution
of 0.1, for other cases use 0.01.Your problem is that
FS
is not the same shape as X and Y.Lets illustrate with a simple example:
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
andY
don't seem programatically related toFS
in any way. To have a meaningful contour plot, you need to make sure thatFS(ii,jj,k)
[ 1 ] corresponds toX(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 asii
,jj
ork
, however, I dont know how these look like so I will stop my explanation here.[ 1 ]: DO NOT CALL VARIABLES
i
andj
IN MATLAB!