Numerical Methods for Civil Engineers Lecture 9 Numerical Integration

14 downloads 250197 Views 116KB Size Report
SCHOOL OF CIVIL ENGINEERING. Lecture 9. Numerical Integration. BASIC IDEAS .... Note that the exact value of the integral can be determined analytically to ...
Numerical Methods for Civil Engineers Lecture 9

Numerical Integration - Basic Ideas - Symbolic vs. vs. Numerical Integration - Trapezoid Rule - Simpson’ Simpson’s Rule - MATLAB quad and quad8 quad8 Functions Mongkol JIRAVACHARADET

SURAN AR EE

INSTITUTE OF ENGINEERING

UNIVERSITY OF TECHNOLOGY

SCHOOL OF CIVIL ENGINEERING

BASIC IDEAS b

I = ∫ f ( x ) dx = Area under curve f ( x ) a

Node f (x) Trapezoidal region

x a

b

Approximated by piecewise-linear: Area = Sum of trapezoidal regions

Symbolic Math Toolbox Symbolic Math Toolboxes incorporate symbolic computation into the numeric environment of MATLAB. Symbolic Integration with MATLAB

>> >> >> >>

I I I I

= = = =

int(f) % Indefinite integral int(f, v) % Designating integration variable int(f, a, b) % Definite integral on close interval int(f, v, a, b)

where f = symbolic expression Variables must be define as symbolic by sym or syms

>> x = sym(‘x’), y = sym(‘y’), z = sym(‘z’) or >> syms x y z

b

Example : I = ∫ ( x 3 − c ) dx a

>> syms x a b c >> I = int(x^3-c,x,a,b)

I =

1/4*b^4-c*b-1/4*a^4+c*a

What is Integration? The integral is equivalent to the area under the curve. f(x) b

I = ∫ f ( x ) dx

I

a

x a

b

Examples of how integration is used to evaluate areas Net force due to wind blowing against the building

Cross-sectional area of a river

Newton-Cotes Formulas Replace a complicated function with a polynomial that is easy to integrate b

b

a

a

I = ∫ f ( x ) dx ≅ ∫ fn ( x ) dx where fn ( x ) = a0 + a1x + L + an x n −1 + an +1x n f(x)

f(x)

x a

b

Approximate area by a straight line

x a

b

Approximate area by a parabola

Trapezoidal Rule 1st degree polynomial = Linear line

f(x) f(b)

f1( x ) = f (a) +

f1(x)

f (b ) − f (a ) ( x − a) b−a

b

I =

f(a)

∫ f1( x ) dx = (b − a ) a

a

f (a ) + f ( b ) 2

x

b

Area of trapezoid = width x average height width = b – a = h

f (a ) + f ( b ) average height = 2

Composite Trapezoidal Rule Improve accuracy by dividing interval into subintervals

f(x) f2

f3

f4

f5

x4

x5

f1

x1

x2

x3

x5

x2

x3

x4

x1

x1

x2

x3

∫ f ( x ) dx = ∫ f ( x ) dx + ∫ f ( x ) dx + ∫ f ( x ) dx

+

x5

∫ f ( x ) dx

x4

f(x) There are n segment with equal width: h =

b–a n

x2

x3

xn

x1

x2

x n −1

I = ∫ f ( x ) dx + ∫ f ( x ) dx + L +

∫ f ( x ) dx

f ( x1 ) + f ( x 2 ) f ( x2 ) + f ( x 3 ) +h +L 2 2 f ( x n−1 ) + f ( x n ) +h 2

I =h x1 x1 = a

x2

x3

x4

x5

x6 xn = b

b–a h= n

f +f f +f f +f I = h 1 2 + h 2 3 + L + h n −1 n 2 2 2

n −1  h  I = f1 + 2 ∑ fi + fn   2  i =2 

f(x) f4

MEAN

f5

f3

f2

f6

f1 x1

Substitute h =

x2

b–a n

x3

x4

x5

x6

n −1   f + 2 f + f ∑ i n 1 i =2   I = (b − a ) 2n

width

average height

Example 9-1 Use Trapezoidal rule to numerically integrate f ( x ) = 0.2 + 25 x − 200 x 2 + 675 x 3 − 900 x 4 + 400 x 5 from a = 0 to b = 0.8. Note that the exact value of the integral can be determined analytically to be 1.640533. or using MATLAB’s Symbolic Math Toolbox : >> x = sym(‘x’) >> I=int(0.2+25*x-200*x^2+675*x^3-900*x^4+400*x^5,x,0,0.8) I = 3076/1875 = 1.6405 Single Trapeziod: f(0) = 0.2 and f(0.8) = 0.232

