## Tuesday, April 20, 2010

### Area under the curve

I had a post the other day about the product rule in differential calculus, and how to extend it to find the derivative of a reciprocal and a quotient. The product rule also does heavy duty in the opposite direction, in the method called integration by parts. Strang has a wonderful diagram of that (shown above), which I've reproduced here using matplotlib.

The product rule is:

 (uv)' = u v' + v u'

Integrating both sides and isolating u v' we get:

 ∫u v' = u v - ∫v u'

where for the definite integral we have to remember to evaluate u times v at both of the limits of integration, subtracting the value at the lower limit from the upper one.

 ∫u v' = u2 v2 - u1 v1 - ∫v u'

Can you see each of these four terms as areas in the diagram? It's very pretty.

 from math import sqrtimport numpy as npimport matplotlib.pyplot as plt@np.vectorizedef f(x): return 100*sqrt(x) + 0.2*x**3# the curvedx = 0.01X = np.arange(0,10,dx)plt.plot(X,f(X),'r',lw=6,zorder=1)# the pointsu1 = 2u2 = 10plt.scatter(u1,f(u1),s=250, color='k',zorder=2)plt.scatter(u2,f(u2),s=250, color='k',zorder=2)# u dvX2 = np.arange(u1,u2+dx,dx)plt.fill_between(X2,f(X2),0, color='0.85',zorder=0)# v duYMAX = f(10)plt.fill_between(X2,YMAX,f(X2), color='0.35',zorder=0)X3 = np.arange(0,u1+dx,dx)plt.fill_between(X3,YMAX,f(u1), color='0.35',zorder=0) plt.text(6,100,s='$\int$ $u$ $dv$', fontsize=24)plt.text(2,300,s='$\int$ $v$ $du$', fontsize=24,color='w')plt.text(0,525, s='$y = 100 \sqrt{x}$ + $0.2 {x^3}$', fontsize=24,color='r')plt.text(2.5,115,s='$u_1$,$v_1$', fontsize=18)plt.text(10.5,500,s='$u_2$,$v_2$', fontsize=18)ax = plt.axes()r1 = plt.Rectangle((0,0),width=u1, height=f(u1),facecolor='cyan')ax.add_patch(r1)ax.set_xlim(-1,12.2)ax.set_ylim(-10,600)plt.savefig('example.png')