Numerical Differentiation and Integration: Lecture Series On " Numerical Techniques and Programming in Matlab"
Numerical Differentiation and Integration: Lecture Series On " Numerical Techniques and Programming in Matlab"
MATLAB”
by
IIST Trivandrum
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 1
Numerical Differentiation
Basic problems
known )
data.
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 2
Solution Approaches ..
interpolating polynomial.
Applications
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 3
First Derivative..
f (c + h) − f (c)
f 0(c) = lim , ∀c ∈ (a, b).
h→0 h
at x = c.
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 4
Taylor Series
Derivative of a function at x = x0
h2 00 0
f (x0 + h) = f (x0) + hf (x0) + f (θ)
2
f (x0 + h) − f (x0)
f 0(x0) ≈
h
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 5
Error
h ”
|ED (f )| ≤ max |f (θ)|
θ∈[a,b] 2
Example
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 6
Other formulae
Backward Formula
f (x0) − f (x0 − h)
f 0(x0) ≈
h
Central Formula
f (x0 + h) − f (x0 − h)
f 0(x0) ≈
2h
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 7
Derivative for discrete data using interpolating
polynomial
x x0 x1 x2 ... xn
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 8
Using Newton’s forward difference formula
where
x = x0 + sh
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 9
For a given x,
Similarly,
2
00 d pn
f (x) '
dx2
d dpn
( )
dx dx
d dpn ds
= ( )
dx ds dx
1 d dpn
= ( )
h dx ds
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 10
1 d2 p n 1
= ( 2 )
h ds h
1 d2 p n
= 2 2
h ds
Thus in general,
k
(k) 1 d pn
f (x) = k k
h ds
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 11
Example 1
x 1 2 3 4
f (x) 2 5 7 10
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 12
Solution
x f (x) ∆ ∆2 ∆3
1 2
2 5 -1
2 2
3 7 1
4 10
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 13
Here h = 1 Using Taylor series
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 14
Using Newton forward divided difference formula
0 1 1
f (2) = 3 − − = 13/6
2 3
00 1 d2 p n 1 2 3
f (x) ≈ 2 2 = 2 ∆ f (x0) + (s − 1)∆ f (x0)
h ds h
f 00(2) = −1
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 15
Example 2
Calculate f (4)(0.15)
Solution:
2 3 2
1 s − s 2 s − 3s + 2s 3
p5(x) = f (x0) + s4 f (x0) + 4 f (x0) + 4 f (x0)+
2 6
d4f dp45 1 4 1 5
' = [4 f (x 0 ) + (5s − 10)4 f (x0)
dx4 dx4 h4 5
1 4
= 4 [4 f (x0) + (s − 2)45f (x0)]
h
1 2
= [−035 + (0.5 − 2)(0.4)] = −95.00 × 10
(0.1)4
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 17
x f (x) 41 f 42 f 43 f 44 f 45 f
0.1 0.425
0.050
-0.075 0.25
0.075 -0.05
0.050
0.6 0.575
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 18
Numerical Integration
Z x
F (x) = f (t)dt ∀x ∈ [a, b].
a
Z b
Observation: If f is nonnegative function, then f (x)dx is represent
a
the area under the curve f (x).
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 19
Antiderivative
an antiderivative of f .
Recall
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 20
Basic Problems
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 21
Newton-Cotes Methods/Formulae
Interpolation.
x x0 x1 x2 ... xn
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 22
The idea is:
Rb
Replace f by pn(x) and evaluate a
pn(x)dx
That is,
Z b Z b n
Z bX
f (x)dx ' pn(x)dx = li(x)f (xi)dx
a a a i=0
n
X Z b
= f (xi) li(x)dx
i=0 a
Xn
= Aif (xi)
i=0
Rb
Where Ai = l (x)dx
a i
called weights.
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 23
Types of Newton-Cotes Formulae
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 24
Trapezoidal Rule
polynomial P1(x).
Z b Z x1
f (x) ≈ P1(x)dx
a x0
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 25
Hence,
Z b Z 1
h
f (x)dx ≈ (f (x0) + s∆f (x0))hds = [f (x0) + f (x1)]
a 0 2
OR
b
b−a
Z
f (x)dx ≈ [f (a) + f (b)]
a 2
Error
T (b − a)3 00
E =− f (ξ),
12
where a < ξ < b
Remark: x0 = a and x1 = b.
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 26
1
Basic Simpson’s 3 Rule
Rb
To evaluate a
f (x)dx.
b
b−a
Z
b+a
f (x) dx = [f (a) + 4f ( ) + f (b).]
a 6 2
Error
s−h5f (4)(ξ)
E =
90
for some ξ ∈ (a, b).
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 27
3
Basic Simpson’s 8 Rule
Z b
3h
f (x)dx ' [f0 + 3f1 + 3f2 + f3]
a 8
−3h5 (4)
s
Error: E = f (ξ), where a < ξ < b.
80
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 28
Example
1
R2 4
R2
Using Trapezoidal and Simpson 3 rules find 0
x dx and 0
sinxdx and
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 29
Composite Rules
Note that if the integral [a, b] is large, then the error in the Trapezoidal
Idea
Error can be reduced by dividing the interval [a, b] into equal subinterval
b−a
h= , xi = x0 + ih
n
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 30
Composite Rule
Z b Z xn n Z
X xi
f (x)dx = f (x)dx = f (x)dx
a x0 i=1 xi−1
Z b
h
f (x)dx = [f (x0) + 2 ∗ (f (x1) + f (x2) · · · f (xn−1)) + f (xn)]
a 2
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 31
Error in composite Trapezoidal rule
CT h2 00
E = −(b − a) f (ξ), ξ ∈ [a, b]
12
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 32
1
The Composite Simpson’s 3 Rule
1, 2, 3, · · · , n.
b−a
Thus here h = 2n .
Then
Z b Z b=x2n
f (x)dx = f (x)dx
a a=x0
Z x2 Z x4 Z x2i Z x2n
= f (x)dx + f (x)dx + · · · + f (x)dx + · · · + f (x)dx
x0 x2 x2i−2 x2n−2
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 33
h h
= [f (x0) + 4f (x1) + f (x2)] + [f (x2) + 4f (x3) + f (x4)]+
3 3
h
+ · · · + [f (x2n−2) + 4f (x2n−1) + f (x2n)]
3
h
= {f (x0) + 4 × [f (x1) + f (x3) + f (x5) + · · · + f (x2n−1)]+
3
+2 × [f (x2) + f (x4) + f (x6) + · · · + f (x2n−2)] + f (x2n)}
CS h4 (4)
E = −(b − a) f (ξ),
180
where ξ ∈ [a, b]
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 34
Example
R1 1
Evaluate the integral −1
x2 exp(−x)dx by composite Simpson’s 3 rule
1
Solution: According to composite Simpson’s 3 rule:
Z 1
h
x2 exp(−x)dx = [f (x0) + 4f (x1) + 2f (x2) + 4f (x3) + 2f (x4)+
−1 3
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 35
f (x2) = f (−0.5) = 0.4122
f (x4) = f (0) = 0
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 36
Substituting these values in the above formula we get:
Z 1
x2 exp(−x)dx ' 0.87965
−1
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 37
Example
00
13 max0<ξ<1 |f (ξ)|
≤ .00001
12n2trap
For the composite Simpson 1/3 rule, we have
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 38
14 max0<ξ<1 |f (4)(ξ)|
≤ .00001
180n4simp
Now,
00
max |f (ξ)| ≤ 3.5, max |f (4)(ξ)| ≤ 95
0<ξ<1 0<ξ<1
(Please verify )
Hence
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 39
3
Composite Simpson’s 8 rule
b−a 3
• [a, b] is divided into 3n equal subintervals. (h = 3n . and we apply 8
Hence,
Z b Z x3 Z x6 Z x3n =b
f (x)dx ' f (x)dx + f (x)dx + · · · + f (x)dx
a x0 =a x3 x3n3
3h 3h
= [f0 + 3f1 + 3f2 + f3] + [f3 + 3f4 + 3f5 + f6]+
8 8
3h
+··· + [f3n−3 + 3f3n−2 + 3f3n−1 + f3n]
8
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 40
3h
= [f0 + 3f1 + 3f2 + 2f3 + 3f4 + 3f5 + 2f6 + 3f7 + · · · + 3f3n−1 + f3n]
8
Remember:
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 41
Example
3
Use composite simpson’s 8 rule, find the velocity after 18 seconds, if a
a= 40 60 70 75 80 83 85 87 88 88
f0 f1 f2 f3 f4 f5 f6 f7 f8 f9
3h
Sol: Velocity v = 8 [f0 +3f1 +3f2 +2f3 +3f4 +3f5 +2f6 +3f7 +3f8 +f9 ]=
3
4 [40+3×60+3×70+2×75+3×80+383+2×83+2×85+3×87+3×88+88]
= 1389 units.
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 42
Method of Undetermined Parameters
I(f ) ' A0f (x0) + A1f (x1) + A2f (x2) + · · · + Anf (xn)
Also, note that the weights Ai’s do not depend on the given function.
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 43
Therefore
I(f ) ' A0f (x0) + A1f (x1) + a2f (x2) + · · · + Anf (xn)
i.e., we want
Undetermined Parameters”.
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 44
Example
b2 −a2
Taking f (x) = x we get 2 = A0 a + A1 b
b−a
Solving the above two equations we get, A0 = A1 = 2 .
Z b
b−a
Thus, f (x)dx = [f (a) + f (b)] + α f 00(ξ)
a 2
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 45
Now if we take f (x) = x2, we get:
b3 − a3 b−a 2
=( )(a + b2) + 2! α
3 2
(b−a)3
=⇒ α = − 12
Thus
b
b−a (b − a)3 00
Z
f (x)dx = [f (a) + f (b)] − f (ξ)
a 2 12
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 46
Thus in the Method of Undetermined Parameters
Working Method:
the formula.
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 47
The Error term in the Simpson’s 38 -rule, using Method
of Undetermined Parameters
Start with:
Z x3
3h
f (x)dx = [f0 + 3f1 + 3f2 + f3] + α f (4)(ξ)
x0 8
x53 − x50 3h 4
= [x0 + 3x41 + 3x42 + x43] + α 4!
5 8
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 48
x53 − x50 3h 4
4!α = − [x0] + 3(x0 + h)4 + 3(x0 + 2h)4 + (x0 + 3h)4
5 8
9 5
4!α = − h
10
That is,
3 5
α=− h
80
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 49
Therefore the error in the Simpson’s rule is =
3 5 (4)
− h f (ξ)
80
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 50
Recall
I(f ) ' A0f (x0) + A1f (x1) + A2f (x2) + · · · + Anf (xn)
Also, note that the weights Ai’s do not depend on the given function.
Hence, if the error is of the form E(I) = Const × f (r+1)(η). Then the
Ans: Using Gaussian Quadrature rule one can improve the accuracy.
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 51
Example
Find x0, x1, A0, A1 and α so that the following rule is exact for all
polynomials of degree≤ 3.
Z 1
f (x)dx = A0f (x0) + A1f (x1) + αf (4)(ξ)
−1
(There are 4 unknowns and hence we have chosen the 4-th derivative
A0 + A1 = 2
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 52
A0 x 0 + A1 x 1 = 0
2
A0x20 + A1x21 = 3
A0x30 + A1x31 = 0
A0 = A1 = 1 x0 = − √13 and x1 = √1 .
3
R1
Thus the integration rule is: −1
f (x)dx = f (− √13 ) + f ( √13 ) + αf (4)(ξ).
2 2
= + α4!
5 9
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 53
1 8 1
=⇒ α = ( )=
4! 45 135
Z 1
1 1 1 (4)
f (x)dx = f (− √ ) + f ( √ ) + f (ξ).
−1 3 3 135
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 54
In general
I(f ) ' A0f (x0) + A1f (x1) + A2f (x2) + · · · + Anf (xn),
Z b
provides the exact value of f (x)dx for f (x) = 1, x, x2, · · · x2n+1.
a
Or What we want is that the quadrature rule is exact for all polynomials
of degree ≤ 2n + 1.
Lecture series on “Numerical techniques and MATLAB”, July 22-28, 2016 Dr. Sarvesh Kumar, IIST Trivandrum. 55