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

Let's talk about cryptography.

Have you ever heard of cryptography?  People write their diaries every day. For many of them, their diary is one of their best friends to whom they tell their every secret. What happens when someone else finds such a diary? That would freak out the dairy's owner for sure. Also, it might put their lives in danger. Wise men always hide their diaries so no one else finds it.  But legends hide their message so that only they can read what they wrote. So even if the diary is found by someone else, they cannot know the secrets. for example, I wrote this in my diary today.   P dyval h isvn wvza hivba jyfwavnyhwof avkhf Can you understand it? This is just a very primitive level of cryptography yet powerful enough to hide what I wrote from 90% of people. I will discuss this type of cryptography in the next blog post. The human used cryptography from the very beginning to share their messages in secret and conceal their inventions. As you may know, many people tried to find a recip...

How to make a your own Discord bot with Python in 2023. #01

I'm sure that you have heard of discord bots way before reading this article. But have you ever tried to make your own bot with Python? If not, this is the time. Let's get started. Over time tons of discord bot tutorials have been published, but if you follow them they don't help you to build a functional bot because many of the packages used in them are updated and no longer support older syntaxes. Here is the full guide to creating your own Discord Bot in 2023 with Python. STEP 1: Create a Discord account First of all, we have to set up a developer account to create the bot. Actually, you can use your ordinary discord account to access the discord developer portal, but it is highly recommended to create a separate account for development purposes. You can create a new discord account with this link:  https://discord.com/register STEP 2: Create a new Application After creating the discord account, you have to go to the discord developer portal and sign in with your newly-c...