Search code examples
matlabmatlab-figurelinear-algebra

How to level this plane to z=0


Idea
Trying to plot a plane based on two vectors and centered at the origin of the vectors

Issue
The plane isn't located at z=0 (since these vectors are in R2, it is expected to do so.)

Details
The vectors are taken from a matrix entered and stored in "valor". To run the cross function is needed vectors of size 3, but valor is of size 2, so it is created another matrix called valor1 to adjust this.

Code

   figure(1);
   valor1 =[ valor(1,1) valor(1,2) 0; valor(2,1) valor(2,2) 0;0 0 0]; #adjusted matrix to run cross
    v1 = valor1(:,1);
    v1 = v1 / norm(v1) ; #normalized vectors
    v2 = valor1(:,2);
    v2 = v2 / norm(v2 ) ;
    p = 10*(rand(3,1) - 0.5);  #points of the plane
    v3 = cross(v1,v2);   #cross product
    [ x , y ] = meshgrid( p(1)+(-5:5) , p(2)+(-5:5) ); #the limits of the plane
    z = p(3) - (v3(1)*(x-p(1)) + v3(2)*(y-p(2)))/v3(3); #plane equation
    surf(x,y,z) #graph it all
    hold all
    z = zeros(size(valor, 1), 1); 
    quiver3(z, z, z, valor(:, 1), valor(:, 2), z, 0); #plot the vectors entered
    hold off
    axis equal

Example obtained
enter image description here

What was expected
The plane must have their origin at the same origin of the vectors

enter image description here


Solution

  • The variable p is defining a point on the plane. However, because p is being generated randomly using p = 10*(rand(3,1) - 0.5);, generally speaking the z-coordinate of that point is not 0 (as @JAC pointed out).

    It's not clear to me why you are randomly generating (and then subtracting 0.5) a point rather than simply setting p = [0 0 0], but assuming you have a good reason to do that, simply force p(3) to be 0 as in the adapted code below.

    figure(1);
    valor1 =[ valor(1,1) valor(1,2) 0; valor(2,1) valor(2,2) 0;0 0 0]; %adjusted matrix to run cross
    v1 = valor1(:,1);
    v1 = v1 / norm(v1) ; %normalized vectors
    v2 = valor1(:,2);
    v2 = v2 / norm(v2 ) ;
    p = [10*(rand(2,1) - 0.5); 0];  %point on the plane with z=0
    v3 = cross(v1,v2);   %cross product
    [ x , y ] = meshgrid( p(1)+(-5:5) , p(2)+(-5:5) ); %the limits of the plane
    z = p(3) - (v3(1)*(x-p(1)) + v3(2)*(y-p(2)))/v3(3); %plane equation
    surf(x,y,z) %graph it all
    hold all
    z = zeros(size(valor, 1), 1); 
    quiver3(z, z, z, valor(:, 1), valor(:, 2), z, 0); %plot the vectors entered
    hold off
    axis equal