Solving Differential Equations

In this notebook I'll show how one can solve differential equations with known initial conditions in python. Note that it is possible to find general solutions to differential equations on a computer, using programs like Mathematica or python's sympy, however here I'll just focus on situations where you know the initial condition. We'll use this to check some of our answers from the tutorial.

To solve an ODE in python, we can use odeint(dydx, y0, x) from scipy's integrate package. We have to give this three arguments:

  1. A function, dydx,of $y$ and $x$ that specifies the derivative $dy/dx$,
  2. The initial condition of $y$,y0 and
  3. the $x$ positions we'd like to evaluate the solution at x. Below I'll show a couple of examples of using this.

In the tutorial, we found that the differential equation $$\frac{dy}{dx} = \frac{x^2 - \tan y}{x} \cos^2 y$$ with the boundary condition that $y(x=1) = \pi/4$ has the solution $$ y(x) = \arctan \left[ \frac{x^3}{3} + \frac{2}{3x} \right] . $$

We also found that the differential equation $$ \frac{dy}{dx} = y^4 - \frac{y}{x} $$ with the boundary condition that $y(x=1) = (2/3)^{1/3}$, has solution $$y(x) = \left( \frac{2}{3x} \right)^{1/3}.$$