I = (b − a)

f (a ) + f (b ) 2

= (0.8 − 0)

εt =

f(x)

0.2 + 0.232 = 0.1728 2

Error

1.6405 − 0.1728 × 100% = 89 .5% 1.6405

Integral estimate 0

2 Trapeziods: n = 2 (h = 0.4) : f(0) = 0.2

f(0.4) = 2.456

f(0.8) = 0.232

n −1   f1 + 2 ∑ fi + fn  i =2  I = (b − a)  2n

I = (0.8 )

εt =

0.2 + 2(2.456 ) + 0.232 = 1.0688 4

1.6405 − 1.0688 × 100 % = 34.9% 1.6405

0.8

MATLAB’s trapz function Trapezoidal numerical integration >> z = trapz(y) >> z = trapz(x, y)

% Integral of y with unit spacing % Integral of y with respect to x

90o

∫ sin x dx

Example:

0o

>> angle = 0:15:90; >> x = (pi*angle/180); >> y = sin(x); >> z = trapz(x,y) >> z = 0.9943

Example 9-2 Use MATLAB’s trapz function to numerically integrate f ( x ) = 0.2 + 25 x − 200 x 2 + 675 x 3 − 900 x 4 + 400 x 5 from a = 0 to b = 0.8. Note that the exact value of the integral can be determined analytically to be 1.640533. For n = 2 :

>> x = linspace(0,0.8,3); >> fx = 0.2+25*x-200*x.^2+675*x.^3-900*x.^4+400*x.^5; >> I = trapz(x,fx) I = 1.0688 n

I

εt

2 3 4 5 6

1.0688 1.3639 1.4848 1.5399 1.5703

34.9 16.5 9.49 6.13 4.28

Simpson’s Rules Using higher-order polynomials to connect the points f(x)

f(x)

x Simpson’s 1/3 rule Quadratic connecting 3 points

x Simpson’s 3/8 rule Cubic connecting 4 points

Simpson’s 1/3 Rules f(x)

Quadratic connecting 3 points f(x1)

f2(x) f(x2)

f(x0)

x1

x0

I≅

x2

I=

x2

x2

∫ f ( x)dx ≅ ∫ f

x0

2

( x)dx

x0

f 2 ( x) =

( x − x1 )( x − x2 ) f ( x0 ) ( x0 − x1 )( x0 − x2 )

+

( x − x0 )( x − x2 ) f ( x1 ) ( x1 − x0 )( x1 − x2 )

+

( x − x0 )( x − x1 ) f ( x2 ) ( x2 − x0 )( x2 − x1 )

h x −x [ f ( x0 ) + 4 f ( x1 ) + f ( x2 )] , h = 2 0 3 2

Example 9-3 Use Simpson’s 1/3 rule to numerically integrate f ( x ) = 0.2 + 25 x − 200 x 2 + 675 x 3 − 900 x 4 + 400 x 5 from a = 0 to b = 0.8. Note that the exact value of the integral can be determined analytically to be 1.640533. Solution: n = 2 (h = 0.4) : f(0) = 0.2

f(0.4) = 2.456

f(0.8) = 0.232

I =

0.4 (0.2 + 4( 2.456) + 0.232) = 1.3675 3

εt =

1.6405 − 1.3675 × 100 % = 16.6% 1.6405

Composite Simpson’s 1/3 Rules x2

x4

xn

x0

x2

xn − 2

I = ∫ f ( x ) dx + ∫ f ( x ) dx + L + I=

∫ f ( x ) dx

where h =

b−a , n = even number n

h [f0 + 4f1 + f2 ] + h [f2 + 4f3 + f4 ] + L + h [fn−2 + 4fn −1 + fn ] 3 3 3 1 1

1

4

1

4

2

4

1 1

4

1 4

1

1 1

4

2

4

1

2 4

a

4

2

4

1

b

n −1 n −2  (b − a)  I= f ( x ) + 4 f ( x ) + 2 f ( x ) + f ( x )  0 ∑ i j =∑ j n  3n  i =1,3,5 2, 4, 6 

Example 9-4 Use composite Simpson’s 1/3 rule with n = 4 to numerically integrate f ( x ) = 0.2 + 25 x − 200 x 2 + 675 x 3 − 900 x 4 + 400 x 5 from a = 0 to b = 0.8. Note that the exact value of the integral can be determined analytically to be 1.640533. Solution: n = 4 (h = 0.2) : f(0) = 0.2

f(0.2) = 1.288

f(0.4) = 2.456

f(0.6) = 3.464

