Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168772
Image: ubuntu2004

Vectors

Points and Spheres

We start with plotting points.

points([(0,0), (1,1), (3,0.5)], pointsize=30)
points([(0,0,0), (1, 1, 1), (3, 0.5, -2)])

The following code helps you find a vector connecting two points and find the distance between two points. Norm gives the length of a vector.

p=vector((0,2,3)) q=vector((1,-2,0))
p-q
(-1, 4, 3)
norm(p-q)
sqrt(26)
(p-q).norm()
sqrt(26)

If you know the center and radius of a sphere, it is simple to draw.

sphere(center=(1,2,-1), size=2)

You can also draw it from an equation.

center=(1,2,-1) radius=2 var('x,y,z,x0,y0,z0,r') equation=(x-x0)^2+(y-y0)^2+(z-z0)^2==r^2 equation
(z - z0)^2 + (y - y0)^2 + (x - x0)^2 == r^2
equation(x0=center[0], x1=center[1],x2=center[2], r=radius)
(x - 1)^2 + (z - z0)^2 + (y - y0)^2 == 4
implicit_plot3d(equation(x0=center[0], y0=center[1],z0=center[2], r=radius), (x,-5,5),(y,-5,5),(z,-5,5))

Now let's do some operations with vectors.

u=vector([1,2]) v=vector([4,-3])

We can add, subtract, find the dot product, and the angle between the vectors.

u+v
(5, -1)
u-v
(-3, 5)
2*u
(2, 4)
2*u-4*v
(-14, 16)

A vector times another vector gives you the dot product.

u*v
-2

You can also compute a dot product using the dot_product method.

u.dot_product(v)
-2
norm(u)
sqrt(5)
v.norm()
5

Using the dot product formula, we can find the angle between two vectors.

theta=arccos(u*v/(u.norm()*v.norm())) theta
arccos(-2/25*sqrt(5))
n(theta) # in radians
1.75064982658737
n(theta*180/pi) # in degrees
100.304846468766

Projections

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

u=vector([3,2]) v=vector([4,-3])
def projection(u,v): return ((u*v)/(v*v)) * v
projection(u,v)
(24/25, -18/25)
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)+arrow(projection(u,v),u,color='green'), xmin=-1,xmax=1,ymin=-1,ymax=1)

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

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

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

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

Vectors in 3d

Everything also works in 3d.

u=vector([1,2,0]) v=vector([0,4,-2])
u+v
(1, 6, -2)
u*v
8
u.dot_product(v)
8
u.cross_product(v)
(-4, 2, 4)

The following (hidden) code makes it possible to have a cross product operator "*cross*".  If the example below doesn't work, then click on the "%hide" and evaluate the cell that pops up.

u *cross* v
(-4, 2, 4)
projection(u,v)
(0, 8/5, -4/5)
norm(u)
sqrt(5)
norm(v)
2*sqrt(5)

Here, we show u\vec u, v\vec v, u×v\vec u \times \vec v, and projvu\textrm{proj}_{\vec v} \vec u.  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))

Lines and Planes

To include a line, remember "Head minus Tail" gets the direction vector, and then you can pick your starting point.  parametric_plot then will give a graph. I like to use (Head minus Tail)*t+Tail, because then t=0t=0 corresponds to the tail, and t=1t=1 corresponds to the head. I will give 2 examples, a 2D one and a 3D one. In the example below, I show two points, the direction vector, and the line for 2t2-2\leq t\leq 2.

First, the direction vector:

var('t') P=vector([2,1]) Q=vector([0,3])
direction_vector=P-Q direction_vector
(2, -2)

Then the parameterization.

r=direction_vector*t+P r
(2*t + 2, -2*t + 1)
parametric_plot(list(r), (t, -2, 2))+points([r(t=-2),r(t=2)],pointsize=30)

You do almost the exact same thing for a 3d plot.

P=vector([2,1,3]) Q=vector([0,3,-1])
direction_vector=P-Q direction_vector
(2, -2, 4)
r=direction_vector*t+P r
(2*t + 2, -2*t + 1, 4*t + 3)
parametric_plot(list(r), (t, -2, 2))+points([r(t=-2),r(t=2)],pointsize=30)

If you want to graph a plane, use implicit_plot3d (just like the sphere example at the beginning). Don't forget that you need a double equal sign in equations. You may have to change the bounds to get your graph to show up.  Because we are only plotting a simple surface, we only need a few plot_points.  If you were plotting a more complicated surface, then you might delete that option (to use the default), or increase the number.

var('x,y,z') implicit_plot3d(x-2*y+3*z==4, (x,-2,2), (y,-2,2), (z,-2,2), plot_points=2)
implicit_plot3d(x-2*y+3*z==4, (x,-2,2), (y,-2,2), (z,-2,2), plot_points=2) + implicit_plot3d(x+y==0, (x,-2,2), (y,-2,2), (z,-2,2),color='green', plot_points=2)

To find a plane through three points, you get two vectors between the points, cross them to get the normal vector, and then use the dot product.  The code below will do all this, and then graph the result, along with the relevant vectors.  The first part of the code is for you to copy and learn to use. The latter half puts the information into a grid, and then creates a graph.

P=vector([2,1,3]) Q=vector([0,1,-1]) R=vector([1,0,2]) var('x,y,z') PQ=Q-P PR=R-P normal=PQ.cross_product(PR) equation=normal.dot_product(vector([x,y,z])-P)==0 html.table([["P",P], ["Q", Q], ["R",R], ["<font color='green'>PQ</font>",PQ],["<font color='purple'>PR</font>",PR],["Normal", normal], ["Equation",equation]])
P \left(2,1,3\right)
Q \left(0,1,-1\right)
R \left(1,0,2\right)
PQ \left(-2,0,-4\right)
PR \left(-1,-1,-1\right)
Normal \left(-4,2,2\right)
Equation -4 \, x + 2 \, y + 2 \, z = 0
ranges=zip((x,y,z), [min(coords)-1 for coords in zip(P,Q,R)], [max(coords)+1 for coords in zip(P,Q,R)]) p=points([P,Q,R], pointsize=30,color='red') p+=plot(PQ,color='green',thickness=3).translate(P) p+=plot(PR,color='green',thickness=3).translate(P) p+=plot(normal,color='orange',thickness=3).translate(P) p+=implicit_plot3d(equation, *ranges, plot_points=3) show(p, aspect_ratio=[1,1,1])