๐Ÿ“ Symbolic Computation with SymPy#

โ€œSolving Your Physics Homework the Easy Way!โ€

๐Ÿค– What is Symbolic Computation?#

Definition

Symbolic computation allows you to compute, manipulate, and simplify mathematical expressions exactlyโ€”without numerical approximations.

  • Think of it as Newtonโ€™s Calculus Notebook in code form.

  • Instead of approximating ( \(\sqrt{8} = 2.828\) ), SymPy simplifies it to ( \(2\sqrt{2}\) )โ€”perfect for solving exact classical mechanics problems like trajectories or forces.

Historical Roots: Symbolic computation has its origins in Newtonian mechanics (17th century), where exact solutions were critical for describing planetary motion. ๐ŸŒ

Example 1: Projectile Motion โ€“ Solving for Time#

Problem:#

Find the time ( \(t\) ) when a projectile hits the ground. The height equation is: $\( h(t) = v_0t - \frac{1}{2}gt^2 \)$

Variables:#

  • (\(h(t)\)): Height at time (\(t\))(\(m\)).

  • (\(v_0\)): Initial velocity (\(m/s\)).

  • (\(g\)): Acceleration due to gravity ((9.8 \(\text{m/s}^2\))).

  • (\(t\)): Time (\(s\)).

Code:#

from sympy import symbols, solve
from sympy import init_printing

init_printing()  # Enables the best option for your environment

v0, g, t = symbols("v0 g t")
h = v0 * t - (1 / 2) * g * t**2
time = solve(h, t)
time
../../_images/c17259b95b19d4706783bbe7de957cf3fa411cb5cfa5dfba576c45dafb690a57.png

Explanation:#

  • (\(t = 0\)): When the projectile is launched.

  • (\(t = \frac{2v_0}{g}\)): When the projectile hits the ground.

Visualization:#

import numpy as np
import matplotlib.pyplot as plt

v0 = 20  # Initial velocity in m/s
g = 9.8  # Gravity in m/s^2
t = np.linspace(0, 2 * v0 / g, 100)
h = v0 * t - 0.5 * g * t**2

plt.plot(t, h)
plt.title("Projectile Motion")
plt.xlabel("Time (s)")
plt.ylabel("Height (m)")
plt.show()
../../_images/0fdfed108be5fcfa0c05b36557323dc4141d84b6067a9f12f78d8dce97c55f23.png

Example 2: Derivatives โ€“ Velocity from Position#

Problem:#

Derive the velocity equation from the position function of a falling object:
$\( s(t) = \frac{1}{2}gt^2\)$

Variables:#

  • (\(s(t)\)): Position (\(m\)).

  • (\(g\)): Gravity ((9.8 \(\text{m/s}^2)\)).

  • (\(t\)): Time (\(s\)).

Code:#

from sympy import diff

s, g, t = symbols("s g t")
s = (1 / 2) * g * t**2
v = diff(s, t)
v
../../_images/7c4a88d39a41d505f076551c5c3a917363636a579aa52dd29edd4167511d8488.png

Output:#

\(v(t) = g \cdot t\)

Visualization:#

g = 9.81  # Gravity in m/s^2
t = np.linspace(0, 5, 100)
v = g * t

plt.plot(t, v)
plt.title("Velocity of a Falling Object")
plt.xlabel("Time (s)")
plt.ylabel("Velocity (m/s)")
plt.grid()
plt.show()
../../_images/de3fccc8571a8842f21bd88561298c7e8013103af79a7d6988f5274de8d94404.png

Example 3: Work Done by a Spring#

Problem:#

Calculate the work ( W ) done by a spring stretched from ( \(x = 0\) ) to ( \(x = 1\) ):
$\(F(x) = -kx \quad \text{and} \quad W = \int F(x) dx\)$

Variables:#

  • (\(F(x)\)): Force (\(N\)).

  • (\(k\)): Spring constant (\(\text{N/m}\)).

  • (\(x\)): Displacement (\(m\)).

Code:#

