Amazon deals

Wednesday, April 25, 2012

What a programmer needs to know about ordinary differential equations

[What are ordinary differential equations? Read my detailed notes here on Evernote:Math:ODE]

Solving an ordinary differential equation is computationally expensive. One of the most common approximation methods used to solve this is euler. Understanding this for non math buffs means a lot of effort.(Lets say "Ordinary.Differential.Equations_Tenenbaum_Pollard" has too many pages). So I wrote a python program that implements both euler and its more accurate form the predictor corrector method and thought I will share it.

What are we trying to achieve?
  1. Implement euler and corrector predictor method
  2. Plot them on a graph to understand their nature
  3. Compare the graph plots to visually understand the accuracy differences
What equation are we trying to solve?

dy/dx = x + 2y

What python libraries do we need?

import math
from pylab import *

Show me the code?
What are the results?

We can visually see from the following plots that
  1. Reducing step size increases accuracy
  2. The predictor corrector (shown with '+' ) is more closer to original equation (shown with '.' ) than the traditional euler method (shown with '*')
  • When we plot from 0 to 3 with step size 0.2



  • When we plot from 0 to 3 with step size 0.1


  • When we plot from 0 to 3 with step size 0.01

    -- Python
    -- Pure awesomeness :)

    1 comment:

    1. Cool, A nice writeup :)!

      Though some suggestions on hosting code, probably github is a better place to host your public code (its easy to embed them anywhere with a short script they give!), that would also allow collaborating and improving the code :).

      ReplyDelete