Skip to main content

Find the resultant torque of coplanar forces with Python







Hello everyone, today I will show you how to use Python to calculate the resultant torque of coplanar forces. In this article, we will consider forces on a coordinate plane as shown below.


To calculate the resultant torque with this Python script, we want to know the magnitude, angle, and coordinates of each force.

With this script, we can calculate the resultant torque around any point of the coordinate plane.

Let's get started.

First of all, we have to install Python and other required software to write our code. After installing Python correctly, open the code editor and create a new file with the .py extension.

We don't have to install any other module as the only module we want to use in this project is the math module which comes with Python.

You can import the math module with the following line.

import math


Then we have to create a dictionary to store all the information about forces. You can use a list instead of a dictionary to do that but I prefer using a dictionary. 

Let's create a dictionary called a with the following line.

a = {}

Then we have to create a loop to collect all the forces from the user.


n = 1
while True:
   
    force = float(input("enter the magnitude of force in N: "))
    angle = math.radians(float(input("The angle with the X
axis in degrees ")))
    x,y  = input("enter the cordinates x,y").split(',')

    x_comp = force*math.cos(angle)
    y_comp = force*math.sin(angle)

    force = [int(x), int(y),x_comp,y_comp]
    a[str(n)] = force
    print
    if input("Enter 'y' to add another or 'q' to quit ") == 'y':
        n += 1
        continue
    else:
        break

In the first line, we print a welcome note 

Inside the loop,

we assign the magnitude of the force to a variable called force
and the angle made with the x-axis to a variable called angle
Then we get the coordinates of the force and assign them to the variables x,y


Then we calculate the x and y component of the force and assign them to the x_comp and y_comp variables. To learn more, have a look at this article



Then we create a list that contains all the information about the force and store it in the dictionary.



Then we use a simple if condition to check whether the user wants to add more forces. If they want we continue the loop and if not we break it.




As I mentioned above, this script can find the resultant torque around any point. So, we simply ask the user to input the coordinates of the point they want to consider as the center and store them in variables called center_x and center_y.



center_x, center_y =  input("Enter the coordinates of the center x,y ").split(',')
 

Then all we have to do is calculate the torque. To calculate the torque we simply create a for loop to calculate the torque made by each force and add them together with the following lines of code.

torque = 0

for force in a.values():
    force_torque = (force[0] - float(center_x))*force[3] +
(force[1] - float
(center_y))*force[2]
    torque += force_torque
print(torque)


That's all now it is time to test.





It works.




Now we can beautify the result. 





 
That's all here is the link to the full code.  https://github.com/chamodhk/TorqueFinder

 Don't forget to follow my blog click the hamburger icon on the top right corner and follow my GitHub for more.

Share & Comment.

Chamodh Nethsara.

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...