Skip to main content

How to find the resultant of multiple vectors with Python.





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

Trending Now

Find the Resultant vector of two Vectors With PYTHON!!

NEW ARTICLE:  How to Find the resultant of multiple vectors with Python! Using Python We can do many complex calculations. I learned how to calculate Resultant Vector using a formula with its size and direction in my Physics class. Then, I simply developed a CLI software to calculate the resultant of given two vectors Hope you like it :>) Follow me... Step :1 You need to install python on your computer. No matter what the operating system is you can find the ideal version of python from the official website of Python org And you will need a code editor like VS code and can download it with a simple Google search. (Hope you hate the first step Haha ) Step 2: we use the math module to calculate our resultant vector. The method I used as follows math.radians() : converts degrees to radians math.degrees() : converts radians to degrees math.sqrt()  : find the square root of a number math.atan() :find the tan inverse Step 3: the formula I used to calculate the size of the resultant is as

How to send and receive discord messages with your bot in 2023.

Hello everyone. Welcome back to the Blog of Chamodh today I will show you how to send and receive discord messages with your bot. Before getting started, I remind you that this is the second episode of the series How to make Discord bot in 2023 and if you haven't read the previous one, please read that first as I explain how to create your bot application and add it to a server there. READ:  https://blogofchamodh.blogspot.com/2023/02/how-to-make-your-own-discord-bot-2023.html We don't gonna use virtual environments, IDEs, or any other utility because we can use Replit for our coding purposes.  Let's Get Started. STEP1: Set up Replit It is really simple. Just go to  https://replit.com/~ and sign up if you have never used its service before and create a new Repl. I don't hope to guide you in this as this is an intermediate tutorial. Make sure to select Python as you create the new Repl because we are going to use Python for our back end. STEP 2: Get your token. To run ou