Numerical Python

My experiments with Python continues on. This time it’s playing with the numerical and plotting capabilities of Python.

The shortest path between two points on a plane is the straight line connecting the two points. There’s a way to derive the equation of the shortest path using variational calculus. This experiment derives such an equation by doing a high dimensional gradient descent on the space of equations that satisfy the Euler-Lagrange equation.

We are looking for a curve that minimizes the arc length between the two points. We define y(x) as the curve, y_x as the x differential of y.

I[y] = \int_a^b \sqrt{1 +(y_x(x))^2} dx

The corresponding Euler-Lagrange equation is

\frac{d}{dx} \frac{\partial \sqrt{1 +(y_x)^2}}{\partial y_x} = 0

The plot below was generated by matplotlib, a plotting library that is almost exactly similar to the Matlab plotting interface. I was surprised at how close Python code can be to Matlab code. Nowhere in my code do I have to loop over the dimensions of the vectors. The numerical calls are passed over to high performance math routines written in Fortran. The initial condition was a sine wave and you can see that it eventually converges to the true solution.

Matplotlib

Get the code here.

Possibly related:

One Response to “Numerical Python”

  1. Fitting data with Python » Ganesh Swami Says:

    [...] a heavy user of the numerical capabilities of Python. I’ve written about my experiments before, but now I’m writing production quality code with numpy and [...]

Leave a Reply