Visualizing Mean Value Theorem in Mathematica

The mean value theorem is a surprising Calculus result that states for any function ff differentiable on (a,b)(a,b)1 there exists x(a,b)x(a,b) such that

f(x)=f(b)f(a)ba

Here are three informal intuitions for what this means (all of them say the same thing in different ways):

  1. Physical example. If you travel 60 miles in one hour, at some point you must have been traveling exactly 60 miles per hour.2
  2. Geometric intuition. There exists a line tangent to ff parallel to the line between the endpoints (i.e. parallel to the line between (a,f(a))(a, f(a)) and (b,f(b))(b, f(b))). See figure 1 below for an illustration.
  3. Algebraic intuition. There exists a point xx at which instantaneous rate of change of ff is equal to the average change of ff on [a,b][a,b].

The proof is remarkably straightforward. You define a function hh that maps values between aa and bb to a height from ff to the line segment between the endpoints. This function hh turns out to be differentiable, and hence has a maximum value (and hence there is a point aa such that h(a)=0h’(a)=0). From there it’s an easy algebraic transformation to demonstrate the mean value theorem is true. See figure 2 for an illustration.

Mathematica

I won’t repeat the proof, but what I wanted to do is play with Mathematica to generate an interactive visualization of how the proof works (I generated above figures in Mathematica, but the final product has an interactive slider to make the proof more clear).

For tasks like this Mathematica is wonderful. Here I define a line at an angle, and another function ff that adds a sine to it. Because the visualization is interactive ss will allow the user to change the slope:

interpol[s_, a_, b_] := a + s*(b - a);

g[s_, x_] = interpol[s, 1/2, 0]*x + interpol[s, 1, 0];
f[s_, x_] = Sin[x - 1] + g[s, x];

These are symbolic definitions. We can plot them, take derivatives, integrate, and do all kinds of fancy computer algebra system operations. Here is an example. The vertical line in figure 2 comes down from the mean value of ff– i.e. a point on ff where the tangent is parallel to the average change. In Mathematica code we can take the derivative of ff (using Mathematica function D), and then solve (using Solve) for all values where the derivative is equal to mean value:

fD[a_] := D[f[s, x], x] /. x -> a;

avgSlope[s_] = (f[s, Pi + 1] - f[s, 1])/Pi;
meanPoint[s_] = 
  Solve[fD[x] == avgSlope[s] && x > 1 && x < Pi + 1, x];

This is the core of the code. Most of the rest of the code is plotting, which Mathematica does exceptionally well. I exported the plots to figures above using the Export function. Another notable function is Manipulate– this is what makes the graph dynamic as the user drags a slider (by changing the variable s which the equations depend on). Finally I was able to publish the notebook in a few clicks, as well as publish the visualization itself using CloudDeploy. Instantaneous deployment of complex objects is very cool and useful.

Snags

There are a few things I don’t like, but it’s too early to say these are Mathematica’s flaws (more likely it’s my lack of familiarity):

Normally I learn very unfamiliar/weird programming languages by writing a toy interpreter of the target language in a language I already know. The Mathematica rules engine is a great candidate. Putting this in my virtual shoebox of future projects.


  1. ff must also be continuous on [a,b][a,b].↩︎

  2. Assuming you were accellerating smoothly.↩︎

  3. Warning: this is probably not the most idiomatic Mathematica code.↩︎

Oct 06, 2024