Sub topic 16
Letting Code Solve the Equations
Assume you know basic algebra, simultaneous linear equations, right-triangle trigonometry, SI units, and the meaning of acceleration. This chapter shows how to give equations of motion to a short Python program and use the result safely.
A numerical solution finds values for unknowns such as acceleration, tension, or normal force. Code does not replace the free-body diagram. You still decide which forces act, choose signs, and write the equations. Code then performs the repetitive algebra and checks the result.
Turn physics into a linear system
For many mechanics problems, the unknowns appear linearly. You can write the equations in the form
A x = b
Here, x is a list of unknown quantities.
| Part | Meaning in a force problem |
|---|---|
| `A` | Coefficients from the component equations |
| `x` | Unknown accelerations or forces |
| `b` | Known terms, often involving mass and gravity |
Consider an ideal Atwood machine. Mass m1 is heavier and moves downward. Mass m2 moves upward. Let a be the shared acceleration magnitude and T the rope tension.
The two free-body equations are
m1*g - T = m1*a
T - m2*g = m2*aRearrange them so the unknowns are on the left:
m1*a + T = m1*g
m2*a - T = -m2*gThe unknown vector is [a, T]. The coefficients and known terms are therefore
A = [[m1, 1],
[m2, -1]]
b = [m1*g, -m2*g]The solution must still agree with the directions selected in the diagram.
Solve and verify with Python
The following program solves a two-by-two linear system without an extra library. It then substitutes the answer back into both original equations and prints the residuals. A residual is the difference between the two sides of an equation. It should be close to zero.
def solve_2x2(A, b):
a11, a12 = A[0]
a21, a22 = A[1]
b1, b2 = b
determinant = a11 * a22 - a12 * a21
if determinant == 0:
raise ValueError("The equations do not have one unique solution.")
x1 = (b1 * a22 - a12 * b2) / determinant
x2 = (a11 * b2 - b1 * a21) / determinant
return x1, x2
m1, m2 = 3.0, 2.0
g = 9.81
A = [[m1, 1.0],
[m2, -1.0]]
b = [m1 * g, -m2 * g]
acceleration, tension = solve_2x2(A, b)
residual_1 = m1 * g - tension - m1 * acceleration
residual_2 = tension - m2 * g - m2 * acceleration
print(f"a = {acceleration:.3f} m/s^2")
print(f"T = {tension:.3f} N")
print(f"residuals = {residual_1:.2e}, {residual_2:.2e}")The output gives a = 1.962 m/s^2 and T = 23.544 N. The residuals are essentially zero.
A reliable coding workflow
Use this order:
- Draw one free-body diagram per object.
- Define unknowns and positive directions.
- Write one component equation for each independent direction.
- Enter coefficients carefully, preserving signs.
- Check units, signs, and residuals.
- Test a limiting case, such as equal masses or zero friction.
Exercise
In the Atwood example, what should the acceleration become when m1 equals m2?
Show answer
It should become zero because the two weights balance. The tension should become m1*g, assuming an ideal rope and pulley.