Using sympy for a PDE

Using sympy for a PDE

If we already know the form of the solution to a PDE, then there is no difference from the approach we just demonstrated with the simple harmonic oscillator.

However, PDEs are more diverse and difficult to solve in general and it is very unlikely that you will be able to treat sympy as a black box for this. The documentation for the PDE solvers is here:

\[ \frac{\partial^2 u}{\partial t^2} = c^2 \frac{\partial^2 u}{\partial x^2} \]

Let’s just check to see that we can write this in the appropriate symbolic form that we can form the relevant derivatives

import sympy
import math
import numpy as np

Symbolic approach

from sympy.core.symbol import Symbol
from sympy.core.function import Function

t     = Symbol('t')
x     = Symbol('x')
c     = Symbol('c', positive=True)
omega = Symbol('omega', positive=True)
k     = Symbol('k', positive=True)
U     = Function('U')

# Potential solution
X = sympy.exp(-sympy.I * omega * t -sympy.I * k * x)
F = U(t,x).diff(t,2) -  c**2 * U(t,x).diff(x,2)
eq=sympy.Eq(F, 0)
eq=sympy.Eq(F, 0)
eq2 = eq.replace(U(t,x), X).simplify()
sympy.solve(eq2, k)
eq2.subs(k, omega/c )