Hello everyone, welcome back to my blog. Two years ago, I published a post on "How to find the resultant of two vectors with Python." and it has become the most popular post on my blog. So I thought to give a deep explanation of the updated version of that resultant finder.
In the "How to find the Resultant of two vectors" script we learned, how to write a script that finds the resultant of just two vectors but what if we want to find the resultant of three or more vectors?
We have to write a new script.
Logic.
Here we use different logic to calculate the resultant.
First, we collect all the vectors with the angle they make with the horizontal level to calculate their x and y components. Look at the image below.
I hope that will help you to get an idea of what x and y components are and how to calculate them.
Then we calculate the sum of all x components and y components. As the direction of all components of each category (x & y), we can simply add them together as we do addition in normal algebra.
At last, we build the vector triangle and calculate the magnitude of the resultant using the Pythagorean theory.
The magnitude of the resultant (R) is equal to R = √(X2+Y2)
and to calculate the θ we can use the tan ratio.
tan θ = Y∕X
θ = tan-1(Y/X)
That's all and let's move to the code.
Code.
Now we can look into the code. It's simple to write a code to get the above things done by Python.
The code is below. Take a look and then you can move to the explanation.
import math
def calculate():
X= 0
Y=0
x_comps = []
y_comps = []
while True:
vector =float(input('Enter the magnitude of the vector you want to add: '))
angle = float(input('Enter the angle made withthe horizontal level in degrees: '))
angle = math.radians(angle)
x = vector * math.cos(angle)
y = vector * math.sin(angle)
x_comps.append(x)
y_comps.append(y)
if input("\n do you want add more (y/n)?").lower() == 'n':
break
else:
pass
for comp in x_comps:
X += comp
for comp in y_comps:
Y+= comp
resultant = math.sqrt(X**2+Y**2)
angle = math.degrees(math.atan(Y/X))
print(resultant)
print(angle)
calculate()
X and Y are to store the sum of each type of component and the x_comps and y_comps are the lists that store the components of each vector.
The while loop helps us repeat the input process and stop it when we want. when you input a vector the scripts get its x and y components and store them in x_comps and y_comps lists.
When the loop is stopped, the scripts add all the components together and assign the values to X and Y using a for loop.
then the scripts calculate the resultant and the angle using the logic mentioned above.
math.atan is used to calculate the tan inverse and the math.degrees is used to convert radians to degrees as the math.atan return the angle in radians.
that's all and hope you learned something interesting. If yes, don't forget to leave a comment below.
Thank you!
Graphics and the images of this post are created by me and prohibited to use without my written permission.
Comments
Post a Comment