lizard2020
lizard2020

Reputation: 55

Write a function to calculate a unit vector

The homework problem is written as follows:

Write a function called unitVec that determines a unit vector in the direction of the line that connects two points (A and B) in space. The function should take as input two vectors (lists), each with the coordinates of a point in space. The output should be a vector (list) with the components of the unit vector in the direction from A to B. If points A and B have two coordinates each (i.e., they lie in the x y plane), the output vector should have two elements. If points A and B have three coordinates each (i.e., they lie in general space), the output vector should have three elements.

I have basically the entire code written but cannot for the life of me figure out how to square each element in the list called connects[].

To calculate a unit vector the program will subtract the elements in vector B with the corresponding elements in vector A and create a new list (connects[]) with these values. Then each of these elements needs to be squared and they all need to be added together. Then the square root will be taken of this number and each element in connects[] will be divided by this number and stored in a new list which will be the unit vector. I'm trying to add the squares of elements in connects[] by using the line

add = add + (connects[i]**2)

but I know this only returns the list twice. The rest of my code is fine I just need help squaring these elements.

from math import *

vecA = []
vecB = []
unitV = []
connects = []
vec = []

elements = int(input("How many elements will your vectors contain?"))


for i in range(0,elements):
    A = float(input("Enter element for vector A:"))
    vecA.append(A)
    B = float(input("Enter element for vector B:"))
    vecB.append(B)  

def unitVec(vecA,vecB):
    for i in range(0,elements):
        unit = 0
        add = 0
        connect = vecB[i] - vecA[i]
        connects.append(connect)
        add = add + (connects[i]**2)
        uVec = sqrt(add)
        result = connects[i]/uVec
        unitV.append(result)
    return unitV

print("The unit vector connecting your two vectors is:",unitVec(vecA,vecB))

Upvotes: 0

Views: 4716

Answers (2)

Akaisteph7
Akaisteph7

Reputation: 6554

You need to change your function to the following:

def unitVec(vecA,vecB):
    add = 0
    for i in range(0, elements):
        unit = 0
        connect = vecB[i] - vecA[i]
        connects.append(connect)
        add = add + (connect**2)

    uVec = sqrt(add)
    unitV = [val/uVec for val in connects]
    return unitV

You cannot do everything in a single for loop, since you need to add all the differences before being able to get the square root. Then you can divide the differences by this uVec.

Upvotes: 1

Seong
Seong

Reputation: 626

python's list is for general use and its arithmetric operation is different from vector operation. for example, [1,2,3]*2 is replication operation instead of vector scalar multiplication such that result is [1,2,3,1,2,3] instead of [2,4,6]. I would use numpy array which is designed for numerical array and provide vector operations.

import numpy as np

a = [1,2,3]

# convert python list into numpy array
b = np.array(a)
# vector magnitude
magnitude = np.sqrt((b**2).sum()) # sqrt( sum(b_i^2))
# or
magnitude = (b**2).sum()**0.5 # sqrt( sum(b_i^2))
# unit vector calculation
unit_b = b/magnitude

Upvotes: 0

Related Questions