Search Knowledge

© 2026 LIBREUNI PROJECT

Advanced SymPy: Calculus, Differential Equations, and Optimization

Beyond Simple Algebra

While simplifying polynomials is useful, SymPy’s real power for scientists lies in its ability to solve complex calculus problems and differential equations—tasks that usually require expensive software like Mathematica or Maple.

Taylor Series Expansions

In physics and engineering, we often approximate complex functions using Taylor series. SymPy can generate these to any arbitrary order.

python
1 
2import sympy as sp
3x = sp.symbols('x')
4 
5# Taylor series of sin(x) around 0 up to 10th order
6series = sp.series(sp.sin(x), x, 0, 10)
7print(f"Taylor series of sin(x):\n{series}")
8 
9# Remove the 'O' term for numerical evaluation
10func_approx = series.removeO()
11print(f"\nPolynomial version: {func_approx}")
12 

Solving Ordinary Differential Equations (ODEs)

SymPy can solve many classes of ODEs analytically. This is extremely useful for verifying numerical solvers or finding exact solutions for simple physical models.

Example: The Harmonic Oscillator

The equation is f(x)+ω2f(x)=0f''(x) + \omega^2 f(x) = 0.

python
1 
2import sympy as sp
3x = sp.symbols('x')
4f = sp.Function('f')
5omega = sp.symbols('omega', positive=True)
6 
7# Define the ODE
8diffeq = sp.Eq(f(x).diff(x, x) + omega**2 * f(x), 0)
9print(f"ODE: {diffeq}")
10 
11# Solve the ODE
12solution = sp.dsolve(diffeq, f(x))
13print(f"General Solution: {solution}")
14 

Multivariable Calculus

SymPy handles gradients, Jacobians, and Hessians with ease.

python
1 
2import sympy as sp
3x, y, z = sp.symbols('x y z')
4 
5f = x**2 + y**2 + z**2
6 
7# Partial derivatives
8df_dx = sp.diff(f, x)
9print(f"df/dx = {df_dx}")
10 
11# Gradient vector
12grad = [sp.diff(f, var) for var in (x, y, z)]
13print(f"Gradient: {grad}")
14 

Symbolic Linear Algebra

Calculating the eigenvalues or inverse of a matrix with symbols is a common requirement in theoretical research.

python
1 
2import sympy as sp
3a, b, c, d = sp.symbols('a b c d')
4 
5M = sp.Matrix([[a, b], [c, d]])
6print("Matrix M:\n", M)
7 
8# Determinant
9print("\nDeterminant:", M.det())
10 
11# Inverse
12print("\nInverse:\n", M.inv())
13 
14# Eigenvalues (returns a dict of value: multiplicity)
15print("\nEigenvalues:", M.eigenvals())
16 

Global Optimization (Symbolic)

If a function is simple enough, we can find its global minimum by solving where the derivative is zero.

python
1 
2import sympy as sp
3x = sp.symbols('x')
4 
5f = x**4 - 4*x**2 + 3
6 
7# Find critical points
8critical_points = sp.solve(sp.diff(f, x), x)
9print(f"Critical points: {critical_points}")
10 
11# Determine which is a minimum using second derivative
12for point in critical_points:
13 second_deriv = sp.diff(f, x, 2).subs(x, point)
14 nature = "minimum" if second_deriv > 0 else "maximum"
15 print(f"x = {point} is a {nature} (f''={second_deriv})")
16 

In the next section, we will integrate these symbolic results into numerical workflows using the SciPy stack.