Chapter 2 Assignment

452 days ago by monetta

1)

# function definition for finding the area of a triangle given its vertices # using formula: ||AB x AC|| / 2 # input: A, B, and C are row vectors with the vertex points of the triangle # returns: area of the triangle as a number def triangle(A, B, C): AB_vector = B - A AC_vector = C - A return (AB_vector.cross_product(AC_vector)).norm() / 2 
       

Demonstration of the triangle function using the vertices given in Example 8 on pg. 145 in the text book.

# The vertices of the triangle p0 = vector([1, 2, 2]) p1 = vector([3, 1, 4]) p2 = vector([5, 2, 1]) area = triangle(p0, p1, p2) #Display result print "The area of the triangle is", area 
       
The area of the triangle is \newcommand{\Bold}[1]{\mathbf{#1}}\frac{3}{2} \, \sqrt{13}
The area of the triangle is \newcommand{\Bold}[1]{\mathbf{#1}}\frac{3}{2} \, \sqrt{13}

 

2)

# function definition for finding the volume of a parallelepiped given 3 of its sides # using formula |u•(v x w)| # input: u, v, and w are vectors of the sides of the parallelepiped # returns: volume of the parallelepiped as a number def parallelepiped(u, v, w): vxw = v.cross_product(w) # cross product of u and w result = u.dot_product(vxw) # dot product of u and (v x w) result = abs(result) return result 
       

Demonstration of the parallelepiped function using the vectors given in Exercise 47 on pg. 147 in the textbook.

# Three of the sides of the parallelepiped a = vector([2, 2, 0]) b = vector([1, 4, 0]) c = vector([1, 2, 4]) volume = parallelepiped(a, b, c) #Display result print "The volume of the parallelepiped is", volume 
       
The volume of the parallelepiped is \newcommand{\Bold}[1]{\mathbf{#1}}24
The volume of the parallelepiped is \newcommand{\Bold}[1]{\mathbf{#1}}24

 

3)

# function definition for finding the area of a parallelogram given its vertices # using formula: ||AB x AC|| # input: A, B, C and D are row vectors with the vertex points of the parallelogram # returns: area of the parallelogram as a number def parallelogram(A, B, C, D): AB_vector = B - A AC_vector = C - A return (AB_vector.cross_product(AC_vector)).norm() 
       

Demonstration of the triangle function using the vertices in Example 8 on pg. 145 in the textbook.

# The vertices of the parallelogram p0 = vector([1, 2, 2]) p1 = vector([3, 1, 4]) p2 = vector([5, 2, 1]) p3 = vector([0, 0, 0]) # made this up area = parallelogram(p0, p1, p2, p3) # Display result print "The area of the parallelogram is ", area 
       
The area of the parallelogram is  \newcommand{\Bold}[1]{\mathbf{#1}}3 \, \sqrt{13}
The area of the parallelogram is  \newcommand{\Bold}[1]{\mathbf{#1}}3 \, \sqrt{13}

 

4)

# function definition for finding the coefficients equation of a plane (Ax + By +Cz = D) given three 3-D points # input: a, b, and c are vectors containing the coordinates of the three points # and 'flag' is the flag variable (original value of variable will be overwritten # returns: # If the points a, b, and c are collinear, then -1 is returned. # If the points a, b, and c are not collinear then 1 is returned and the 'coefficients' has the coefficients to the # equation of the plane ([A, B, C, D]) def plane(a, b, c, coefficients): u = b - a v = c - a n = u.cross_product(v) if (n.norm() == 0): # coordinates are collinear return -1 else: # coordinates are not collinear coefficients.extend(list(n)) # add first 3 coefficients to the list list_a = list(a) # turn first coordinates into a list # Calculate D D = 0 for i in range(len(coefficients)): D = D + coefficients[i] + list_a[i] # add D to the list of coefficients coefficients.append(D) return 1 
       

Demonstration of the triangle function using the points in Example 7 on pg. 156 in the textbook

# The three points on the plane p0 = vector([1, 3, 2]) p1 = vector([2, 0, -1]) p2 = vector([4, 5, 1]) # extra variables used for displaying the results c = [] # will contain the list of coefficients if the points are not collinear # f will contain the flag f = plane(p0, p1, p2, c) if (f == 1): F(x, y, z)= c[0]*x + c[1]*y + c[2]*z print "The equation of the plane is", F(x, y, z) elif (f == -1): print "The points are collinear." 
       
The equation of the plane is \newcommand{\Bold}[1]{\mathbf{#1}}9 \, x - 8 \, y + 11 \, z = 7
The equation of the plane is \newcommand{\Bold}[1]{\mathbf{#1}}9 \, x - 8 \, y + 11 \, z = 7

Demonstration of the triangle function using the points on pg. 161

# The three points on the plane p0 = vector([2, 1, 5]) p1 = vector([6, 3, 15]) p2 = vector([-4, -2, -10]) # extra variables used for displaying the results c = [] # will contain the list of coefficients if the points are not collinear # f will contain the flag f = plane(p0, p1, p2, c) if (f == 1): F(x, y, z)= c[0]*x + c[1]*y + c[2]*z print "The equation of the plane is", F(x, y, z) elif (f == -1): print "The points are collinear." 
       
The points are collinear.
The points are collinear.