Lines, Planes and MATLAB

copyright © 2000 by Paul Green and Jonathan Rosenberg

In this notebook, we will use MATLAB to solve problems in three-dimensional geometry such as the ones in chapter 11 of the text by Gulick and Ellis.  We begin with the problem of finding the equation of a plane through three points.

 

Example 1:

Find an equation for the plane through the points (1,-1,3), (2,3,4), and (-5,6,7).

We begin by creating MATLAB vectors that represent the three points:

 

P1=[1,-1,3];

P2=[2,3,4];

P3=[-5,6,7];  

 

If you wish to see MATLAB's response to these commands, you should delete the semicolons. Next, we create the normal vector to our plane by taking the cross-product of two vectors parallel to the plane.

normal=cross(P1-P2,P1-P3)  

             

normal =

     9   -10    31  

             

Next, we declare x, y and z to be symbolic variables, we create a vector whose components represent the vector from P1 to a typical point P on the plane, and we compute the dot product of this vector with our normal.

 

syms x y z  

P=[x,y,z]  

             

P =

[ x, y, z]  

             

planefunction=dot(normal,P-P1)  

             

planefunction =

9*x-112-10*y+31*z  

The equation of our plane is now planefunction=0.   We remark that the MATLAB's symbolic dot product function dot assumes that its arguments may be complex numbers and takes the complex conjugates (i.e., changes the signs of the imaginary parts) of the components of its first argument. To see the effect of this, we compute

 

dot(P-P1, normal)  

 

ans =

-112+9*conj(x)-10*conj(y)+31*conj(z)  

             

             

We have created the function realdot.m to avoid this annoyance.

 

realdot(P-P1,normal)  

 

ans =

9*x-112-10*y+31*z  

             

Problem 1:

Find a function of three variables whose vanishing characterizes the plane through the three points (1,1,0), (2,3,-2) and (7,4,1).

 

Example 2:

Let us now use the equation of the plane in Example 1 to find the point of intersection of the plane with the line through (1,2,-1) and (3,3,3).

 

 

P4=[1,2,-1];

P5=[3,3,3];  

 

 

We parametrize the line:

 

syms t

   line=P4+t*(P5-P4)  

 

line =

[  1+2*t,    2+t, -1+4*t]  

 

 

Now line gives the coordinates of a typical point on our line in terms of the parameter t. We can now evaluate planefunction at such a point by substituting line for P.

 

 

newfunction=subs(planefunction,P,line)  

 

newfunction =

-154+132*t  

 

 

Now, we solve newfunction=0 for t, and substitute the result in line to obtain our desired point, and check our answer.

 

t0=solve(newfunction)

point=subs(line,t,t0)  

 

t0 =

7/6

point =

[ 10/3, 19/6, 11/3]  

 

 

subs(planefunction,P,point)  

 

ans =

0  

 

 

Notice that the command solve(newfunction) automatically sets the function to zero, recognizes the independent variable, and solves for it.

 

Problem 2:

Find the point in which the line through (1,1,1) and (1,3,0) meets the plane of Problem 1.

Example 3:

Let us now use the plotting capabilities of MATLAB to plot the plane and the line. We will solve the equation of the line for z, and use the MATLAB plotting function ezsurf to plot the result.  We will use the MATLAB function ezplot3 to plot the parametrized line.

 

The last argument to ezplot3 gives the minimum and maximum values of the parameter for the plot. ezsurf can also accept a domain argument consisting of a four component vector giving the minimum and maximum values for the two parameters in the form [xmin,xmax,ymin,ymax]. If the parameters are not x and y, MATLAB assigns limits in alphabetical order according to the same scheme.

 

The commands hold on and hold off cause the two plots to appear on the same diagram. Following the command hold on, MATLAB includes all succeeding plots on the same figure until the command hold off is issued.

 

 

zdep=solve(planefunction, z)  

 

zdep =

-9/31*x+112/31+10/31*y  

ezplot3(line(1),line(2),line(3),[-1,3]);hold on

































Problem 3:

Plot the plane of Problem 1 and the line of Problem 2 on a single figure.

 

Example 4:

Let us now see if we can find an equation for the cylinder of radius 3 around our line (Compare Gulick and Ellis Section 11.5 Problem 26). The cylinder in question is the set of all points whose distance from the line is 4. P already represents a generic point with coordinates (x, y, z) and line represents a point on the line. Thus the square of the distance from a point in space to a point on the line is given by

distsq=realdot(P-line,P-line)  

 

distsq =

(x-1-2*t)^2+(y-2-t)^2+(z+1-4*t)^2  

 

What we want to do is to minimize this squared distance as a function of t, while keeping  x, y and z generic.

distsqprime=diff(distsq,t)  

 

distsqprime =

-4*x+42*t-2*y-8*z  

 

tcrit=solve(distsqprime,t)  

 

tcrit =

2/21*x+1/21*y+4/21*z  

 

distsqfunc=simplify(subs(distsq,t,tcrit))  

 

distsqfunc =

6-2*x-4*y+2*z+17/21*x^2-4/21*x*y-16/21*x*z+20/21*y^2-8/21*y*z+5/21*z^2  

 

cylinder=distsqfunc-9  

 

cylinder =

-3-2*x-4*y+2*z+17/21*x^2-4/21*x*y-16/21*x*z+20/21*y^2-8/21*y*z+5/21*z^2  

 

Setting the last output to 0 gives us an equation for the cylinder.

 

Problem 4:

Find a function of three variables that takes the value 0 precisely on the cylinder of radius 5 around the line of Problem 2.

Additional Problems:

 

1.  In this problem you will find the distance from point P4 to the plane of Example 1 in two different ways:   

a) using the method of Section 11.6 of Gulick and Ellis, performing the calculations with MATLAB.

b) By finding the foot of the perpendicular from P4 to the plane as follows:  

(1)   Parametrize the line through P4 normal to the plane.

(2)   Find the point common to the line you have just parametrized and the plane, as in Example 2.

(3)   Find the distance between the point you have just found and P4.

(4)   Verify that methods a) and b) give the same answer.

 

2.  Find the equation of the plane in Example 1 in another way, by assuming that the equation has the form

ax + by + cz = 1

(this is always possible if the plane doesn't go through the origin), and solving for a, b and c so as to make the plane pass through P1, P2, and P3.  Check that your answer agrees with the one we found above.