Sie sind auf Seite 1von 33

ENGINEERING MATHEMATICS 4

(BWM 30603 / BDA34003)


Lecture Module 6: Numerical Integration

Waluyo Adi Siswanto


Universiti Tun Hussein Onn Malaysia

This work is licensed under a Creative Commons Attribution 3.0 License.

Topics

Rectangular rule

Trapezoidal rule

Simpson's rule

Simpson 1/3

Simpson 3/8

Gauss Quadrature

2-point

3-point

Lecture Module 6

Engineering Mathematics IV

You want to calculate


the yellow area,
under the curve y(x)

y ( x)

Then you can calculate


the area

xn

Area= y ( x) dx
x1

x1
Lecture Module 6

xn
Engineering Mathematics IV

If the function is difficult


You will find it difficult to integrate.

y n1

Then you can predict


by approximation

y ( x)

y2

Area= Area 1+ Area 2+ Area 3

Area=h y 1 +h y 2 +h y n1

y1

Or you can write

Area=h( y 1 + y 2 + y n1 )
Of course not accurate.
If you use smaller division ?

x1
Lecture Module 6

xn
Engineering Mathematics IV

Rectangular rule
If you want to divide by N division

y
y ( x)

x n x 1
h=
N

y n1

Number of data n = N +1

y2
y1

xn

y ( x)dx = h( y 1+ y 2+ + y n1)
x1

x1
Lecture Module 6

xn

Engineering Mathematics IV

Trapezoidal rule
If you want to divide by N division

y
y ( x)

yn
yn1
y3
y2

h=

x n x 1
N

Now the area is not rectangular


but trapezoidal
xn

y1

y( x)dx = 2 ( y 1+ y 2)+ 2 ( y 2+ y 3)++ 2 ( y n1+ y n)


x1

xn

x1

Lecture Module 6

y ( x)dx = 2 ( y 1+2 y 2++2 y n1+ y n)

x1

xn

Engineering Mathematics IV

Trapezoidal m function (trapezoidal.m):

function I = trapezoidal(f_str, a, b, n)
%TRAPEZOIDAL Trapezoidal Rule integration.
% I = TRAPEZOIDAL(F_STR, A, B, N) returns the Trapezoidal Rule approximation
% for the integral of f(x) from x=A to x=B, using N divisions (subintervals), where
% F_STR is the string representation of f.
I=0;
g = inline(f_str);
h = (b-a)/n;
I = I + g(a);
for ii = (a+h):h:(b-h)
I = I + 2*g(ii);
end
I = I + g(b);
I = I*h/2;

Lecture Module 6

Engineering Mathematics IV

Simpson's rule
Simpson 1/3
xn

y( x)dx =
x1

h
( y +4 y 2 +2 y 3 +4 y 4 +2 y 5 ++4 y n1 + y n )
3 1

The number of division must be multiplication of 2


Simpson 3/8
xn

y( x)dx = 38h ( y 1+3( y 2+ y 3+ y5+ y6+)+2( y 4+ y 7+)+ y n)


x1

The number of division must be multiplication of 3

Lecture Module 6

Engineering Mathematics IV

Simpson 1/3 m function (simpson13.m):


function I = simpson13(f_str, a, b, n)
%SIMPRULE Simpson's rule integration.
% I = SIMPRULE(F_STR, A, B, N) returns the Simpson's rule approximation
% for the integral of f(x) from x=A to x=B, using N subintervals, where
% F_STR is the string representation of f.
% An error is generated if N is not a positive, even integer.
I=0;
g = inline(f_str);
h = (b-a)/n;
if((n > 0) && (rem(n,2) == 0))
I = I + g(a);
for ii = (a+h):2*h:(b-h)
I = I + 4*g(ii);
end
for kk = (a+2*h):2*h:(b-2*h)
I = I + 2*g(kk);
end
I = I + g(b);
I = I*h/3;
else
disp('Incorrect Value for N')
end

Lecture Module 6

Engineering Mathematics IV

Simpson 3/8 m function (simpson38.m):


function int=simpson38(f_str,x1,x2,n)
h=(x2-x1)/n;
x(1)=x1;
f=inline(f_str);
if((n > 0) && (rem(n,3) == 0))
sum=f(x1);
for i=2:n
x(i)=x(i-1)+h;
end
for j=2:3:n
sum=sum+3*f(x(j));
end
for k=3:3:n
sum=sum+3*f(x(k));
end
for l=4:3:n
sum=sum+2*f(x(l));
end
sum=sum+f(x2);
int=sum*3*h/8;
else
disp('Incorrect Value for N')
end

