Dr. Hossam S.S.
AbdelMeguid
hssaleh@[Link]
Mansoura University
2011-2012
Calculus is the mathematics of change.
Engineers must continuously deal with
systems and processes that change,
making calculus an essential tool of our
profession.
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 2
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 3
Definition of derivative : y f xi x f xi
rate of change of a x x
dependent variable with
respect to an independent dy f xi x f xi
variable lim x 0
dx x
f (x)
f xi x
y
f xi
x
All Copy rights reserved
[Link] AbdelMeguid x
18/09/2011 4
b
Definition:
I f x dx
it is the total value or a
summation of f(x)dx
over a range of x. In f (x)
fact the integration
symbol is actually a
stylized capital S
intended to signify the
connection between
integration and
summation.
x
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 5
Newton-Cotes Integration Formulas
◦ Trapezoidal rule
◦ Simpson’s Rules
◦ Unequal Segments
◦ Open Integration
Integration of Equations
◦ Romberg Integration
◦ Gauss Quadrature
◦ Improper Integrals
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 6
Common numerical integration scheme
Based on the strategy of replacing a complicated
function or tabulated data with some
approximating function that is easy to integrate
b b
I f x dx f n x dx
a a
f n x a1 x n a2 x n 1 an x an 1 fn(x) is an nth order
polynomial
All Copy rights reserved
18/09/2011 [Link] AbdelMeguid 7
The approximation of an integral by the area under
- a first order polynomial
- a second order polynomial
x/10 + sin(x) x/10 + sin(x)
1 1
0.5 0.5
f(x)
f(x)
0 0
-0.5 -0.5
-1 -1
-2 -1 0 1 2 3 4 5 -2 -1 0 1 2 3 4 5
x x
We can also approximated the integral by using a
series of polynomials applied piece
All Copy wise.
rights reserved
[Link] AbdelMeguid 18/09/2011 8
Newton-Cotes Integration
x/10 + sin(x)
0.5
f(x)
-0.5
-1
-2 -1 0 1 2 3 4 5
x
An approximation of an integral by the
area under straight line segments.
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 9
Closed form - data is at the beginning and end
of the limits of integration
Open form - integration limits extend beyond
the range of data.
x/10 + sin(x) x/10 + sin(x)
1 1
0.5 0.5
f(x)
f(x)
0 0
-0.5 -0.5
-1 -1
-2 -1 0 1 2 3 4 5 -2 -1 0 1 2 3 4 5
x x
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 10
First of the Newton-Cotes closed integration formulas
Corresponds to the case where the polynomial is a
first order b b
I f x dx f1 x dx
a a
f n x a0 a1 x
A straight line can be
represented as:
f(b)
f b f a
f1 x f a x a
ba f(a)
a
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011
b 11
Trapezoidal Rule
b b
I f x dx f1 x dx
a a f(b)
b
f b f a f(a)
f a x a dx
a
ba
Integrate this equation. Results a b
in the trapezoidal rule.
f a f b
I b a
height
2
height
width
Area base x average of the heights
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 12
1
Et f ' ' b a
3
12
where a b
This indicates that is the function being integrated is linear,
the trapezoidal rule will be exact.
Otherwise, for section with second and higher order
derivatives (that is with curvature) error can occur.
A reasonable estimate of x is the average value of b and a
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 13
Improve the accuracy by dividing the integration interval
into n of smaller segments
Apply the method to each segment
x1 x2 xn
I f ( x)dx f ( x)dx
x0 x1
xn1
f ( x)dx
f x0 f x1 f x1 f x2 f xn 1 f xn
I h h h
2 2 2
We can group terms to express a general form
n 1
f x0 2 f xi f xn
b a
3
I b a i 1
, Et 2
f ''
2n 12n
width
average height
weighted average All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 14
Evaluate the following integral using the trapezoidal rule and h = 0.1
1.6 exp(x 2)
I
2
e x dx 40
35
1
30
b a 1.6 1
h 0.1 n 6
f(x)=exp(x2)
25
n n 20
n 1 15
f x0 2 f xi f xn 10
I b a i 1 5
2n 0
0 0.5 1 1.5 2
x
x = 1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000
Exp(x2)= 2.7183 3.3535 4.2207 5.4195 7.0993 9.4877 12.9358
2.72 2 3.35 4.22 5.42 7.10 9.49 12.94
I 1.6 1 3.74
2 6
Exact = 3.7109
All Copy rights reserved
[Link] AbdelMeguid Error =0.8%
18/09/2011 15
function [ I ] = TrapezoidalRule( funhandle, a, b ,n )
%TrapezoidalRule a function evaluates the closed integral
%of the funhandlefrom a to b with number of segments n
x=a:(b-a)/n:b;
f=funhandle(x);
I=(b-a)*(f(1)+2*sum(f(2:end-1))+f(end))/(2*n);
end
>> f = inline('exp(x.^2)') >> TrapezoidalRule(f,1,1.6,600)
>> TrapezoidalRule(f,1,1.6,6) ans =
ans = 3.7109
3.7408 >> TrapezoidalRule(f,1,1.6,6000)
>> ans =
TrapezoidalRule(f,1,1.6,60) 3.7109
ans = >>
3.7112
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 16
trapz
Trapezoidal numerical integration
Syntax
Z = trapz(Y)
Z = trapz(X,Y)
Description
Z = trapz(Y) computes an approximation of the integral of Y via the trapezoidal method
(with unit spacing).
Z = trapz(X,Y) computes the integral of Y with respect to X using trapezoidal integration.
Inputs X and Y can be complex.
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 17
>> f = inline('exp(x.^2)')
f=
Inline function:
f(x) = exp(x.^2)
>> xx=1:(1.6-1)/6:1.6
xx =
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000
>> yy=f(xx)
yy =
2.7183 3.3535 4.2207 5.4195 7.0993 9.4877 12.9358
>> trapz(xx,yy)
ans =
3.7408
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 18
Corresponds to the case where the function is a
second order polynomial
b b
I f x dx f 2 x dx
a a
f n x a0 a1 x a2 x 2
Designate a and b as x0 and x2, and estimate
f2(x) as a second order Lagrange polynomial
b b
I f x dx f x dx
a a
2
x x1 x x2
x2
f x0 .......dx
x0 0
x x1 x0 x2
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 19
After integration and algebraic manipulation, we
get the following equations
h
I f x0 4 f x1 f x2
3
f x0 4 f x1 f x2
b a
6
width
average height
1
Et f b a
(4) 5
2880
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 20
x1 x2 xn
I
x0
f ( x) dx
x1
f ( x )dx
xn1
f ( x )dx
n 1 n2
f x0 4 f xi 2 f x f x
j n
I b a
i 1,3,5.. j 2,4,6..
3n
b a
5
4
Ea f
180n 4
Number of segments n must be even
The odd points represent the middle term for each
application. Hence carry the weight 4.
The even points are common to adjacent applications and
are counted twice.
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 21
Corresponds to the case where the function is a
third order polynomial
b b
I f x dx f 3 x dx
a a
f n x a0 a1 x a2 x a3 x
2 3
3h
I f x0 3 f x1 3 f x2 f x3
8
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 22
Evaluate the following integral using the Simpson’s 1/3 rule and h = 0.1
1.6 exp(x 2)
I
2
e x dx 40
35
1
30
b a 1.6 1
f(x)=exp(x2)
25
h 0.1 n 6 20
n n 15
n 1 n2
f x 2 f x f x
10
f x0 4 i j n 5
I b a
i 1,3,5.. j 2,4,6.. 0
0 0.5 1 1.5 2
3n x
x = 1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000
Exp(x2)= 2.7183 3.3535 4.2207 5.4195 7.0993 9.4877 12.9358
2.72 4 3.35 5.42 9.49 2 4.22 7.10 12.94
I 1.6 1 3.7112
3 6
Exact = 3.7109
All Copy rights reserved
[Link] AbdelMeguid
Error =0.008%23
18/09/2011
function [ I ] = Simpsons13Rule( funhandle, a, b ,n )
if(mod(n,2)~=0)
msgbox('Number of segments n must be even','error','error');
return;
end
x=a: (b-a)/n :b;
f=funhandle(x);
I=(b-a)*(f(1) + 4*sum(f(2:2:n)) + 2*sum(f(3:2:n)) + f(end))/(3*n);
end
>> f = inline('exp(x.^2)') >>Simpsons13Rule(f,1,1.6,10)
>> Simpsons13Rule(f,1,1.6,6) ans =
ans = 3.7109
3.7112
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 24
Experimental and field study data is often
unevenly spaced
In previous equations we grouped the term (i.e. hi)
which represented segment width.
n 1
f x0 2 f xi f xn
I b a i 1
2n
f x0 f x1 f x1 f x2 f xn 1 f xn
I h h h
2 2 2
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 25
We should also consider alternately using higher
order equations if we can find data in consecutively
even segments
trapezoidal
1/3 rule
rule
trapezoidal
rule
3/8
rule
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 26
Write a Matlab function to evaluate the
integration of unequal segments of the set of
data X and Y by trapezoidal rule. X and Y are
a vectors of n+1 elements. The first line of
the code is
function [I] = TrapzUnSeg(X, Y)
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 27
Integration of analytical as opposed to tabular
functions
Romberg Integration
◦ Richardson’s Extrapolation
◦ Romberg Integration Algorithm
Gauss Quadrature
Improper Integrals
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 28
quad
Numerically evaluate integral, adaptive Simpson quadrature
b
Syntax
q = quad (fun, a, b) q f x dx
a
dblquad
Numerically evaluate double integral over rectangle y xmax
max
f x, y dx dy
Syntax
q = dblquad (fun, xmin, xmax, ymin, ymax) q
ymin xmin
triplequad zmax ymax xmax
Numerically evaluate triple integral
Syntax
q f x, y, z dx dy dz
zmin ymin xmin
q = triplequad (fun, xmin, xmax, ymin, ymax, zmin, zmax)
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 29
>> fx = inline('exp(x.^2)');
>> quad(fx, 1,1.6)
ans =
3.7109
>> fxy = inline('exp(x.^2.*y.^2)');
>> dblquad(fxy, 1,1.6, 1,1.6)
ans =
14.4167
>> fxyz = inline('exp(x.^2.*y.^2.*z.^2)');
>> triplequad(fxyz, 1,1.6, 1,1.6, 1,1.6)
ans =
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 30
>> syms x y a b c
>> F=a*x*y^2-b*x^2*y-c F axy 2 bx 2 y c
F=
- b*x^2*y + a*x*y^2 - c
>> SF = int(F,x,y)
SF =
SF F dx dy
x*(c - x*((a*y^2)/2 - (b*x*y)/3)) - y*(c - y*((a*y^2)/2 -
(b*y^2)/3))
>> dF = diff(F,x,y)
d 2F
dF
dF = dx dy
2*a*x*y - b*x^2
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 31
Numerical Differentiation
◦ High accuracy formulas
◦ Richardson’s extrapolation
◦ Unequal spaced data
◦ Uncertain data
Applied problems
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 32
Background
◦ Often need to approximate derivatives to solve ODE
models
◦ Numerical differentiation: approximate derivatives of a
function using only functional values
◦ Can introduce large errors due to data noise and
numerical inaccuracies
Definition of derivative
df ( x) f ( x x) f ( x)
lim
dx x 0 x
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 33
Based on the Taylor Series
f '' xi 2
f xi 1 f xi f ' xi x x .........
2!
◦ Forward finite divided difference
◦ Backward finite divided difference
◦ Center finite divided difference
df ( xi ) f ( xi 1 ) f ( xi )
Forward
dx x
df ( xi ) f ( xi ) f ( xi 1 )
Backward
dx x
df ( xi ) f ( xi 1 ) f ( xi 1 )
Central rights reserved
All Copy
18/09/2011 [Link] AbdelMeguid2x 34
f (x) f (x)
true derivative Forward finite divided
difference approx.
x x
f (x ) f (x)
Backward finite divided Centered finite divided
difference approx. difference approx.
x x
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 35
You should be familiar with the following
Tables in your text
◦ Table 23.1: Forward Finite Difference Formulas
◦ Table 23.2: Backward finite difference formulas
◦ Table 23.2: Centered finite difference formulas
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 36
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 37
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 38
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 39
The rate of cooling of a body can be
expressed as dT
k T Ta
d
where T =temperature of the body (C), Ta =
temperature of the surrounding medium (C) , Time T (C)
and k =a proportionality constant (per min
minute). Thus, this equation specifies that the
rate of cooling is proportional to the 0 80
difference in the temperatures of the body
and of the surrounding medium. If a metal 5 44.5
ball heated to 80C is dropped into water that
is held constant at Ta = 20C , the temperature 10 30.0
of the ball changes, as
15 24.1
Utilize numerical differentiation to determine
dTldt at each value of time . Plot dTldt versus 20 21.7
T - Ta and employ linear regression to
evaluate k. 25 20.7
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 40
Time T (C) method dT/dt Matlab
(min)
0 80 2 point F (44.5-80)/5 -7.1 -10.16
5 44.5 2 point F (30.0-44.5)/5 -2.9 -4.55
10 30.0 2 point F (24.1-30.0)/5 -1.18 -1.74
15 24.1 2 point F (21.7-24.1)/5 -0.48 -0.7
20 21.7 2 point F (20.7-21.7)/5 -0.20 -0.40
25 20.7 2 point B (20.7-21.7)/5 -0.20 0.177
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 41
0
-1
-2
-3
dT/dt
-4
-5
-6
-7
-8
0 10 20 30 40 50 60
T-Ta
0
data 1
linear
dT/dt -2
K=0.12 -4
-6
y = - 0.12*x - 0.029
-8
0 10 20 30 40 50 60
All Copy rights reserved T-Ta
[Link] AbdelMeguid 18/09/2011 42
Polynomial evaluation- polyval
Syntax: y = polyval(p, x)
returns the value of a polynomial of degree n evaluated at x
Polynomial curve fitting - polyfit
Syntax : p = polyfit(x, y, n)
finds the coefficients of a polynomial p(x) of degree n that fits the
data, p(x(i)) to y(i), in a least squares sense.
Polynomial derivative - polyder
Syntax: k = polyder(p)
calculates the derivative of polynomial p.
Integrate polynomial analytically - polyint
Syntax: I = polyint(p)
returns a polynomial representing the integral of polynomial p
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 43
80 80
data
70 70 polyfit 4
60 60
T(C)
50 50
40 40
30
30
20
20 0 5 10 15 20 25
0 5 10 15 20 25
t(min)
2
by hand
0 by polyfit & polyder
linear
-2
-4
dT/dt
K=0.17 -6
y = - 0.17*x - 0.0065
-8
-10
-12
0 10 20 30 40 50 60
All Copy rights reserved
T-Ta
[Link] AbdelMeguid 18/09/2011 44
In addition to unequal spacing, the other problem
related to differentiating empirical data is
measurement error
Differentiation amplifies error
Integration tends to be more forgiving
Primary approach for determining derivatives of
imprecise data is to use least squares regression
to fit a smooth, differentiable function to the data
In absence of other information, a lower order
polynomial regression is a good first choice
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 45
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 46
THE END
hssaleh@[Link]
All Copy rights reserved
[Link] AbdelMeguid 18/09/2011 47