Project 2 V4

MTH 280!
Project 2: Approximating the Derivative Version 4!
Spring 2015
Objectives: 1. Practice writing and using functions
2. Practice using loops and vectors
3. Practice using Boolean expressions and If-THEN-ELSE
4. Practice using MATPLOTLIB’s plot function.
Background: A standard method for approximating the derivative of a function is to use the central difference
quotient formula
f (x + h) − f (x − h)
f ′(x) ≈
2h
where h ≠ 0 is a small number. However, for a given function f and a given value of x, it is not clear what value
of h will give the best approximation. One method uses limits to determine a best approximation by calculating
as few of the central difference quotient as possible. The formula above is modified as shown below
f (x + h0 2 − k ) − f (x − h0 2 − k )
f ′(x) ≈ Dk =
for k = 0, 1,2,…
h0 21−k
where h = h0 2 − k ≠ 0 is a “small” value and 0 < h0 < 1 is a random value. The reason for using h0 is to try to
avoid computing f (x) where f is undefined. The sequence D0 , D1 , D2 ,… is calculated until either
(a) Dk − Dk−1 < Tol which means the requested tolerance is achieved, or
(b) Dk+1 − Dk > Dk − Dk−1 which means the differences are not getting smaller. or
(c) k + 1 = Max which means that the maximum number of Di have been calculated
Here, Tol is the given tolerance and Max is the given maximum number of elements of the sequence. The
approximation is then taken to be Dk where k is the smallest integer such that either (a), (b), or (c) is true.
Assignment: Write a Python function
Derivative(f, x, Tol = 10e-5, Max = 20)
that approximates the derivative of the Python function f at the point x.
This can be done by using either a for-loop or a while loop. Before entering your loop, compute Dold and Dnew
for k = 2 and k = 3 and Eold = Dnew − Dold . Inside the loop you should, do the following:
(i) Replace Dold ‘s value with Dnew ’s value,
(ii) Use the next value of k to compute Dnew ,
(iii) Use the values of Dold and Dnew to compute Enew ,
(iv) Use Enew , Eold and k to check if any of the conditions (a), (b), or (c) have been met.
If so, return Dold ; otherwise, replace Eold ‘s value with Enew ’s value and continue with another pass
through the loop.
!
—1—
MTH 280!
Project 2: Approximating the Derivative Version 4!
Spring 2015
Finally, to catch floating point errors, you should place your code inside a try-exception block within your
function:
try:
!
!
!
!
Your derivative code
!
!
!
except:
!
!
!
!
return nan
Thus, nan (Not A Number) will be returned whenever a numerical error such as division by zero occurs.
Testing: Test your Derivative on the following functions
1
(a) f (x) = cos(2π x) (b) f (x) = x 2
(c) f (x) =
(d) f (x) = x
(e) f (x) = x 2 sin(1 x)
x
by plotting both the approximation found by Derivative and the true derivative f ′(x) on the same axes
over the interval −1 ≤ x ≤ 1 . In the case of (c) and (e) , to avoid zero, your plots may need to be plotted over the
intervals −1 ≤ x ≤ −0.1 and 0.1 ≤ x ≤ 1 . Also print a table showing x and the absolute error
f ′(x) − Derivative( f , x) for x = −1.0, − 0.9,− 0.8, …,0.9,1.0 .
Note that Derivative can be called to compute the derivative of any Python numerical function f (x) by
entering
Derivative(f, x) .
where x is a number. Thus, to approximate the derivative of sin(π 4) , enter
Derivative(sin, pi/4).
To generate a vector of approximations where xx = arange(xMin, xMax, 0.011), enter
yy = [Derivative(sin, x) for x in xx]
Note: The increment value 0.011 is used to prevent xx from containing the value 0.0!
Within an IPython notebook, you can plot the approximation of derivative of a function by entering the
following lines
xx = arange(xMin, xMax, 0.011)
# A Numpy vector of x-values
yy = [Derivative(f, x) for x in xx]# Vector of f’ approximations
plot(xx, yy, ‘r’, linewidth = 5)
# Plot using a red line of width 5
You can plot a second function g(x) along with the derivative (over the same domain) by adding the lines
below to the same ipython cell containing the lines above.
yy2 = [g(x) for x in xx]
plot(xx, yy2, ‘b’, linewidth = 2) # Plot using a blue line of width 2
!
—2—