003 Numerical Differentiation and Integration
003 Numerical Differentiation and Integration
AbdelMeguid
[email protected]
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.
y
f xi
x
All Copy rights reserved
Dr.Hossam 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
Integration of Equations
◦ Romberg Integration
◦ Gauss Quadrature
◦ Improper Integrals
b b
I f x dx f n x dx
a a
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
0.5
f(x)
-0.5
-1
-2 -1 0 1 2 3 4 5
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
f n x a0 a1 x
a
All Copy rights reserved
Dr.Hossam 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
f a f b
I b a
height
2
height
width
12
where a b
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
Dr.Hossam 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
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
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).
>> 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
f n x a0 a1 x a2 x 2
x x1 x x2
x2
f x0 .......dx
x0 0
x x1 x0 x2
All Copy rights reserved
Dr.Hossam 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
Dr.Hossam 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
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
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
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
Dr.Hossam 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
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
>> 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
Definition of derivative
df ( x) f ( x x) f ( x)
lim
dx x 0 x
x x
f (x ) f (x)
x x
All Copy rights reserved
Dr.Hossam 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
-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
Dr.Hossam 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
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
Dr.Hossam 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
Dr.Hossam AbdelMeguid 18/09/2011 45
All Copy rights reserved
Dr.Hossam AbdelMeguid 18/09/2011 46
THE END