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 sqrt
import numpy as np
import matplotlib.pyplot as plt

@np.vectorize
def f(x):
return 100*sqrt(x) + 0.2*x**3
# the curve
dx = 0.01
X = np.arange(0,10,dx)
plt.plot(X,f(X),'r',lw=6,zorder=1)

# the points
u1 = 2
u2 = 10
plt.scatter(u1,f(u1),s=250,
color='k',zorder=2)
plt.scatter(u2,f(u2),s=250,
color='k',zorder=2)
# u dv
X2 = np.arange(u1,u2+dx,dx)
plt.fill_between(X2,f(X2),0,
color='0.85',zorder=0)
# v du
YMAX = 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')

No comments: