A weight of 22lbs is hung by a ring. The ring is held by two cords pulled apart.
The cord A on the left is at an angle $\alpha$ = 45° CW relative to the -x-axis (45° above horazontal)
The cord B on the right is at an angle $\beta$ = 60° CCW relative to the +x-axis (60° above horazontal)
The tension in cord A
The tension in cord B
To solve this problem with python we are going to use numpy arrays, and the numpy`s linear algebra solver.
To begin we need to import numpy as np and from numpy's linear algebra module .linalg import the matrix inverse function inv. To decrease the number of digits we see when we print out a numpy array we'll use numpys np.set_printoptions(precision=3) this will give use three digits past the decimal point.
In [9]:
import numpy as np
from numpy.linalg import inv
np.set_printoptions(precision=3)
Now we will define the angles $\alpha$ and $\beta$ in radians
In [10]:
alpha = 45*np.pi/180
beta = 60*np.pi/180
print(alpha)
print(beta)
Next we will create a numpy array which has two rows and two columns (2 x 2 array). The terms will be the rows will be the sum of the forces in x and the sum of the forces in y. The columns will correspond to coefficients multiplied by A and coefficients multiplied by B.
In [11]:
A = np.array([[-np.cos(alpha), np.cos(beta)],[np.sin(alpha), np.sin(beta)]])
print(A)
Next we create the answer matrix B. This is a two row, one column matrix (2 x 1 array). The rows are the sum of the x-terms (xero) and sum of the y-terms (22)
In [12]:
B = np.array([[0],[22]])
print(B)
Now use numpy`s np.linalg.solve() function and pass in our two arrays. We will assign the output of the solve() function to the variable x.
When we print(x) we see the answer to our problem.
In [13]:
x = np.linalg.solve(A,B)
print(x)
We can also have the output of the solve() function be split into two variables, a and b.
In [14]:
a,b = np.linalg.solve(A,B)
print(a,b)
We can also solve this problem without using numpy's linalg.solve() function.
To produce $A^{-1}$, the inverse of A, we need to use numpy's np.linalg.inv() function. To matrix multiply the terms $A^{-1}$ and $B$, we need to use numpy's matrix multiplication function np.dot().
In [15]:
a,b = np.dot(inv(A),B)
print(a)
print(b)
Rounding to three sig figs and printing out the answer in a pretty way
In [16]:
print('a = %3.1f lb'%a)
print('b = %3.1f lb'%b)
In [ ]: