5
NUMERICAL INTEGRATION Trapezoidal Rule: for R b a f (x)dx; divide [a, b] into n equal subintervals with x i = a + i(b - a)/n, for i =0, 1,...,n; approximate integral using subinterval Trapezoid areas Z x i+1 x i f (x)dx b - a 2n h f (x i )+ f (x i+1 ) i , Z b a f (x)dx b - a 2n h f (x 0 )+2f (x 1 )+···+2f (x n-1 )+f (x n ) i ; notes: a) Compare with midpoint rule R b a f (x)dx b-a n h f ( x 0 +x 1 2 )+ ··· + f ( x n-1 +x n 2 ) i ; b) Trapezoidal and midpoint rules both usually have errors C/n 2 ( for different C ’s). c) Concavity determines sign of error.

l0801

Embed Size (px)

Citation preview

Page 1: l0801

NUMERICAL INTEGRATION

Trapezoidal Rule: for∫ b

a f (x)dx;

• divide [a, b] into n equal subintervals withxi = a + i(b− a)/n, for i = 0, 1, . . . , n;

• approximate integral using subinterval Trapezoid areas∫ xi+1

xi

f (x)dx ≈ b− a

2n

[f (xi) + f (xi+1)

],

∫ b

a

f (x)dx ≈ b− a

2n

[f (x0)+2f (x1)+· · ·+2f (xn−1)+f (xn)

];

• notes: a) Compare with midpoint rule∫ b

a f (x)dx ≈ b−an

[f (x0+x1

2 ) + · · · + f (xn−1+xn

2 )];

b) Trapezoidal and midpoint rules both usually haveerrors ≈ C/n2 ( for different C’s).

c) Concavity determines sign of error.

Page 2: l0801

TRAPEZOIDAL RULE EXAMPLES

Trapezoidal Rule Examples:

•∫ 1

0 x2dx with n = 2, 4

• I =∫ 1

0 ex2dx? Matlab Trapezoidal vs. Midpoint

f = @(x)exp(x.^2); a = 0; b = 1;

n = 4; h = (b-a)/n; % Trapezoidal

T = (h/2)*(f(a)+2*sum(f(a+h:h:b-h))+f(b)); disp(T)

1.4907

n = 8; h = (b-a)/n; % Trapezoidal

T = (h/2)*(f(a)+2*sum(f(a+h:h:b-h))+f(b)); disp(T)

1.4697

n = 4; h = (b-a)/n; % Midpoint Rule

M = h*sum(f(a+h/2:h:b-h/2)); disp(M)

1.4487

n = 8; h = (b-a)/n; % Midpoint Rule

M = h*sum(f(a+h/2:h:b-h/2)); disp(M)

1.4591

2

Page 3: l0801

TRAPEZOIDAL RULE EXAMPLES

format long

n = 1000; h = (b-a)/n; % Trapezoidal

T = (h/2)*(f(a)+2*sum(f(a+h:h:b-h))+f(b)); disp(T)

1.462652198954077

n = 2000; h = (b-a)/n; % Trapezoidal

T = (h/2)*(f(a)+2*sum(f(a+h:h:b-h))+f(b)); disp(T)

1.462651859168920

n = 10000; h = (b-a)/n; % Trapezoidal

T = (h/2)*(f(a)+2*sum(f(a+h:h:b-h))+f(b)); disp(T)

1.462651750437651

n = 10000; h = (b-a)/n; % Midpoint Rule

M = h*sum(f(a+h/2:h:b-h/2)); disp(M)

1.462651743641941

3

Page 4: l0801

NUMERICAL INTEGRATION CONT.

Simpson’s Rule: for∫ b

a f (x)dx;

• divide [a, b] into n equal subintervals with

xi = a + ib− a

n

for i = 0, 1, . . . , n, and n even;

• approximate integral using subinterval parabola areas∫ xi+2

xi

f (x)dx ≈ xi+2 − xi

6

[f (xi)+4f (xi+1)+f (xi+2)

],

so that∫ b

a

f (x)dx ≈ b− a

3n

[f (x0) + 4f (x1) + 2f (x2) + 4f (x3) +

· · · + 2f (xn−2) + 4f (xn−1) + f (xn)];

• note: Simpson’s rule error is usually ≈ Cn4 (some C).

4

Page 5: l0801

SIMPSON’S RULE EXAMPLE

Simpson’s Rule Examples:

•∫ 1

0 x2dx with n = 2, 4

• I =∫ 1

0 ex2dx? Matlab

f = @(x)exp(x.^2); a = 0; b = 1;

n = 4; h = (b-a)/n;

O = f(a+h:2*h:b-h); E = f(a+2*h:2*h:b-2*h);

S = (h/3)*(f(a)+4*sum(O)+2*sum(E)+f(b)); disp(S)

1.4637

n = 8; h = (b-a)/n;

O = f(a+h:2*h:b-h); E = f(a+2*h:2*h:b-2*h);

S = (h/3)*(f(a)+4*sum(O)+2*sum(E)+f(b)); disp(S)

1.4627

format long

n = 1000; h = (b-a)/n;

O = f(a+h:2*h:b-h); E = f(a+2*h:2*h:b-2*h);

S = (h/3)*(f(a)+4*sum(O)+2*sum(E)+f(b)); disp(S)

1.462651745907483

n = 2000; h = (b-a)/n;

O = f(a+h:2*h:b-h); E = f(a+2*h:2*h:b-2*h);

S = (h/3)*(f(a)+4*sum(O)+2*sum(E)+f(b)); disp(S)

1.462651745907200

5