Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.1
Lecture 5
Numerical Techniques : Numerical
Differentiation and Integration
Variable Force in One Dimension, Simple Pendulum
IKC-MH.55 Scientific Computing with Python at November 10,
2023
Dr. Cem Özdo
˘
gan
Engineering Sciences Department
˙
Izmir Kâtip Çelebi University
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.2
Contents
1 Numerical Differentiation and Integration with a Computer
Variable force in one dimension
Differentiation with a Computer
Simple Pendulum
Numerical Integration - The Trapezoidal Rule
The Composite Trapezoidal Rule
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.3
Variable force in one dimension I
•
Consider the motion of a particle of mass m moving along
the x-axis under the action of a force F.
Write Newton’s second law
(F = ma) in terms of
velocity for a most general
force F(x,v,t)
dv
dt
=
F (x,v ,t)
m
dx
dt
= v (1)
•
In principle, the functions v = v(t) and x = x(t) can be
found by s olving Equation 1 for every F(x, v, t) function
(i.e., constant acceleration for F = constant, or harmonic
oscillating motion for F= -kx).
•
However, for more complex forces F(x, v , t) the analytical
solution may not always be available.
•
In this case, we will consider the numerical s olution.
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.4
Variable force in one dimension II
•
We want to find the solutions of the Equation 1 at the
equally spaced times t
1
, t
2
, . . . , t
N
and x
i
and v
i
.
•
Write the velocity and position derivatives in the form of
forward-difference derivative approximation. Take dt = h
as step length, then:
v
i+1
− v
i
h
=
F (x, v, t)
m
−→ v
i+1
= v
i
+
F
i
m
h
x
i+1
− x
i
h
= v
i
−→ x
i+1
= x
i
+ v
i
h
•
By given initial velocity v
1
, initial position x
1
at the time
t
1
= 0 and the values for F
i
= F (x
i
, v
i
, t
i
); the values for
v
i
, x
i
can be calculated for i = 2.3, . . . in a loop.
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.5
Numerical Differentiation and Integration I
•
When the function is explicitly known, we can emulate the
methods of calculus.
•
If we are working with experimental data that are displayed
in a table of [x , f(x )] pairs emulation of calculus is
impossible.
•
We must approximate the function behind the data in some
way.
•
Differentiation with a Computer:
•
Employs the interpolating polynomials to derive formulas for
getting de rivatives.
•
These can be applied to functions known explicitly as well
as those whose values are found in a table.
•
Numerical Integration- The Trapezoidal Rule:
•
Approximates, the inte grand fu nction with a linear
interpolating polynomial to derive a very simple but
important formula for numerically integrating functions
between given limits.
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.6
Numerical Differentiation and Integration II
•
We cannot often find the true answer numerically because
the analytical value is the limit of the sum of an infinite
number of terms.
•
We must be satisfied with approximations for both
derivatives and integrals but, for most applications, the
numerical answer is adeq uate.
•
The derivative of a function, f (x) at x = a, is defined as
df
dx
|
x=a
= lim
∆x→0
f (a + ∆x ) − f (a)
∆x
•
This is called a forward-difference approximation.
•
The limit could be approached from the opposite direction,
giving a backward-difference approximation.
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.7
Differentiation with a Computer I
•
Forward-difference approximation. A computer can
calculate an approximation to the derivative, if a very small
value is used for ∆x.
df
dx
x=a
=
f (a + ∆x ) − f (a)
∆x
•
Recalculating with small er and smaller values of x start ing
from an initial value.
•
What happens if the value is not small enough?
•
We should expect to find an optimal value for x.
•
Because round-off errors in the numerator will become
great as x approaches zero.
•
When we try this for
f (x) = e
x
sin(x)
at x = 1.9. The analytical answer is 4.1653826.
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.8
Differentiation with a Computer II
Apply Forward-difference approximation to f (x) = e
x
sin(x).
(Example py-file: myforwardderivative.py)
Table: Forward-difference approximation for f (x) = e
x
sin(x).
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.9
Differentiation with a Computer III
−10
0
Error
Ra io
Forward-difference: Deriva ive of Func ion
e
x
sin
(
x
)
0.0
0.2
Error
Varia ion
4.1
4.2
4.3
Approxima ed
Deriva ive
0 10 20 30 40
I era ion S eps
0.00
0.05
Δ
x
Figure: Forward-difference approximation for f (x) = e
x
sin(x).
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.10
Differentiation with a Computer IV
•
Starting with ∆x = 0.05 and halving ∆x each time. Table
gives the results.
•
We find that the errors of the approximation decr ease as
∆x is reduced until about ∆x = 0.05/2097152.
•
Notice that each successive er ror is about 1/2 of the
previous er ror as ∆x is halved until ∆x gets quite small, at
which time round off affects the ratio.
•
At values for ∆x smaller than 0.05/2097152, the error of
the approximation increases due to round off.
•
In effect, the best value for ∆x is when the effects of
round-of f and truncation errors are balanced.
•
If a backward-difference approximation is used; similar
results are obtained.
•
Backward-difference approximation.
df
dx
x=a
=
f (a) − f (a − ∆x)
∆x
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.11
Differentiation with a Computer V
Apply Backward-difference approximation to f (x) = e
x
sin(x).
(Example py-file: mybackwardderivative.py)
Table: Backward-difference appr oximation for f (x) = e
x
sin(x).
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.12
Differentiation with a Computer VI
0
50
Error
Ratio
Backward-difference: Derivative of Function
e
x
sin
(
x
)
−0.25
0.00
Error
Variation
3.75
4.00
4.25
A roximated
Derivative
0 10 20 30 40
Iteration Ste s
0.00
0.05
Δ
x
Figure: Backward-difference approximation for f (x) = e
x
sin(x).
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.13
Differentiation with a Computer VII
•
It is not by chance that the errors are about halved each
time for both forward- and backward-difference
approximations.
•
Look at this Taylor series where we have used h for ∆x:
f (x + h) = f (x) + f
′
(x) ∗ h + f
′′
(ξ) ∗ h
2
/2
•
Where the last term is the error term. The value of ξ is at
some point between x and x + h.
•
If we solve this equation for f
′
(x), we get
f
′
(x) =
f (x + h) − f (x)
h
− f
′′
(ξ) ∗
h
2
(2)
•
If we repeat this but begin with the Taylor series for
f (x − h), it turns out that
f
′
(x) =
f (x) − f (x − h)
h
+ f
′′
(ζ) ∗
h
2
(3)
•
Where ζ is between x and x − h.
•
The two error terms of Eqs. 2 and 3 are not identical
though both are O(h).
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.14
Differentiation with a Computer VIII
•
If we add Eqs. 2 and 3, then divide by 2, we get the
central-difference approximation to the derivative:
f
′
(x) =
f (x + h) − f (x − h)
2h
− f
′′′
(ξ) ∗
h
2
6
•
We had to extend the two Taylor series by an additional
ter m to get the error because the f
′′
(x) terms cancel.
•
This shows that using a central-difference approximation is
a much preferred way to estimate the derivative.
•
Even though we use the same number of computations of
the function at each step,
•
We approach the answer much mo re rapidly.
df
dx
x=a
=
f (x + h) − f (x − h)
2h
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.15
Differentiation with a Computer IX
Apply Central-difference approximation to f(x ) = e
x
sin(x).
(Example py-file: mycentralderivative.py)
Table: Central-difference approximation for f (x) = e
x
sin(x).
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.16
Differentiation with a Computer X
−2.5
0.0
2.5
Error
Ratio
Central-difference: Derivative of Function
e
x
sin
(
x
)
(0.005
0.000
Error
Variation
4.160
4.165
A roximated
Derivative
0 5 10 15 20 25 30
Iteration Ste s
0.00
0.05
Δ
x
Figure: Central-difference approximation for f (x) = e
x
sin(x).
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.17
Variable force in one dimension III
Apply Forward-difference approximation to Simple Harmonic
Motion. (Example py-file: shm.py)
0 1 2 3 4 5 6
Time
−1.0
−0.5
0.0
0.5
1.0
Position/Velocity
Time change of position and velocity
(t)
v(t)
Figure: Time change of position and velocity in motion under the
force F=-kx.
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.18
Simple Pendulum I
Figure: Simple pendulum.
•
The motion of a simple pendulum,
which consists of a point mass m
suspended on the end of a rope of
length L.
•
Newton’s second law for motion in
the tangential direc tion:
F = ma −→ −mgSinθ = m
d
2
s
dt
2
•
Here s is the arc length and θ is the
angle the rope makes with the
vertical (see F igure).
•
Since s = Lθ,
d
2
θ
dt
2
= −
g
L
sinθ
•
This differential equation has no analytical solution.
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.19
Simple Pendulum II
•
However, for small angle oscillations sinθ ≈ θ is
approximated:
d
2
θ
dt
2
= −
g
L
θ (4)
•
This equation has a solution in terms of sinusoidal
functions:
θ(t) = θ
0
cos
2πt
T
T = 2π
s
L
g
•
Here T is the period of the oscillation and θ
0
is the angular
amplitude.
•
This formula is small angle (θ
0
≤ 15
◦
) amplitudes gives
approximately good results.
•
We want to find the exact solution of the pendulum
problem for each amplitude θ
0
numerically.
•
For this purpose, we will transform the problem into a
numerical integral.
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.20
Simple Pendulum III
•
Let’s multiply both sides of the equation 4 by dθ/dt and
rearrange:
dθ
dt
d
2
θ
dt
2
= −
g
L
sinθ
dθ
dt
1
2
d
dt
dθ
dt
2
= −
g
L
d
dt
cosθ
•
If the indefinite integral is taken side by side,
1
2
dθ
dt
2
=
g
L
cosθ + C
•
The initial velocity and angle values ar e used to determine
the integration c onstant C.
•
If the pendulum is initially released from the maximum
angle, its velocity will be zero.
•
So, if d θ/dt = 0 and θ = θ
0
are substituted into the equation
at time t = 0, C = −(g/L)cosθ
0
is found.
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.21
Simple Pendulum IV
•
If the constant C is put in place and the arrangement is
made,
dθ
dt
=
q
2g
L
(cosθ − cosθ
0
)
dt =
q
L
2g
dθ
√
cosθ−cosθ
0
•
Integrating the right side of this expression from θ = 0 to
the angle θ = θ
0
, the left side will be one quarter of a
period.
T
4
=
s
L
2g
Z
θ
0
0
dθ
√
cosθ − cosθ
0
Thus, we have written the period formula as an integral.
•
For each given amplitude θ
0
we can calculate this integral
numerically.
•
However, since the value of the integrand diverges at the
upper bound (for θ = θ
0
), it is necessary to do a variable
replacement first.
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.22
Simple Pendulum V
•
For this purpose, first, the identities cosθ = 1 − 2sin
2
(θ/2)
and cosθ
0
= 1 − 2sin
2
(θ
0
/2) are inserted in the
denominator,
•
and then with the variable change k = sin(θ
0
/2) =
sin(θ/2)
sinφ
,
the integral becomes:
T = 4
s
L
g
Z
π/2
0
dφ
p
1 − sin
2
(θ
0
/2)sin
2
φ
•
This integral whose denominator is never zero is k nown as
an elliptical integral of the first kind.
•
There is no analytical solution.
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.23
Numerical Integration - The Trapezoidal Rule I
•
Given the function, f (x), the antiderivative is a function
F (x) such that F
′
(x) = f (x).
•
The definite integral
Z
b
a
f (x)dx = F(b) − F (a)
can be evaluated from the antider ivative.
•
Still, there are functions that do not have an antiderivative
expressible in terms of ordinary functions. Such as the
function: f (x) = e
x
/log(x)
1 from sympy i mport
*
2 x = symbols ( ’ x ’ )
3 f = exp ( x ) / log ( x )
4 df = i n t e g r a t e ( f , x) # Function d e r i v a t i v e i n symbolic form
5 p r i n t ( d f )
6 # I n t e g r a l ( exp ( x ) / log ( x ) , x )
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.24
Numerical Integration - The Trapezoidal Rule II
•
Is there any way that the definite integral can be found
when the antiderivative is unknown?
•
We can do it numerically by using the composite
trapezoidal rule
•
The definite integral is the area between the curve of f (x)
and the x-ax is.
•
That is the principle behind all numerical integration;
Figure: The trapezoidal rule.
•
We divide the distance from
x = a to x = b into vertical
strips and
add the areas of these strips.
•
The strips are often made
equal i n widths but that is not
always required.
•
Approximate the curve with a
sequence of straight l ines
.
•
In effect, we slope the top of
the strips t o match with the
curve as best we can.
•
The gives us the trapezoidal rule. Figure illustrates this.
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.25
The Trapezoidal Ru le II
•
It is clear that the area of the strip from x
i
to x
i+1
gives an
approximation to the area under the curve:
Z
x
i+1
x
i
f (x)dx ≈
f
i
+ f
i+1
2
(x
i+1
− x
i
)
•
We will usually wr ite h = (x
i+1
− x
i
) for the width of the
interval.
•
Error term for the trapezoidal integration is
Error = −(1/12)h
3
f
′′
(ξ) = O(h
3
)
•
What happens, if we are getting the integral of a known
function over a larger span of x-values, say, from x = a to
x = b?
•
We subdivide [a,b] into n smaller intervals with ∆x = h,
apply the rule to each subinterval, and add.
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.26
The Composite Trapezoidal Rule I
•
This gives the composite trapezoidal rule;
Z
b
a
≈
n−1
X
i=0
h
2
(f
i
+ f
i+1
) =
h
2
(f
0
+ 2f
1
+ 2f
2
+ . . . + 2f
n−1
+ f
n
)
•
The error is not the local error O(h
3
) but the global error,
the sum of n local errors;
Global error =
−(b − a)
12
h
2
f
′′
(ξ) = O(h
2
)
where nh = (b − a)
•
Use the trapezoidal r ule to estimate the integral from
x = 1.8 to x = 3.4 for f (x) = e
x
.
•
Applying the trapezoidal rule:
R
3.4
1.8
f (x)dx ≈
0.2
2
[f (1.8) + 2f (2.0) + 2f (2.2) + 2f (2.4)
+2f (2.6) + 2f (2.8) + 2f (3.0 ) + 2f (3.2)
+f (3.4)] = 23 .9944
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.27
The Composite Trapezoidal Rule II
Apply The trapezoidal rule to f (x ) = e
x
in interval of 1.8 and
3.4. (Example py-file: mytrapezoid.py)
0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0
X Value
0
10
20
30
40
50
Function Value
Area under the curve=23.914652
f(x)=
e
x
Figure: Integration for f (x) = e
x
by the trapezoidal rule.
Numerical Techniques:
Numerical
Differentiation and
Integration
Dr. Cem Özdo
˘
gan
LOGIK
Numerical
Differentiation and
Integration with a
Computer
Variable force in one
dimension
Differentiation with a
Computer
Simple Pendulum
Numerical Integration - The
Trapezoidal Rule
The Composite
Trapezoidal Rule
5.28
Simple Pendulum VI
Apply The trapezoidal rule to Simple Pendulum to find the
period. (Example py-file: simplependulum.py)
Table: Integration for
1.0
√
1.0−(sin(θ
0
/2)sin(φ))
2
by the trapezoidal rule.