Lecture Module 6

Engineering Mathematics IV

10

Example 6-1
4

Find the approximate value of

x
dx
x+1
1

By using :
a) Exact integration, use SMath
b) Rectangular rule, 12 division
c) Trapezoidal rule, 12 division
d) Simpson 1/3, 12 division
e) Simpson 3/8, 12 division

Lecture Module 6

Engineering Mathematics IV

11

Exact integration in SMath

Rectangular rule

N=
h=
No data
1
2
3
4
5
6
7
8
9
10
11
12
13

x
1
1.25
1.5
1.75
2
2.25
2.5
2.75
3
3.25
3.5
3.75
4

12
0.25
x/sqrt(x+1)
0.70711
0.83333
0.94868
1.05529
1.15470
1.24808
1.33631
1.42009
1.50000
1.57648
1.64992
1.72062
15.15060

Lecture Module 6

Engineering Mathematics IV

Result=0.25(15.15060)=3.78765
12

Trapezoidal rule
N=
h=
No data
1
2
3
4
5
6
7
8
9
10
11
12
13

x
1
1.25
1.5
1.75
2
2.25
2.5
2.75
3
3.25
3.5
3.75
4

12
0.25
x/sqrt(x+1)
0.70711
0.83333
0.94868
1.05529
1.15470
1.24808
1.33631
1.42009
1.50000
1.57648
1.64992
1.72062
1.78885
2.49596
14.44350

Result=
Lecture Module 6

In FreeMat:

0.25
(2.49596+2(14.44350))=3.9229
2
Engineering Mathematics IV

13

Simpson 1/3 rule


N=
h=
No data
1
2
3
4
5
6
7
8
9
10
11
12
13

x
1
1.25
1.5
1.75
2
2.25
2.5
2.75
3
3.25
3.5
3.75
4

12
0.25
x/sqrt(x+1)
0.70711
0.83333
0.94868
1.05529
1.15470
1.24808
1.33631
1.42009
1.50000
1.57648
1.64992
1.72062
1.78885
2.49596

Result=
Lecture Module 6

In FreeMat :

7.85389

6.58961

0.25
(2.49596+4(7.85389)+2(6.48961))=3.9242
3
Engineering Mathematics IV

14

Simpson 3/8 rule


N=
h=
No data
1
2
3
4
5
6
7
8
9
10
11
12
13

x
1
1.25
1.5
1.75
2
2.25
2.5
2.75
3
3.25
3.5
3.75
4

12
0.25
x/sqrt(x+1)
0.70711
0.83333
0.94868
1.05529
1.15470
1.24808
1.33631
1.42009
1.50000
1.57648
1.64992
1.72062
1.78885
2.49596

Result=
Lecture Module 6

In FreeMat :

10.47542

3.96808

(3)0.25
(2.49596+3(10.47542)+2(3.96808))=3.9242
8
Engineering Mathematics IV

15

Example 6-2
A racing car velocity record from start to 12 seconds is shown below.
You have to calculate the distance of the car from its start position in 12 seconds.
The data is taken every 1 second.
Use Simpson's rule 1/3.
180

170

170

170

160
160

150

140

speed (km/h)

120

110

100

90

80

70

75

80

60
60

50

40
20
0
0
0

10

12

time (seconds)

Lecture Module 6

Engineering Mathematics IV

16

N=
h=

12
1/3600

no
1
2
3
4
5
6
7
8
9
10
11
12
13

t
0
1
2
3
4
5
6
7
8
9
10
11
12

v(t)
0
50
60
70
75
80
90
110
150
160
170
170
170
170

Result=
Lecture Module 6

640

545

1
(170+4(640)+2(545))=0.3537 km
33600
Engineering Mathematics IV

17

Gauss Quadrature
The main idea in Gauss Quadrature is to change the integration limits to
natural (dimensionless) coordinate limits from -1 to 1

y ( x)

x1

xn

()

1
Lecture Module 6

1
Engineering Mathematics IV

Change to
natural
coordinates

18

Gauss Quadrature
The main idea in Gauss Quadrature is to change the integration limits to
natural (dimensionless) coordinate limits from -1 to 1

y ( x)

x =

1
[(1) x 1+(1+) x n ]
2

()= y ( x )
x1

