# Scripting a visual comparison between the derivative and some finite scheme

I want to make an application where I can specify a function $f:\mathbb R\to \mathbb R$ and a dynamic value $n$ and get the function $\frac{f(x+\frac{1}{n})-f(x)}{\frac{1}{n}}$. I moreover want to compare it to $f'(x)$, i.e. plot it in the same graph.

I first tried it with $f(x):=x^m, m=2$ and used Listplot, but even here I got into some obsticles. Here is my attempt:

Table[((x+1/n)^m)-x^m)/(1/n),{m,1000,100}]

Function[x,#][Rangle[10]]&/@%

%/.{n->3}//N

Show[
Plot[3 x^2,{x,0,10}]
ListPlot[#]&/@%
]


If I do just ListPlot[#]&/@% instead of the last line, the I get all the plots, however it doesn't compare.

I'd also like to get away from $x^m$ and make an abstraction w.r.t. $f$, but my tries with Function fails. Is there a difference between Function[$x$,term] and $\lambda x.$term ?.

-
Something like With[{n = 1*^2, f = Function[x, x Exp[x]]}, Plot[{f[x], f'[x], n DifferenceDelta[f[x], {x, 1, 1/n}]} // Evaluate, {x, -1, 1}]]? – J. M. Jun 6 '13 at 8:03

Here's a way to approach this. Define the function f[x] and two versions of the derivative: the real derivative df and a numerical approximation appF.

f[x_] := x^3;
df[x_] = D[f[x], x];
appF[x_, h_] := (f[x + h/2] - f[x - h/2])/h


We can plot them all together

Plot[{f[x], df[x], appF[x, 1/2]}, {x, 0, 1}]


and see both how the derivative compares to the approximation, and also how the derivative(s) compare to the original function. Taking this a bit further, set up a Manipulate to control the h parameter in the approximation:

Manipulate[Plot[{df[x], appF[x, h]}, {x, 0, 1}], {h, 0.001, 1}]


Now h is controlled by the slider and you can see that it gets closer to the real derivative as h gets closer to zero. Here's a somewhat more visually appealing example:

f[x_] := Sin[x^3];
df[x_] = D[f[x], x];
appF[x_, h_] := (f[x + h/2] - f[x - h/2])/h;
Manipulate[Plot[{f[x], df[x], appF[x, h]}, {x, 0, 1}], {h, 0.001, 1}]


To change example functions, only the definition of f needs to change (and perhaps the region over which the plot is made). When I re-read your problem, I realized I used a slightly different formulation of the derivative apporximation. Here is the one that corresponds to your problem statement:

f[x_] := Sin[x^3];
df[x_] = D[f[x], x];
appF[x_, n_] := (f[x + 1/n] - f[x])/(1/n);
Manipulate[Plot[{f[x], df[x], appF[x, n]}, {x, 0, 1}], {n, 1, 100}]


which gives (more or less) the same results as above.

-

I'm not entirely clear on what your intention is, but are you looking for something like this?

fComp[f_, n_, x_] := {n*((f /. x -> (x + 1/n)) - f), D[f, x]}

Show[Plot[Evaluate[fComp[x^2, 3, x]], {x, 0, 100}]]

Show[Plot[Evaluate[fComp[Tan[x], 1, x]], {x, 0, 2 Pi}]]


Alternately, you can simplify the syntax of fComp if you don't mind passing pure functions:

fComp[f_, n_] := {n*(f[x + 1/n] - f[x]), D[f[x], x]}

Show[Plot[Evaluate[fComp[#^2 &, 3]], {x, 0, 100}]]

Show[Plot[Evaluate[fComp[Tan, 1]], {x, 0, 2 Pi}]]


There are two "tricks" to take note of here. First, functions can be passed as arguments, just like any other expressions in Mathematica. The second is the use of Evaluate[] to ensure that the value of x from Plot[] is passed to the function before Mathematica tries to plot it. You'll get errors about invalid variables, otherwise.

To compare values of n, you could do any of the following:

Show[Plot[Evaluate[fComp[#^2 &, #]], {x, 0, 1}]&/@Range[10]]

Table[Plot[Evaluate[fComp[#^2 &, n]], {x, 0, 1}],{n,1,10}]

Manipulate[Plot[Evaluate[fComp[#^2 &, n]], {x, 0, 1}],{n,1,10,1}]

-