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

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