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.
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
Post a Comment