Area Between Two Curves

In the example of consumer surplus, we interpreted the surplus as an area between the demand curve and horizontal line determined by the equilibrium price. Now, we want to look at the situation with more complex curves to represent and solve area problems. Let’s begin by considering the area between the curves \(f(x) = x\) and \(g(x) = x^2\) from \(x = 0\) to \(x = 1\).

In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
In [9]:
def f(x):
    return x

def g(x):
    return x**2

x = np.linspace(0,1,100)
plt.plot(x,f(x))
plt.plot(x,g(x))
plt.fill_between(x, f(x), color = "grey",alpha = 0.3, hatch = '|')
plt.fill_between(x, g(x), color = "yellow", alpha = 0.2, hatch = '-')
Out[9]:
<matplotlib.collections.PolyCollection at 0x113f83208>
_images/0.10_calc_area_between_curves_3_1.png

Note that the area between the two curves is simply the difference between the area under \(f\) and the area under \(g\). We can represent this area with a definite integral:

\[\int_0^1 x - x^2 ~ dx\]

And we can use our patterns from summations to evaluate this. We can also use sympy to check our results.

In [10]:
import sympy as sy

x = sy.Symbol('x')
sy.integrate(f(x) - g(x), (x, 0,1))
Out[10]:
1/6

We can summarize this with a plot.

In [16]:
x = np.linspace(0,1,100)
plt.plot(x, f(x))
plt.plot(x, g(x))
plt.fill_between(x, f(x), g(x), color = 'lightpink', alpha = 0.4, hatch = '-')
Out[16]:
<matplotlib.collections.PolyCollection at 0x1156669b0>
_images/0.10_calc_area_between_curves_7_1.png

In the problem above, we knew the boundary points of the region and were able to set up the definite integral using these. We may instead have some curves and a region formed by the intersection of these curves that is of interest.

In [74]:
def f(x):
    return 12 - x**2

def g(x):
    return x**2 - 6
In [75]:
x = np.linspace(-10,10,1000)
plt.plot(x, f(x))
plt.plot(x, g(x))
plt.fill_between(x, f(x), g(x), alpha = 0.2)
Out[75]:
<matplotlib.collections.PolyCollection at 0x116d69fd0>
_images/0.10_calc_area_between_curves_10_1.png

Suppose we want to find the area of the middle region here. We would need to know the points of intersection of the curves and use these as boundaries for our definite integral. We can find these with sympy, update the plot, and evaluate the integral.

In [76]:
x = sy.Symbol('x')
sy.solve(f(x) - g(x), x)
Out[76]:
[-3, 3]
In [77]:
x = np.linspace(-4,4,1000)
fill = np.linspace(-3,3,1000)
plt.plot(x, f(x))
plt.plot(x, g(x))
plt.fill_between(fill, f(fill), g(fill), color = 'lightblue', alpha = 0.2)
Out[77]:
<matplotlib.collections.PolyCollection at 0x116f96d68>
_images/0.10_calc_area_between_curves_13_1.png

Note that we created a list based on the points of intersection to tell the plot where to fill between.

In [78]:
x = sy.Symbol('x')
sy.integrate(f(x) - g(x), (x, -3,3))
Out[78]:
72

We can have regions that are determined by more than one set of intersections as well. Here, we can determine the area over a domain where the curves switch positions. For example, consider the functions \(f(x) = \sin{x}\) and \(g(x) = \cos{x}\) from \(x=0\) to \(x = \pi/2\).

In [93]:
def f(x):
    return np.sin(x)

def g(x):
    return np.cos(x)

x = np.linspace(0, np.pi/2, 1000)
plt.plot(x, f(x), label = '$f(x)$')
plt.plot(x, g(x), label = '$g(x)$')
plt.fill_between(x, f(x), g(x), color = 'red', alpha = 0.2)
plt.legend(loc = 'best')
Out[93]:
<matplotlib.legend.Legend at 0x116fc3fd0>
_images/0.10_calc_area_between_curves_17_1.png
In [94]:
x = sy.Symbol('x')
y1 = sy.sin(x)
y2 = sy.cos(x)
sy.solve(y2 - y1)
Out[94]:
[-3*pi/4, pi/4]
In [98]:
area_1 = sy.integrate(y2 - y1, (x, 0, sy.pi/4))
In [99]:
area_2 = sy.integrate(y1 - y2, (x, sy.pi/4, sy.pi/2))
In [101]:
sy.pprint(area_1 + area_2)
-2 + 2⋅√2