Differentiation
The Problem
- Given f:R→R, evaluate f′(x) for a given input x
- Similar to integration, finding the derivative analytically is expensive for some functions
เราอาจจะไม่ได้ต้องการว่า f’(x) เป็นอะไร แต่อยากรู้ว่า f’(2) คือเท่าไหร่ ประมาณนี้แหละ!
Goal
- Approximate f′(x) for a given input x numerically without computing the formula for f′
Analogy: Think of this like estimating the slope of a hill at a specific point by measuring how much you climb over a small distance, rather than using calculus formulas.
Finite Difference
Forward Difference
Let f:R→R. By Taylor series:
f(x+h)=f(x)+f′(x)h+2!f′′(x)h2+3!f′′′(x)h3+…(1)
Rearranging:
f′(x)h=f(x+h)−f(x)−2!f′′(x)h2−3!f′′′(x)h3−…
f′(x)=hf(x+h)−f(x)−2f′′(x)h+…
f′(x)=hf(x+h)−f(x)+O(h)
เป็น O(h) ก็เพราะว่าหลังจากก็จะเป็น Degree of h ไปเรื่อย ๆ
Forward difference formula:
f′(x)≈hf(x+h)−f(x)
- This has first-order accuracy (error is O(h))
h should be very small and finite number!
Backward Difference
By Taylor series:
f(x−h)=f(x)−f′(x)h+2!f′′(x)h2−3!f′′′(x)h3+…(2)
Rearranging:
f′(x)h=f(x)−f(x−h)+2!f′′(x)h2−3!f′′′(x)h3+…
f′(x)=hf(x)−f(x−h)+2f′′(x)h+…
f′(x)=hf(x)−f(x−h)+O(h)
Backward difference formula:
f′(x)≈hf(x)−f(x−h)
- Also first-order accurate (error is O(h))
Centered Difference
Subtract equation (2) from equation (1): จากด้านบนนะ
f(x+h)−f(x−h)=2f′(x)h+23!f′′′(x)h3+…
2f′(x)h=f(x+h)−f(x−h)−3f′′′(x)h3−…
f′(x)=2hf(x+h)−f(x−h)−6f′′′(x)h2+…
f′(x)=2hf(x+h)−f(x−h)+O(h2)
Centered difference formula:
f′(x)≈2hf(x+h)−f(x−h)
- Second-order accurate (error is O(h2))
Analogy: This is like measuring the slope by looking both forward and backward and taking the average - more balanced and accurate!
Centered Difference for Second Derivative
Add equations (1) and (2):
f(x+h)+f(x−h)=2f(x)+f′′(x)h2+24!f(4)(x)h4+…
f′′(x)h2=f(x+h)+f(x−h)−2f(x)−12f(4)(x)h4−…
f′′(x)=h2f(x+h)−2f(x)+f(x−h)−12f(4)(x)h2−…
f′′(x)=h2f(x+h)−2f(x)+f(x−h)+O(h2)
Centered difference for f′′:
f′′(x)≈h2f(x+h)−2f(x)+f(x−h)
Example 1
Problem: Approximate f′(2) where f(x)=2x4−3x3 with the three methods. Use h=0.1.
Solution:
Forward difference:
f′(2)≈0.1f(2+0.1)−f(2)=0.1(2(2.1)4−3(2.1)3)−(2(24)−3(23))≈31.1320
Backward difference:
f′(2)≈0.1f(2)−f(2−0.1)=0.1(2(24)−3(23))−(2(1.9)4−3(1.9)3)≈25.1280
Centered difference:
f′(2)≈2(0.1)f(2+0.1)−f(2−0.1)=0.2(2(2.1)4−3(2.1)3)−(2(1.9)4−3(1.9)3)≈28.1300
Note:
- f′(x)=8x3−9x2
- True value: f′(2)=8(23)−9(22)=28
- Centered difference is closest!
Second derivative approximation: Approximate f′′(1). Use h=0.1.
f′′(1)≈(0.1)2f(1.1)−2f(1)+f(0.9)
=.01(2(1.1)4−3(1.1)3)−2(2(1)4−3(1)3)+(2(.9)5−3(.9)3)≈6.0400
Note:
- f′′(x)=24x2−18x
- True value: f′′(1)=24−18=6
Exercise 2
- Approximate f′(−1) where f(x)=x−2x2 with the three methods. Use h=0.2
- Approximate f′′(−1). Use h=0.2
Solution
Calculate function values:
- f(−1+0.2)=f(−0.8)=(−0.8)−2(−0.8)2=−2.08
- f(−1)=(−1)−2(−1)2=−3
- f(−1−0.2)=f(−1.2)=(−1.2)−2(−1.2)2=−4.08
Forward difference:
f′(−1)≈0.2f(−0.8)−f(−1)=4.6
Backward difference:
f′(−1)≈0.2f(−1)−f(−1.2)=5.4
Centered difference:
f′(−1)≈0.4f(−0.8)−f(−1.2)=5
Centered difference for f′′:
f′′(−1)≈(0.2)2f(−0.8)−2f(−1)+f(−1.2)=−4
Example 3
Problem: Use Taylor series to derive a second-order accurate approximation to f′(x) in terms of the values of f(x), f(x−2h), and f(x+4h).
Given Taylor series:
f(x−2h)=f(x)−2f′(x)h+2f′′(x)h2−34f′′′(x)h3+O(h4)(3)
f(x+4h)=f(x)+4f′(x)h+8f′′(x)h2+332f′′′(x)h3+O(h4)(4)
Solution
To get a second-order accurate approximation, we need to eliminate the h2 term.
Multiply (3) by 4:
4f(x−2h)=4f(x)−8f′(x)h+8f′′(x)h2−316f′′′(x)h3+O(h4)(5)
Subtract (5) from (4):
f(x+4h)−4f(x−2h)=−3f(x)+12f′(x)h+16f′′′(x)h3+O(h4)
12f′(x)h=f(x+4h)−4f(x−2h)+3f(x)−16f′′′(x)h3+O(h4)
f′(x)=12hf(x+4h)−4f(x−2h)+3f(x)−34f′′′(x)h2+O(h3)
Final formula:
f′(x)≈12hf(x+4h)−4f(x−2h)+3f(x)
- This is second-order accurate as the error term is O(h2)
Key Idea
- The previous Taylor-series method is cumbersome for higher-order or higher-derivative methods
- Alternative approach: Interpolate f and find the derivative of the interpolant
Setup
- Let f:R→R be the function whose derivatives we want to find
- Let ti, i=1,…,n be equally spaced points in R, with step size h=ti+1−ti
- Let yi=f(ti)
Two-Point Interpolation (Forward Difference)
Let's interpolate two points (ti,yi) and (ti+1,yi+1).
Lagrange interpolant:
p(t)=yiti−ti+1t−ti+1+yi+1ti+1−tit−ti
=yi−ht−ti+1+yi+1ht−ti
=yi+1ht−ti−yiht−ti+1
Take the derivative:
p′(t)=hyi+1−hyi=hyi+1−yi=hf(ti+1)−f(ti)=hf(ti+h)−f(ti)
This gives the forward difference formula for f′.
Two-Point Interpolation (Backward Difference)
Now interpolate (ti−1,yi−1) and (ti,yi).
Lagrange interpolant:
p(t)=yi−1ti−1−tit−ti+yiti−ti−1t−ti−1
=yi−1−ht−ti+yiht−ti−1
=yiht−ti−1−yi−1ht−ti
Take the derivative:
p′(t)=hyi−hyi−1=hyi−yi−1=hf(ti)−f(ti−1)=hf(ti)−f(ti−h)
This gives the backward difference formula for f′.
Three-Point Interpolation (Centered Difference)
Interpolate three data points: (ti−1,yi−1), (ti,yi), (ti+1,yi+1).
Lagrange interpolant:
p(t)=yi−1(ti−1−ti)(ti−1−ti+1)(t−ti)(t−ti+1)+yi(ti−ti−1)(ti−ti+1)(t−ti−1)(t−ti+1)
+yi+1(ti+1−ti−1)(ti+1−ti)(t−ti−1)(t−ti)
Substituting h=ti+1−ti:
p(t)=yi−1(−h)(−2h)(t−ti)(t−ti+1)+yih(−h)(t−ti−1)(t−ti+1)
+yi+12h(h)(t−ti−1)(t−ti)
=yi−12h2(t−ti)(t−ti+1)−yih2(t−ti−1)(t−ti+1)+yi+12h2(t−ti−1)(t−ti)
Using the product rule (dtd(uv)=dtduv+udtdv):
p′(t)=yi−12h2(t−ti)+(t−ti+1)−yih2(t−ti−1)+(t−ti+1)
+yi+12h2(t−ti−1)+(t−ti)(6)
Evaluate at t=ti:
p′(ti)=yi−12h2(ti−ti)+(ti−ti+1)−yih2(ti−ti−1)+(ti−ti+1)
+yi+12h2(ti−ti−1)+(ti−ti)
=yi−12h2−h−yih2h−h+yi+12h2h
=−2hyi−1+2hyi+1=2hyi+1−yi−1
=2hf(ti+1)−f(ti−1)=2hf(ti+h)−f(ti−h)
This gives the centered difference formula for f′.
Second Derivative
Take the derivative of equation (6) with respect to t:
p′′(t)=yi−12h22−yih22+yi+12h22
=h2yi+1−2yi+yi−1
=h2f(ti+1)−2f(ti)+f(ti−1)
=h2f(ti+h)−2f(ti)+f(ti−h)
This gives the centered difference formula for f′′.
General Approach for Higher-Order Methods
To derive higher-order methods or methods for higher derivatives:
- Add more data points, e.g., (ti−2,yi−2), (ti+2,yi+2), (ti−3,yi−3), (ti+3,yi+3), etc.
- Find the interpolant
- Take the derivative of the interpolant
Analogy: It's like fitting a smoother curve through more points to get a better estimate of the slope.
Step Size Considerations
Key Points
- In many problems, including numerical integration or differentiation, smaller step sizes give more accurate results
- For quadrature: think of the widths of the subintervals for composite rule as step sizes
- But step sizes cannot be too small
Issues with Very Small Step Sizes
If step size is too small:
- Too expensive (e.g., too many function evaluations for quadrature)
- Too high error (e.g., catastrophic cancellation for finite difference)
Analogy: Using a microscope with too much magnification - you see so much detail that you lose perspective, and tiny measurement errors become huge problems.
Another idea to improve accuracy for finite difference.
Setup
-
Let F(h) denote the value obtained from a given method with step size h
- Example: the value obtained from a finite difference method using step size h
-
Suppose:
F(h)=a0+a1hp+O(hr)
as h→0 for some p and r, with r>p
-
We know the values of p (and r), but not a0 or a1
-
a0 is the true value we want to find (the true derivative, for instance)
-
p is the order of the formula F
มันก็เหมือน Forward Difference นั่นแหละ ที่ p=1,r=2
Derivation
Suppose we compute F for two step sizes: h and h/q for some positive integer q.
We have:
F(h)=a0+a1hp+O(hr)(7)
and
F(h/q)=a0+a1(qh)p+O(hr)
F(h/q)=a0+a1q−php+O(hr)(8)
This is a system of two linear equations in two unknowns a0 and a1.
Solving for a0:
Multiply (7) by q−p:
q−pF(h)=q−pa0+a1q−php+O(hr)
Subtract (8):
q−pF(h)−F(h/q)=(q−p−1)a0+O(hr)
(q−p−1)a0=q−pF(h)−F(h/q)−O(hr)
(q−p−1)a0=q−pF(h)−F(h/q)+O(hr)
a0=q−p−1q−pF(h)−F(h/q)+O(hr)
Rearranging:
a0=q−p−1q−pF(h)−F(h)+F(h)−F(h/q)+O(hr)
a0=q−p−1(q−p−1)F(h)+F(h)−F(h/q)+O(hr)
a0=F(h)+q−p−1F(h)−F(h/q)+O(hr)
a0=F(h)+q−p−1F(h)−F(h/q)
Key observations:
- The above has error O(hr), which is smaller than the errors of F(h) and F(h/q)
- If F(h) is known for several values of h, can repeat the extrapolation process to get even more accurate approximations (Not going through that detail na)
Analogy: It's like taking two rough measurements and using the pattern in their errors to calculate a much more accurate estimate.
Example 4
Problem: Use Richardson extrapolation to improve the accuracy of the first-order forward difference formula to approximate the derivative of sin(x) at x=1. Use h=0.5 and h/2=0.25.
Solution
Note: for forward difference, p=1 and r=2.
F(h)=F(0.5)=0.5sin(1.5)−sin(1)=0.312048
F(h/2)=F(0.25)=0.25sin(1.25)−sin(1)=0.430055
The extrapolated value is:
a0=F(h)+q−p−1F(h)−F(h/q)
=0.312048+2−1−10.312048−0.430055
=0.312048+(1/2)−10.312048−0.430055
=0.548061
For comparison:
- The correctly rounded result: cos(1)=0.540302
- Much closer than either original approximation!
Automatic Differentiation (AD) Preview
จะสอนแบบไม่ได้เจาะลึกมากเพราะยาก
What is AD?
- A tool/software that takes a program code that computes f and outputs a program code that computes f′ (and f)
- The output code computes the true derivatives (subject to roundoff error)
- In contrast, finite difference formulas have truncation error too
- Works for any code regardless of how complicated f is
- (Although the output derivatives code may be very slow or very expensive spacewise)
Analogy: It's like having a robot that can watch you bake a cake and automatically write down the recipe for making the cake rise twice as fast.
The Idea Behind AD
Core concept:
- Any computer program is just a sequence of basic arithmetic operations (addition, subtraction, multiplication, and division), whose derivatives are easy to compute
- Use chain rules (properly) on each statement of the code for f to get the code for f′
Two modes:
- Forward mode
- Reverse mode
- They differ in how the chain rule is used
How Forward-mode AD Works
Original code:
function y = fun(x)
u = 2*x+3;
v = u*(x+2);
y = u+v;
end
Forward mode approach:
- For each statement w=…, add a statement to compute dw/dx immediately after it
- For simplicity, let z˙ denote dz/dx
- Note: z˙ is a new variable in the program
AD-generated code (forward mode):
function [y, ẏ] = fun(x)
u = 2*x+3;
u̇ = 2;
v = u*(x+2);
v̇ = u̇*(x+2) + u;
y = u+v;
ẏ = u̇+v̇;
end
Analogy: Forward mode is like watching a domino chain fall and calculating how fast each domino hits the next one as you go forward.
Reverse-mode AD [Optional]
Approach:
- Run the original code once first
- From bottom to top, for each statement w=g(t1,…,tn), add a statement that computes ∂w∂ydtidg and adds it to dy/dti for all ti
Analogy: Reverse mode is like tracing backward from the final result to see how much each earlier step contributed to it - like forensic analysis of a chain reaction.
Forward Mode vs Reverse Mode
When to use each:
- Suppose f:Rn→Rm
- Forward mode is faster when m is much larger than n
- Reverse mode is faster when n is much larger than m
- Reverse mode takes a lot more space (except for some special cases)
Practical note:
- For machine learning (many inputs, one output loss function): reverse mode (backpropagation) is typically used
- For simulations (few inputs, many outputs): forward mode is typically better
Summary
First Derivative:
- Forward difference (1st order): f′(x)≈hf(x+h)−f(x)
- Backward difference (1st order): f′(x)≈hf(x)−f(x−h)
- Centered difference (2nd order): f′(x)≈2hf(x+h)−f(x−h)
Second Derivative:
- Centered difference (2nd order): f′′(x)≈h2f(x+h)−2f(x)+f(x−h)
Richardson Extrapolation:
a0=F(h)+q−p−1F(h)−F(h/q)
Key Concepts
- Higher order = more accurate (but requires more function evaluations)
- Centered difference is generally more accurate than forward/backward
- Step size trade-off: smaller is more accurate, but too small causes numerical issues
- Richardson extrapolation can boost accuracy without reducing step size
- Automatic differentiation computes exact derivatives by applying calculus rules to code