MATH 232 Sage Lesson 1: Vectors

768 days ago by scofield

Points, Lines and Vectors

a = [3,-1,2] b = (-1, -2, 4) print type(a) type(b) 
       
<type 'list'>
<type 'tuple'>
<type 'list'>
<type 'tuple'>
points((a,b)) 
       
points([a,b], size=10) + line([a,b], thickness=2) 
       
u = vector(a) v = vector(b) print u + v plot(u+v, color='blue', thickness=2) + plot(u, color='red', thickness=2) + plot(v, color='green', thickness=2) 
       
(2, -3, 6)
(2, -3, 6)
arccos(u*v/(norm(u)*norm(v))) 
       
arccos(1/42*sqrt(14)*sqrt(21))
arccos(1/42*sqrt(14)*sqrt(21))

Projections

Let's make a function that projects one vector onto another vector.

u=vector([3,2]) v=vector([4,-3]) v[1] 
       
-3
-3
def projection(u,v): return ((u*v)/(v*v)) * v 
       
type(projection(u,v)) 
       
<type 'sage.modules.vector_rational_dense.Vector_rational_dense'>
<type 'sage.modules.vector_rational_dense.Vector_rational_dense'>
plot(u)+plot(v)+plot(projection(u,v),color='red',aspect_ratio=1) 
       
plot(u)+plot(v)+plot(projection(u,v),color='red',aspect_ratio=1)+line([u,projection(u,v)],linestyle=':') 
       
@interact def show_projection(r1=slider(0,n(2*pi))): u=vector([cos(r1),sin(r1)]) v=vector([cos(n(pi/3)),sin(n(pi/3))]) show(plot(u,color='black')+plot(v)+plot(projection(u,v),color='red',aspect_ratio=1)+circle((0,0),1,color='yellow')+arrow(projection(u,v),u,color='green'), xmin=-1,xmax=1,ymin=-1,ymax=1) 
       

Click to the left again to hide and once more to show the dynamic interactive window

To calculate the magnitude of the projection (i.e., the component of \vec u in the direction of \vec v), we can split the projection up into its magnitude and direction: \left(\frac {\vec u \cdot \vec v}{||\vec v||}\right)\frac{\vec v}{||\vec v||}.

To calculate the magnitude of the projection (i.e., the component of u in the direction of v), we can split the projection up into its magnitude and direction: \left(\frac {\bf u \cdot \bf v}{||\bf v||}\right)\frac{\bf v}{||\bf v||}.

(u*v)/norm(v) 
       
6/5
6/5

The component of \vec u that is orthogonal to \vec v is found by vector subtraction.

The component of u that is orthogonal to v is found by vector subtraction.

u-projection(u,v) 
       
(51/25, 68/25)
(51/25, 68/25)

Alternately, we might replace v with a vector w that is orthogonal to v, and then find the projection of u onto w:

def orthogComponent(u,v): vperp = vector((v[1], -v[0])) return projection(u, vperp) orthogComponent(u,v) 
       
(51/25, 68/25)
(51/25, 68/25)

The projection of u onto v and the orthogonal component represent perpendicular vectors which, together with u, form a right triangle.  Naturally, then, u is the sum of these components.

print "u is", u, "and the sum of the components is likewise", projection(u, v) + orthogComponent(u, v) show(plot(u,color='red') +plot(v,color='green') +plot(projection(u,v)) +plot(orthogComponent(u,v)) +arrow(projection(u,v), u, rgbcolor=(0,0,0.5)), aspect_ratio=1) 
       
u is (3, 2) and the sum of the components is likewise (3, 2)
u is (3, 2) and the sum of the components is likewise (3, 2)

Plane containing two vectors, and a normal vector

There are several ways to plot equations in three variables, all of which must first be declared to be variables.  One way is to use the implicit_plot3d( ) command.  We plot the plane

   2x + 3y - z = 4

and set the "viewing box" using this command below.  Note the double-equals used when entering the equation.

var('x y z') implicit_plot3d(2*x+3*y-z==4, (x,-2,2), (y,-2,2), (z,-2,2)) 
       
u = vector([1,3,-1]) v = vector([-1,4,-2]) 
       

Next, we display the two vectors defined above, the plane containing them, and the cross product (vector) of u and v, which is naturally normal to the plane containing u and v.

enn = u.cross_product(v) var('x y z') show(plot(u,color='red',thickness=2,aspect_ratio=[1,1,1]) +plot(v,color='blue',thickness=2) +plot(enn,color='orange',thickness=2) +implicit_plot3d(enn[0]*x + enn[1]*y + enn[2]*z == 0, (x,-5,5), (y,-5,5), (z,-3,8))) 
       

More fancy: We show u (red), v (blue), the cross product of u and v (orange), and the projection of u onto v (black).  We also show the parallelogram whose area equals the length of the cross product.

show(plot(u,color='red',thickness=2,aspect_ratio=[1,1,1]) +plot(v,color='blue',thickness=2) +plot(u.cross_product(v),color='orange',thickness=2) +plot(projection(u,v),color='black',thickness=2) +plot(u-projection(u,v),color='green',thickness=2) +polygon([(0,0,0),u,u+v,v],color='yellow',opacity=0.3)) 
       

Recall that many surfaces are graphs of an equation z = f(x, y).  The partial derivatives of f provide "rates of change" along the surface---i.e., both (1, 0, f_x) and (0, 1, f_y) lie in the tangent plane.  You just have to decide at what target point (x_0, y_0) you seek these items.  In the next cell, we plot the surface z = 2x^2 + y^2 with target point (-1, 2), the vectors (1, 0, f_x) and (0, 1, f_y) above the target point, the tangent plane containing these vectors, and the normal to the surface.

var('x y z') # not needed if done in an earlier cell f(x,y) = 2*x^2 + y^2 x0 = -1; y0 = 2; z0 = f(x0, y0); atPt = vector((x0,y0,z0)) pslopes = f.gradient()(x0, y0) u = vector((1,0,pslopes[0])); v = vector((0,1,pslopes[1])); enn = u.cross_product(v) show(plot3d(f(x,y), (x, -3, 3), (y, -3, 3), color='green') +implicit_plot3d(enn[0]*(x-x0)+enn[1]*(y-y0)+enn[2]*(z-z0)==0, (x,-3,3), (y,-3,3), (z,0,27), opacity=0.7) +arrow3d(atPt, atPt+u, color='red', thickness=2) +arrow3d(atPt, atPt+v, color='red', thickness=2) +arrow3d(atPt, atPt+v.cross_product(u), color='orange', thickness=3))