13 Differentiation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Differentiation

Math 315
Numerical Analysis
Differentiation and Integration I

Prof. Talea L. Mayo

Updated : October 22, 2024

1/13
Prof. Talea L. Mayo Math 315 Numerical Analysis
Differentiation

1 Differentiation

2/13
Prof. Talea L. Mayo Math 315 Numerical Analysis
Differentiation

Differentiation
Introduction

Why differentiate a function numerically?


derivative may be complicated to determine analytically
e.g. parametric wind field of tropical cyclone

2
2Rm Vm + f Rm fr
V (r) = 2 2
r−
Rm + r 2

function may not be explicitly known


e.g. may only have data describing the state of a system, but may
want to understand relevant derivatives

3/13
Prof. Talea L. Mayo Math 315 Numerical Analysis
Differentiation

Differentiation

formal definition of a derivative


f (x + h) − f (x)
f 0 (x) = lim
h→0 h

assumptions
f (x) differentiable
f (x) defined in interval centered at x

4/13
Prof. Talea L. Mayo Math 315 Numerical Analysis
Differentiation

Differentiation

f (x + h) − f (x) h
f 0 (x) − ≈ f 00 (ξ)
h 2

assumptions
assume f (x) has a second derivative at x
h sufficiently small
Taylor series:

h2 00 hk hk+1 (k+1)
f (x+h) = f (x)+hf 0 (x)+ f (x)+· · ·+ f (k) (x)+ f (ξ)
2 k! (k + 1)!

5/13
Prof. Talea L. Mayo Math 315 Numerical Analysis
Differentiation

Differentiation

f (a + h) − f (a)
f 0 (a) ≈
h

How do we choose h?
f (a + h) required, i.e. choose based on availability of data
generally want derivative across some interval, [α, β], β−α
h “steps”
required → consider desired efficiency
consider desired accuracy of approximation
“discretization error” or “truncation error” shrinks as h shrinks if
f is sufficiently smooth → make h as small as possible
limited by the effects of catastrophic cancelation in practice
roundoff effects tend to dominate for values of h much smaller

than εm

6/13
Prof. Talea L. Mayo Math 315 Numerical Analysis
Differentiation

Differentiation

Taylor Series
h2 00 hk (k) hk+1 (k+1)
f (x + h) = f (x) + hf 0 (x) + f (x) + · · · + f (x) + f (ξ)
2 k! (k + 1)!

forward difference
h2 00
f (a + h) = f (a) + hf 0 (a) + f (ξ)
2

f (a + h) − f (a) h
f 0 (a) = − f 00 (ξ)
h 2

backward difference
h2 00
f (a − h) = f (a) − hf 0 (a) + f (ξ)
2

f (a) − f (a − h) h
f 0 (a) = + f 00 (ξ)
h 2
7/13
Prof. Talea L. Mayo Math 315 Numerical Analysis
Differentiation

Differentiation

forward difference
f (a + h) − f (a)
f 0 (a) =
h
backward difference
f (a) − f (a − h)
f 0 (a) =
h

What about the error?


the two-point formulas are first order accurate, i.e. associated
truncation error is O(h)

8/13
Prof. Talea L. Mayo Math 315 Numerical Analysis
Differentiation

Differentiation

h2 00 hk (k) hk+1 (k+1)


f (x + h) = f (x) + hf 0 (x) + f (x) + · · · + f (x) + f (ξ)
2 k! (k + 1)!

centered difference
h2 00 h3 (3)
f (a + h) = f (a) + hf 0 (a) + f (a) + f (ξ1 )
2 3!
h2 00 h3 (3)
f (a − h) = f (a) − hf 0 (a) + f (a) − f (ξ2 )
2 3!
2h3 (3)
f (a + h) − f (a − h) = 2hf 0 (a) + f (ξ)
3!
f (a + h) − f (a − h) h2 (3)
f 0 (a) = − f (ξ)
2h 3!

9/13
Prof. Talea L. Mayo Math 315 Numerical Analysis
Differentiation

Differentiation

centered difference
f (a + h) − f (a − h)
f 0 (a) =
2h

one-sided difference (higher order)


−3f (a) + 4f (a + h) − f (a + 2h)
f 0 (a) =
2h

f (a − 2h) − 4f (a − h) + 3f (a)
f 0 (a) =
2h

What about the error?


the centered difference formula is second order accurate, i.e.
associated truncation error is O(h2 )

10/13
Prof. Talea L. Mayo Math 315 Numerical Analysis
Differentiation

Differentiation

centered fourth order difference

1
f 0 (a) = (f (a − 2h) − 8f (a − h) + 8f (a + h) − f (a + 2h))
12h

11/13
Prof. Talea L. Mayo Math 315 Numerical Analysis
Differentiation

Numerical Differentiation

example: f (x) = ex at x = 0
f 0 (x) = 1

eh −1 eh −e−h −e2h +8eh −8e−h +e−2h


h h
−1 2h
−1 12h
−1
0.1 5.17e-2 1.67e-3 3.33e-6
0.01 5.02e-3 1.67e-5 3.33e-10
0.001 5.0e-4 1.67e-7 -4.54e-14
0.0001 5.0e-5 1.67e-9 -2.60e-13
0.00001 5.0e-6 1.21e-11 -3.63e-12

12/13
Prof. Talea L. Mayo Math 315 Numerical Analysis
Differentiation

13/13
Prof. Talea L. Mayo Math 315 Numerical Analysis

You might also like