Question

In: Computer Science

1. Writing new functions for the following: Find the magnitude of a vector (as one function...

1. Writing new functions for the following:

  • Find the magnitude of a vector (as one function that works on both 2D and 3D vectors).
  • Normalize a vector (also working for both 2D and 3D).
  • Compute the dot product of two vectors.
  • Compute the cross product of two vectors.
  • Get the distance between two positions.
  • Get the angle between two vectors.
  • Get a point on a ray (given the origin, direction, and parametric value along ray).

2. Use Python lists to representing vectors and positions. Note that the vectors and positions can be either 2D or 3D, so your code should use the length of the input lists (using the len() function) to determine whether 2D or 3D is used.

3. Plan carefully by understanding the inputs and outputs needed for each function.

4. Test each function by calling it at least once.

5. Include documentation for each function with descriptive comments.

Solutions

Expert Solution

import  math

#to find magnitude of a given 2-D or 3-D vector
def magnitude(vector):

    #check for 2-D vector
    if len(vector)==2:
        return math.sqrt(vector[0]**2+vector[1]**2)

    #3-D vector
    else:
        return math.sqrt(vector[0] ** 2 + vector[1] ** 2+vector[2]**2)

#to normalize a given 2-D or 3-D vector
def normalize(vector):

    m=magnitude(vector)

    #magnitude of a given vector should be > 0
    if m>0:

        #2-D
        if len(vector) == 2:
            vector[0] = vector[0] / m
            vector[1] = vector[1] / m
        #3-D
        else:
            vector[0] = vector[0] / m
            vector[1] = vector[1] / m
            vector[2] = vector[2] / m

    return vector


#to find the Dot-Product of two given vector (2-D or 3-D)
def dotProduct(vector1,vector2):

    dP=0
    if len(vector1) == len(vector2):
        for i in range(0,len(vector1)):
            dP=dP+(vector1[i]*vector2[i])

    return dP

#to find the Cross-Product of two given vector (2-D or 3-D)
def crossProduct(vector1,vector2):

    #for 3-D
    if len(vector1) == 3 and len(vector2) == 3:
        cP =(vector1[1] * vector2[2]) - (vector1[2] * vector2[1])+(vector1[2] * vector2[0]) - (vector1[0] * vector2[2])+(vector1[0] * vector2[1]) - (vector1[1] * vector2[0])

    #for 2-D
    else:
        cP = (vector1[0] * vector2[1]) - (vector1[1] * vector2[0])

    return cP


#to find the distance of two given position(2-D or 3-D)
def distance(pos1,pos2):

    # for 2-D
    if len(pos1) and len(pos2) == 2:
        return math.sqrt((pos1[0]-pos2[0])**2+(pos1[1]-pos2[1])**2)

    # for 3-D
    else:
        return math.sqrt((pos1[0]-pos2[0])**2+(pos1[1]-pos2[1])**2+(pos1[2]-pos2[2])**2)

# to find the angle between two given vectors
def findAngle(vector1,vector2):

    angle=math.degrees(math.acos(dotProduct(vector1,vector2)/(magnitude(vector1)*magnitude(vector2))))

    return angle

#main() to test the functionality of each functions
def main():


    #create two 3-D vectors
    vector1 = [3, 2, 1]
    vector2 = [4, 7, 5]

    #create two positions in 3-D
    pos1 = [7, 9, 5]
    pos2 = [3, 4, 2]

    print("Given vector1 is :")
    print(str(vector1[0])+"i+ "+str(vector1[1])+"j+ "+str(vector1[2])+"k ")

    print("Given vector2 is :")
    print(str(vector2[0]) + "i+ " + str(vector2[1]) + "j+ " + str(vector2[2]) + "k ")

    print("Magnitude of vector1 is ",magnitude(vector1))
    print("Magnitude of vector2 is ", magnitude(vector2))

    print("Normalize form of vector1 is ")

    vector3 = normalize(vector1)
    print(str(vector3[0]) + "i+ " + str(vector3[1]) + "j+ " + str(vector3[2]) + "k ")
    print("Normalize form of vector2 is ")

    vector3 = normalize(vector2)
    print(str(vector3[0]) + "i+ " + str(vector3[1]) + "j+ " + str(vector3[2]) + "k ")

    print("Dot product of vector1 and vector2 is ", dotProduct(vector1,vector2))

    print("Cross product of vector1 and vector2 is ", crossProduct(vector1, vector2))

    print("Distance between two positions is ",distance(pos1,pos2))

    print("Angle between two vectors is ",findAngle(vector1,vector2))

#calling main()
if __name__ == '__main__':
    main();

Sample Input and Output of above code:

Given vector1 is :
3i+ 2j+ 1k
Given vector2 is :
4i+ 7j+ 5k
Magnitude of vector1 is 3.7416573867739413
Magnitude of vector2 is 9.486832980505138
Normalize form of vector1 is
0.8017837257372732i+ 0.5345224838248488j+ 0.2672612419124244k
Normalize form of vector2 is
0.4216370213557839i+ 0.7378647873726218j+ 0.5270462766947299k
Dot product of vector1 and vector2 is 0.8733260632194672
Cross product of vector1 and vector2 is 0.1408590424547527
Distance between two positions is 7.0710678118654755
Angle between two vectors is 29.15251940703007

Code screenshots:


Related Solutions

1. Magnitude & Direction of Vector Write a function m-file to find the magnitude and the...
1. Magnitude & Direction of Vector Write a function m-file to find the magnitude and the direction of cosine of vector F which expresses 3 dimension - x, y, and z. Here are requirements for the function m-file. - Parameter-list (input(s)): · 3 dimensional vector · row vector or column vector - undetermined - Return Value (outputs): · Magnitude of a given vector, |F| = √ F2 x + F2 y + F2 z · Angles(degree) from direction of cosine...
For the following 2 functions, find a unit vector at the specified point for the following...
For the following 2 functions, find a unit vector at the specified point for the following function, allowing the function to increase the fastest in this direction, and find the fastest rate of change. 1) f(x,y) = e^x(siny) , P(0, 3/π) 2) f(x,y,z) = arctan (x+y+z), P(1,1,1)
1.Find the derivative of the product between a scalar function and a vector function using the...
1.Find the derivative of the product between a scalar function and a vector function using the product formula. 2. Find the volume of an irregular solid using triple integration, the first integral should have at least one limit with variables. 3. Determine the moment of inertia of an irregular solid using triple integration. the first integral should have at least one limit with variables. 4. Find the angle between two lines using dot product. the two lines should not pass...
1.Find the derivative of the product between a scalar function and a vector function using the...
1.Find the derivative of the product between a scalar function and a vector function using the product formula. 2. Find the volume of an irregular solid using triple integration, the first integral should have at least one limit with variables. 3. Determine the moment of inertia of an irregular solid using triple integration. the first integral should have at least one limit with variables. 4. Find the angle between two lines using dot product. the two lines should not pass...
Question 3. Determine if the following vector fields are conservative. If they are,find their potential functions....
Question 3. Determine if the following vector fields are conservative. If they are,find their potential functions. a.F=〈y,x+z,−y〉. b.F=〈y+z,x+z,x+y〉.
Consider the following vector function. r(t) =<3t, 1/2 t2, t2> (a) Find the unit tangent and...
Consider the following vector function. r(t) =<3t, 1/2 t2, t2> (a) Find the unit tangent and unit normal vectors T(t) and N(t). (b). Find the curvature k(t).
1. The following two linear functions represent a market (thus one is a supply function, the...
1. The following two linear functions represent a market (thus one is a supply function, the other a demand function). Circle the answer closest to being correct. Approximately what will suppliers willingly supply if the government controls the market price to be $3.00 (You must first find the market equilibrium price and quantity in order to see how the $3.00 relates to them)? Q = 100 – 4.6P and Q = 75 + 6.2P Possible answers: 2.3 84.3 86.2     89.3    ...
Report-writing is considered one of the essential functions of the accounting profession. Why? A good report-writing...
Report-writing is considered one of the essential functions of the accounting profession. Why? A good report-writing style needs to comply with some basic principles. Identify these principles and, in respect of each, summarise its main features.
1.29 Prove or disprove that this is a vector space: the real-valued functions f of one...
1.29 Prove or disprove that this is a vector space: the real-valued functions f of one real variable such that f(7) = 0.
Write a new MATLAB function as described in the following: 1. The new function will have...
Write a new MATLAB function as described in the following: 1. The new function will have three input parameters: f, W, C. 2. Parameter f will specify the frequency of the square wave. 3. The parameter W will specify the width of the single pulse as a number of sample periods. 4. The parameter C will specify the number of square wave cycles. 5. Calculate a number of samples, N, to run the simulation for both waveforms. 6. Create one...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT