Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...