riccs_0x
riccs_0x

Reputation: 201

how to plot a plane spanned by two vectors in matlab?

I have a matrix, lets say

value = [ 1,0,0;0,1,0]

and need to plot a plane in the space generated by these vectors, something like these: enter image description here
with the next code

 tt=-0.5:0.1:1.5;
 ss=-0.5:0.1:1.5;
 [s,t]=meshgrid(ss,tt);
 grid on
 mesh(0+(value(1:1)*t)+(value(2:1)*s),0+(value(1:2)*t)+(value(2:2)*s),(0+value(1:3)*t)+(value(2:3)*s))
 view(45, 45);

but all that I got is the next figure enter image description here
Matlab sends me an error in the line of the mesh command.

Mesh instruction explained
I used the idea that a plane need a point and two vectors in the next way
Point (0,0,0)
u=1,0,0
v=0,1,0
so in parametric equations you would have

w=(x=(0+t+0s),y=(0+0t+s),z=(0+0t+0s)
but the idea is generalize to any matrix of the size, so instead of fixed values, it is expressed in terms of the matrix elements in the mesh part. I have seen a couple of methods, but these involve the extremes of the plane (that depends on the vectors, so isnt always known) and use something like patch.

Upvotes: 0

Views: 1773

Answers (1)

user1543042
user1543042

Reputation: 3440

As @Dev-il said you just need to use the cross function

% Generate vectors
v1 = rand(3,1) - 0.5 ;
v1 = v1 / norm(v1) ;
v2 = rand(3,1) - 0.5 ;
v2 = v2 / norm(v2 ) ;

% Generate point
p = 10*(rand(3,1) - 0.5);

% Start actual code
v3 = cross(v1,v2); % get plane's normal vector

% Points on plane
[ x , y ] = meshgrid( p(1)+(-5:5) , p(2)+(-5:5) );

% Equation for a plane
z = p(3) - (v3(1)*(x-p(1)) + v3(2)*(y-p(2)))/v3(3);

% plot plane
surf(x,y,z)

% plot vectors
hold all
v1Plot = num2cell( [ p , p + v1 ] , 2 );
v2Plot = num2cell( [ p , p + v2 ] , 2 );
v3Plot = num2cell( [ p , p + v3 ] , 2 );
plot3( v1Plot{:}, v2Plot{:}, v3Plot{:} )
hold off

axis equal

Upvotes: 1

Related Questions