Beruflich Dokumente
Kultur Dokumente
METODE NUMERIK
OLEH :
AMBANG RAMADHAN
F1B314020
KENDARI
2017
1. Function trapezoid
function Ih = trapezoid(func,a,b,I2h,k)
% Recursive trapezoidal rule.
% USAGE: Ih = trapezoid(func,a,b,I2h,k)
% func = handle of function being integrated.
% a,b = limits of integration.
% I2h = integral with 2(k-1) panels.
% Ih = integral with 2k panels.
if k == 1
fa = feval(func,a); fb = feval(func,b);
Ih = (fa + fb)*(b - a)/2.0;
else
n = 2^(k -2 ); % Number of new points
h = (b - a)/n ; % Spacing of new points
x = a + h/2.0; % Coord. of 1st new point
sum = 0.0;
for i = 1:n
fx = feval(func,x);
sum = sum + fx;
x = x + h;
end
Ih = (I2h + h*sum)/2.0;
end
2. Example 6.1
Solution Referring to Fig. 6.4, we see that Simpsons 1/3 rule uses three nodes
located at x1 = a, x2 = (a+b)/2 and x3 = b. The spacing of the nodesish =
(ba)/2. The cardinal functions of Lagranges three-point interpolation are
(see Art. 3.2)
x x2 x x3 x x1 x x3
x x
1
x1 x2 x1 x3 2
x2 x1 x2 x3
x x1 x x3
x
3
x3 x1 x3 x2
h
0 h d 1 h 2 h d h
A1 h 2h 2h 2 h
3
h
A2
h
h h h
d 2 2 h 2 d
1 4h
h h h h h 3
h
h 0 d 1 h 2 h d h
A3 2h h 2h 2 h
3
h
3
ab h
I Ai f xi f a 4 f f b
i 1 2 3
3. Example 6.2
Evaluate the boundson sin( x)dx with the composite trapezoidal rule using
0
(1)
Eight panels and (2)sixteen panels.
Penyelesaian :
%code CompTrapez.m
clc;
clear all;
a = 0; % syarat batas bawah integral
b = pi; % syarat batas atas integral
n = 8;
n1=16;
%metode composite trapezoidal
I = CompTrapz(a,b,n,'f');
disp(['Nilai I = ' num2str(I)]);
I2 = CompTrapz(a,b,n1,'f');
disp(['Nilai I2 = ' num2str(I2)]);
Nilai I = 4.9348
Nilai I2 = 4.9348
4. Example 6.3
25
Estimate f ( x)dx from the data
0
Penyelesaian :
clc;
clear all;
a = 0;
b = 1.5;
h = (b - a)/3;
c = a + h;
d = a + 2*h;
a=1.5; b=1.6364; c=2; d=2;
a1=1.6364; c1=1.25; b1=0.9565;
simpson_38 = (3*h/8)*(f(a)+3*f(c)+3*f(d)+f(b));
simpson_13 = h/3*(f(a1)+4*f(c1)+f(b1));
simpson_gab=simpson_38+simpson_13;
disp(['simpson_38 = ' num2str(simpson_38)]);
disp(['simpson_13 = ' num2str(simpson_13)]);
disp(['simpson_gab = ' num2str(simpson_gab)]);
simpson_38 = 2.8381
simpson_13 = 1.2655
simpson_gab = 4.1036
5. Example 6.4
function y = fex6_4(x)
% Function used in Example 6.4
y = sqrt(x)*cos(x);
Integral =
-0.894831664853286
No_of_func_evaluations =
32769
6. Example 6.5
Show that Rk ,2 in Romberg integration is identical to the composite Simpsons
1/3 rule in Eq.(6.10) with 2 panels.
n 1
1 h
Rk ,1 I k f ( x1 ) 2 f ( xi ) f ( xn )
i 2 2 2
n 1
1
Rk 1,1 I k 1 f ( x1 ) 2 f ( xi ) f ( xn ) h
i 3,5,... 2
4 1
Rk ,2 Rk ,1 Rk 1,1
3 3
1 4 n 1 2 n2 1
f ( x1 ) f ( xi ) f ( xn ) h
3 3 i 2,4,... 3 i 3,5... 3
7. Function Romberg
clear all;
clc;
func=inline('sin(x)');
a=0;
b=pi;
tol=1.0e-8;
kMax=20;
[I,numEval] = romberg(func,a,b,tol,kMax);
disp('nilai integralnya adalah :')
fprintf('%1.4f\n',I)
disp('nilai nomor function evaluasi adalah :')
fprintf('%1.4f\n',numEval)
nilai integralnya adalah :
2.0000
nilai nomor function evaluasi adalah :
33.0000
7. Example 6.7
2x
2
Use Romberg integration to evaluate cos x 2 dx and compare the result
0
[Integral,numEval] = romberg(@fex6_7,0,sqrt(pi))
-0.894831469484157
numEval =
129
Problem sheet
/4
1. Use there cursive trapezoidal rule to evaluate ln(1 tan x)dx Explain
0
the results.
Penyelesaian :
%code CompTrapez.m
clc;
clear all;
a = 0; % syarat batas bawah integral
b = pi/4; % syarat batas atas integral
n = 1;
%metode composite trapezoidal
I = CompTrapz(a,b,n,'f');
disp(['Nilai I = ' num2str(I)]);
Nilai I = 0.30843