Skip to content

Instantly share code, notes, and snippets.

@krantikal
Last active April 2, 2021 06:03
Show Gist options
  • Save krantikal/2281e3b7c7b1e3c458c98f5de152513a to your computer and use it in GitHub Desktop.
Save krantikal/2281e3b7c7b1e3c458c98f5de152513a to your computer and use it in GitHub Desktop.
[Vector Projection Python] Problem #physics, #math,#QP,#SOL #pylab
# Testme\aacode\MidsemSolP1.py ; also contains ipynb
from pylab import *
# Construct arrays for two vectors given as a: (1,2,4) and b: (1,1,3)
# Find the component of a parallel to b
# Express a as a sum of vectors parallel and perpendicular to b
a=array([1,2,4])
b=array([1,1,3])
# Find magnitudes to find the unit vectors
amag=norm(a)
bmag=norm(b)
ahat=a/norm(a)
bhat=b/norm(b)
norm(ahat) #check
# Find the component of a parallel to b
a_b=dot(a,bhat)
# This is a along b
a_par=a_b*bhat
# This is a along a perpendicular to b
a_perp=a-a_par
dot(a_perp,b) #check, should return zero
a_par+a_perp #check, should return a in full
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment