Lab 1, Exercise 4: Vectors
All of our programs will use vectors for position, velocity, and acceleration. In Python, a vector is represented as three numbers--one number for each component of the vector. All of our programs will use a Cartesian coordinate system.
In the previous exercises, you simply copied existing code and executed the programs. In this exercise, you will again copy the base code, but at the end of the exercise you will write your own code to solve a new problem. Look for your instructions in the comment statements at the end of the code below.
Open a new IDLE programming window and save the file with the filename YourName_Lab1_PHYS495_4.py. Type the following lines:
from __future__ import division
from visual import *
"""
Filename: YourName_Lab1_PHYS495_4.py
Author: Your Name
Date: 01/17/2007
Description: Vector quantities in Python.
"""
"""
A vector is declared with the 'vector' function,
which takes three arguments.
The first argument is the x-component; the second
argument is the y-component; the third argument is
the z-component.
The following lines declare two vectors, named A
and B. Notice that vector A points only along the x-axis,
since its y- and z-components are zero.
"""
A = vector(14.14, 0.0, 0.0) # vector pointing along the x-axis
B = vector(10.0, 10.0, 0.0) # vector pointing along x- and y-axes.
print "Vector A is ", A # don't use the format string!
print "Vector B is ", B # don't use the format string!
"""
To access a particular component, use the
dot-operator:
A.x for the x-component of A
A.y for the y-component of A
A.z for the z-component of A
DO use format strings for components of a vector, but
not when printing the full vector.
"""
print "The x-component of A is %3.2f " %A.x
print "The x- and y-components of B are %3.2f and %3.2f " %(B.x,B.y)
"""
To calculate the magnitude of a vector, use
the mag() function.
The following line declares the variable A_mag
and stores the magnitude of the vector A.
Then we do the same for B.
"""
A_mag = mag(A)
B_mag = mag(B)
print "The magnitude of vector A is %3.2f " %A_mag
print "The magnitude of vector B is %3.2f " %B_mag
"""
To calculate the dot product of two vectors, use
the dot() function.
"""
AB_dot = dot(A,B)
print "The dot product of vectors A and B is %3.2f " %AB_dot
"""
To calculate the cross product of two vectors, use
the cross() function.
Notice that we DON'T use a format string to print
AB_cross. It's a vector quantity.
"""
AB_cross = cross(A,B)
print "The cross product of A and B is ", AB_cross
"""
To add or subtract vectors, just use the + and -
operators.
"""
AB_add = A + B
print "The sum of A and B is ",AB_add
AB_subtract = A - B
print "The difference of A and B is ", AB_subtract
"""
It's easy to change the values of the components.
Just use the dot operator as you did above.
"""
A.x = 3.0
A.y = 4.0
A.z = 5.0
print "Now, vector A is ", A
"""
Create a new vector, using the name C.
Write code to add vectors A and B and store the result
in C. Then, evaluate the magnitude of C, using a new
variable name to store the result.
Finally, use format strings to print the value of each
component of C and its magnitude, using 3 decimal places.
"""
Hit the F5 key to execute the program. Make sure the output makes sense. It should look something like this:
Next: Exercise 5
Previous: Exercise 3
Go back to the Lab 1 main page.