Вы находитесь на странице: 1из 5

NUMERICAL INTEGRATION

Rb
Trapezoidal Rule: for a f (x)dx;
divide [a, b] into n equal subintervals with
xi = a + i(b a)/n, for i = 0, 1, . . . , n;
approximate integral using subinterval Trapezoid areas
Z xi+1
i
b ah
f (x)dx
f (xi) + f (xi+1) ,
2n
xi

i
b ah
f (x0)+2f (x1)+ +2f (xn1)+f (xn) ;
f (x)dx
2n
a
notes: a) Compare
h with midpoint rule
i
Rb
x
+x
x
+x
ba
n
n1
0
1
) ;
a f (x)dx n f ( 2 ) + + f (
2
b) Trapezoidal and midpoint rules both usually have
errors C/n2 ( for different Cs).
c) Concavity determines sign of error.

TRAPEZOIDAL RULE EXAMPLES


Trapezoidal Rule Examples:
R1 2
0 x dx with n = 2, 4

I=

R1
0

ex dx? 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

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

NUMERICAL INTEGRATION CONT.


Rb
Simpsons Rule: for a f (x)dx;
divide [a, b] into n equal subintervals with
ba
xi = a + i
n
for i = 0, 1, . . . , n, and n even;
approximate integral using subinterval parabola areas
Z xi+2
i
xi+2 xi h
f (x)dx
f (xi)+4f (xi+1)+f (xi+2) ,
6
xi

so that
b

Z
a

b ah
f (x0) + 4f (x1) + 2f (x2) + 4f (x3) +
f (x)dx
3n
i

+ 2f (xn2) + 4f (xn1) + f (xn) ;

note: Simpsons rule error is usually

C
n4

(some C).

SIMPSONS RULE EXAMPLE


Simpsons Rule Examples:
R1 2
0 x dx with n = 2, 4

I=

R1
0

ex dx? Matlab

f
n
O
S

= @(x)exp(x.^2); a = 0; b = 1;
= 4; h = (b-a)/n;
= f(a+h:2*h:b-h); E = f(a+2*h:2*h:b-2*h);
= (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

Вам также может понравиться