Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
967 views
in Technique[技术] by (71.8m points)

matlab - drawing 3d contour plot from 3d vector

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...