xn

x
1

()

x n x1
I=
() d

2 1
I=

1
Lecture Module 6

1
Engineering Mathematics IV

x n x1
I
2

19

I = () d
1

I = R1 (1 ) + R 2 ( 2 ) + + Rn ( n )
j is the location of the integration point j relative to the center

R j is the weighting factor for point j relative to the center,


and n is the number of points at which () is to be calculated

Lecture Module 6

Engineering Mathematics IV

20

Gauss Quadrature

Coefficients for Gaussian Quadrature


n

Rj

0.0

2.0

0.5773502692

1.0

0.7745966692

0.555555556

0.0

0.888888889

0.8611363116

0.3478548451

0.3399810436

0.6521451549

0.9061798459

0.2369268851

0.5384693101

0.4786286705

0.0

0.5688888889

4
5

Lecture Module 6

Engineering Mathematics IV

21

Program to generate abscissa and weight for any number of integration points
function [x,A] = GaussNodes(n,tol)
% USAGE: [x,A] = GaussNodes(n,tol)
% n = order of integration points
% tol = error tolerance (default is 1.0e4*eps).
format long;
if nargin < 2;
tol = 1.0e4*eps;
end
A = zeros(n,1);
x = zeros(n,1);
nRoots = fix(n + 1)/2;
for i = 1:nRoots
t = cos(pi*(i - 0.25)/(n + 0.5));
for j = i: 30
p0 = 1.0; p1 = t;
for k = 1:n-1
p = ((2*k + 1)*t*p1 - k*p0)/(k + 1);
p0 = p1;p1 = p;
end
dp = n *(p0 - t*p1)/(1 - t^2);
dt = -p/dp; t = t + dt;
if abs(dt) < tol
x(i) = t; x(n-i+1) = -t;
A(i) = 2/(1-(t^2))/(dp^2);
A(n-i+1) = A(i);
break
end
end
if nRoots == 1
x(i) =0;
end
end

Lecture Module 6

Engineering Mathematics IV

22

Program to use Gauss Quadrature, any number of integration points


function I = GaussQuadrature(func,a,b,n)
% USAGE: I = gaussQuad(func,a,b,n)
% func = handle of function to be integrated.
% for example --> func= @(x) ((sin(x)/x)^2)
% a,b = integration limits.
% n = order of integration points
% I = integral result
format long;
c1 = (b + a)/2;
c2 = (b - a)/2;
[x,A] = GaussNodes(n);
sum = 0;
for i = 1:length(x)
y = feval(func,c1 + c2*x(i));
sum = sum + A(i)*y;
end
I = c2*sum;

Lecture Module 6

Engineering Mathematics IV

23

Gauss Quadrature

1 point

()

0.0

1
2.0

weighting

I = R1 (1 )
I = 2.0 (0)

Lecture Module 6

Engineering Mathematics IV

24

Gauss Quadrature

2 points

0.0

-0.5773502692

0.5773502692

()

1
1.0

1.0

weighting

I = R1 (1 ) + R 2 ( 2 )
I = 1.0 (0.5773502692) + 1.0 (0.5773502692)

Lecture Module 6

Engineering Mathematics IV

25

Gauss Quadrature

3 points

0.0

-0.7745966692

0.7745966692

()

0.888888889
0.555555556
0.555555556

weighting

I = R1 (1 ) + R 2 ( 2 ) + R3 ( 3 )
I = 0.555555556 (0.7745966692) +
0.888888889 (0.0) +
0.555555556 (0.7745966692)

Lecture Module 6

Engineering Mathematics IV

26

Example 6-3
4

Find the approximate value of

x
dx
x+1
1

By using :
a) Gauss Quadrature 2 points
b) Gauss Quadrature 3 points

Lecture Module 6

Engineering Mathematics IV

27

Lecture Module 6

Engineering Mathematics IV

28

Lecture Module 6

Engineering Mathematics IV

29

Example 6-4
4

1+ x 2
Find the approximate value of
dx
3
1 x +1
By using :
a) Gauss Quadrature 4 points
b) Check your result in Smath (Exact integration)
c) Check your result, using Freemat (GaussQuadrature)

Lecture Module 6

Engineering Mathematics IV

30

Lecture Module 6

Engineering Mathematics IV

31

Lecture Module 6

Engineering Mathematics IV

32

Lecture Module 6

Engineering Mathematics IV

33

Das könnte Ihnen auch gefallen