Upload
dina-latifatunnisa
View
4
Download
1
Embed Size (px)
Citation preview
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.
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
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
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
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