f(0.8) = 0.232

n −1 n−2  (b − a )  I= f ( x ) + 4 f ( x ) + 2 f ( x ) + f ( x )  0 ∑ i j =∑ j n  3n  i =1,3,5 2, 4,6 

I =

εt =

0.8 ( 0.2 + 4(1.288 + 3.464) + 2( 2.456) + 0.232) = 1.624 3( 4)

1.6405 − 1.624 × 100 % = 1.04% 1.6405

Simpson’s 3/8 Rules Cubic connecting 4 points

b

b

a

a

I = ∫ f ( x)dx ≅ ∫ f 2 ( x)dx f(x)

3h [ f ( x0 ) + 3 f ( x1 ) + 3 f ( x2 ) + f ( x3 )] 8 h = ( x3 − x0 ) / 3 I≅

x0 a or…

x1 I =

x2

x3 b

x

(b − a) [f ( x0 ) + 3 f ( x1) + 3 f ( x2 ) + f ( x3 )] 8

Truncation Errors Interval width

Et

Trapezoid

1 3 h f ′′(ξ ) 12

h =b−a

(b − a )3 f ′′(ξ ) 12

Simpson’s 1/3

1 5 (4) h f (ξ ) 90

b−a h= 2

(b − a )5 (4) f (ξ ) 2880

Simpson’s 3/8

3 5 (4) h f (ξ ) 80

b−a h= 3

(b − a )5 (4) f (ξ ) 6480

Method

Et

Simpson’s 3/8 rule is used when number of segments is odd.

Apply Simpson’s 1/3 and 3/8 Rules To handle multiple application with odd number of intervals f(x)

x 1/3 rule

3/8 rule

Example 9-5 Use Simpson’s 1/3 + 3/8 rule with n = 5 to numerically integrate f ( x ) = 0.2 + 25 x − 200 x 2 + 675 x 3 − 900 x 4 + 400 x 5 from a = 0 to b = 0.8. Note that the exact value of the integral can be determined analytically to be 1.640533. Solution: n = 5 (h = 0.16) : f(0) = 0.2 f(0.32) = 1.743 f(0.64) = 3.182

f(x)

f(0.16) = 1.297 f(0.48) = 3.186 f(0.8) = 0.232

For the first two segments use Simpson’s 1/3 : I =

0.16 (0.2 + 4(1.297) + 1.743) = 0.380 3

For the last three segments use Simpson’s 3/8 : I =

0.48 (1.743 + 3(3.186 + 3.182) + 0.232) 8

x 0

1/3 rule

= 1.265 Total integral : I = 0.380 + 1.265 = 1.645

0.16 0.32 0.48 0.64

εt =

0.8

3/8 rule

1.6405 − 1.645 × 100 % = 0.274 % 1.6405

MATLAB’s quad and quad8 Functions quad : low order method, quad8: high order method >> q = quad(’f',a,b) Approximates the integral of f(x) from a to b within a relative error of 1e-3 using an adaptive recursive Simpson's rule. ’f' is a string containing the name of the function. >> quad(‘sin’, 0, pi/2) Function f must return a vector of output values if given a vector of input values.

0.8

Example:



0.2 + 25x − 200 x 2 + 675x3 − 900 x 4 + 400 x5 dx

0

poly5.m function p = poly5(x) p=0.2+25*x-200*x.^2+675*x.^3-900*x.^4+400*x.^5; >> quad(‘poly5’, 0, 0.8) ans = 1.6405

Example: Computing the Length of a Curve

x(t ) = sin(2t ),

y (t ) = cos(t ),

z (t ) = t

where t ∈ [ 0, 3π ] >> t = 0:0.1:3*pi; >> plot3(sin(2*t), cos(t), t) 10 8 6 4 2 0 1 0.5

1 0.5

0 0

-0.5

- 0.5 -1

-1

Length of the curve: Norm of derivative





4 cos(2t )2 + sin(t )2 + 1 dt

0

hcurve.m function f = hcurve(t) f = sqrt(4*cos(2*t).^2 + sin(t).^2 + 1); >> len = quad(‘hcurve’,0,3*pi) len = 17.2220

Example: Double Integration y max x max

∫ ∫

y min

f ( x, y ) dx dy

x min

For example: f (x, y) = y sin(x) + x cos(y) integrnd.m function out = integrnd(x,y) out = y*sin(x) + x*cos(y); >> xmin = pi; xmax = 2*pi; >> ymin = 0; ymax = pi; >> result = dblquad(‘integrnd’,xmin,xmax,ymin,ymax); result = -9.8698

Suggest Documents