k, x = symbols("k x")
F = -k * x
W = F.integrate((x, 0, 1))
W
../../_images/e51b65d4522ac8baffbceb6338a6173ee6d0a0f6942d49e4c57cb07df7a0e343.png

Output:#

\[W = -\frac{k}{2}\]

Visualization:#

x = np.linspace(0, 1, 100)
k = 50  # Spring constant
F = -k * x
W = -0.5 * k * x**2

plt.plot(x, F, label="Force (F)")
plt.plot(x, W, label="Work (W)")
plt.title("Spring Force and Work Done")
plt.xlabel("Displacement (m)")
plt.ylabel("Force (N) / Work (J)")
plt.legend()
plt.grid()
plt.show()
../../_images/93f14dafcf6b277ab07d2fddc66b3c02c802cb1fd5fdb6849259e771d881645a.png

Example 4: Center of Mass#

Problem:#

Find the center of mass ( \(x_{cm}\) ) of a rod with length ( \(L\) ) and a linear density ( \(\lambda(x) = \lambda_0x\) ): $\(x_{cm} = \frac{\int_0^L x\lambda(x)dx}{\int_0^L \lambda(x)dx}\)$

Variables:#

  • ( \(\lambda(x)\) ): Linear density (( \(\text{kg/m}\) )).

  • ( \(L\) ): Length of the rod (\(m\)).

  • ( \(x\) ): Position along the rod (\(m\)).

Code:#

from sympy import integrate

lambda0, L, x = symbols("lambda0 L x")
numerator = integrate(x * lambda0 * x, (x, 0, L))
denominator = integrate(lambda0 * x, (x, 0, L))
x_cm = numerator / denominator
x_cm
../../_images/2598ce3086e65706c0d2a921bf38ce315a208c15a306a61f8c9280dc502eb3a9.png

Output:#

\(x_{cm} = \frac{2L}{3}\)

Visualization:#

x = np.linspace(0, 10, 100)
lambda0 = 2  # Linear density constant
L = 10  # Length of rod
density = lambda0 * x

plt.plot(x, density, label="Linear Density (ฮป(x))")
plt.axvline(x=L * 2 / 3, color="red", linestyle="--", label="Center of Mass ($x_cm$)")
plt.title("Linear Density and Center of Mass")
plt.xlabel("Position (m)")
plt.ylabel("Density (kg/m)")
plt.legend()
plt.grid()
plt.show()
../../_images/89be040f80b380a1f2b3a45cc58d628665ef33a18f50b25ecd41a0c46f850bb5.png

Example 5: Energy in a Spring System#

Problem:#

Calculate the total energy ( \(E\) ) stored in a spring stretched to ( \(x = a\) ):
\(E = \frac{1}{2}kx^2\)

Variables:#

  • ( \(E\) ): Energy (\(J\)).

  • ( \(k\) ): Spring constant (( \(\text{N/m}\) )).

  • ( \(x\) ): Displacement (\(m\)).

Code:#

k, x = symbols("k x")
E = (1 / 2) * k * x**2
E
../../_images/2aea8d16110a75b9fc32f68c775103c15f154fa5517faff2aa24fd7fdea1029a.png

Output:#

\[E = \frac{kx^2}{2}\]

Visualization:#

x = np.linspace(0, 2, 100)
k = 100  # Spring constant
E = 0.5 * k * x**2

plt.plot(x, E)
plt.title("Energy Stored in a Spring")
plt.xlabel("Displacement (m)")
plt.ylabel("Energy (J)")
plt.grid()
plt.show()
../../_images/4268edee9c3599648c94aa749d76bccfebbc3e8d956072da9da8457ae2eef94f.png

Why SymPy for Classical Mechanics?#

  • Exact Solutions: No approximationsโ€”ideal for forces, energies, and motion.

  • Visualization: Combine symbolic calculations with numerical plotting for deeper insights.

  • Free and Open Source: A perfect alternative to costly tools like Mathematica.

โ€œWith SymPy, solving Newtonian mechanics problems becomes exact and intuitiveโ€”just like Newton intended!โ€ ๐Ÿš€