Computing Derivatives

Preface: Quick and dirty notebook to see if I could do calculus concepts with Jupyter Notebooks. Taken from GVSU's Active Calculus open source calculus book.

Chapter 2

Computing Derivatives

(...)

In this present chapter, we will investigate how the limit definition of the derivative,

$$ f'(x)=\lim_{h\to0}\frac{f\big(x+h\big)-f\big(x\big)}{h} $$

leads to interesting patterns and rules that enable us to quickly find a formula for $$f'(x)$$ based on the formula for $$f(x)$$ without using the limit definition directly. For example, we already know that if $$f(x) = x$$, then it follows ...

Preview Activity 2.1

(a) Use the limit definition of the derivative to find $f'(x)$ for $f(x)=x^2$

First, lets define f(x):

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

Second, lets do it numericly with x=5

In [3]:
x=5
for h in [10,1,1/10,1/100,1/1000,1/10000,1/100000]:
    answer=(f(x+h)-f(x))/h
    print("{:8.5f}: {}".format(h,answer))
10.00000: 20.0
 1.00000: 11.0
 0.10000: 10.09999999999998
 0.01000: 10.009999999999764
 0.00100: 10.001000000002591
 0.00010: 10.000099999984968
 0.00001: 10.000009999444615

Try changing values of x to see how it changes the result

Finally, lets do it symbolically.

In [15]:
import sympy
from IPython.display import display,Latex # Display latex from Python windows.
In [16]:
x = sympy.Symbol("x", real=True)
h = sympy.Symbol("h", real=True)
In [25]:
answer=(f(x+h)-f(x))/h
for h_limit in [10,1,1/10,1/100,1/1000,1/10000,1/100000]:
    print(answer.subs(h,h_limit))
-x**2/10 + (x + 10)**2/10
-x**2 + (x + 1)**2
-10.0*x**2 + 10.0*(x + 0.1)**2
-100.0*x**2 + 100.0*(x + 0.01)**2
-1000.0*x**2 + 1000.0*(x + 0.001)**2
-10000.0*x**2 + 10000.0*(x + 0.0001)**2
-100000.0*x**2 + 100000.0*(x + 1.0e-5)**2

That's a bit ugly, let's use sympy's simplify function.

In [26]:
answer=(f(x+h)-f(x))/h
for h_limit in [10,1,1/10,1/100,1/1000,1/10000,1/100000]:
    print(sympy.simplify(answer.subs(h,h_limit)))
2*x + 10
2*x + 1
2.0*x + 0.1
2.0*x + 0.01
2.0*x + 0.001
2.0*x + 0.0001
2.0*x + 1.0e-5

As the limit of h->0 the function x^2 -> 2*x

Lets check the answer with the sympy's diff function:

In [28]:
sympy.diff(x**2,x)
Out[28]:
2*x