Est. read time: 1 minute | Last updated: January 17, 2025 by John Gentile


Contents

Open In Colab

Overview

Differential equations are important for fields such as engineering, physics, science and others as they have phenomena that are best described, modeled and solved via differential equations. Ordinary differential equations are differential equations that depend on a single variable. Complex differential equations that are dependent on more than one variable are partial differential equations.

from sympy import *
x, y, z = symbols('x y z')
init_printing()

Ordinary Differential Equations (ODEs)

First-Order ODEs

The simplest ODEs are first-order because they involve the first derivative of an unknown function and no higher derivatives; specific differential equations are generally described of order n where the nth derivative of the unknown function is the highest derivative present. These unknown functions represent y(x)y(x) or y(t)y(t) depending on the units in question.

Thus, a first-order ODE takes the explicit form:

y=f(x,y)y{}'=f(x,y)

Solving

Using Sympy, can represent the differential equation f(x)2f(x)+f(x)=sin(x)f''(x) - 2f'(x) + f(x) = \sin(x):

# Create an undefined functions f(x) and g(x)
f, g = symbols('f g', cls=Function)
diffeq = Eq(f(x).diff(x, x) - 2*f(x).diff(x) + f(x), sin(x))
diffeq
f(x)2ddxf(x)+d2dx2f(x)=sin(x)\displaystyle f{\left(x \right)} - 2 \frac{d}{d x} f{\left(x \right)} + \frac{d^{2}}{d x^{2}} f{\left(x \right)} = \sin{\left(x \right)}
# solve using `dsolve` with arbitrary constants C1, C2, etc.
dsolve(diffeq, f(x))
f(x)=(C1+C2x)ex+cos(x)2\displaystyle f{\left(x \right)} = \left(C_{1} + C_{2} x\right) e^{x} + \frac{\cos{\left(x \right)}}{2}

Partial Differential Equations (PDEs)

References