Sie sind auf Seite 1von 9

TUGAS VI

METODE NUMERIK

( INTEGRAL SECARA NUMERIK )

OLEH :
AMBANG RAMADHAN
F1B314020

JURUSAN TEKNIK PERTAMBANGAN

FAKULTAS ILMU DAN TEKNOLOGI KEBUMIAN

UNIVERSITAS HALU OLEO

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

x 0 0.5 1.0 1.5 2.0 2.5


f ( x) 1.5000 2.0000 2.00000 1.6364 1.2500 0.9565

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

Use therecursivetrapezoidal rule to evaluate to six decimal places. How many


function evaluation sare required to achieve this result?
Penyelesaian :
format long % Display extra precision
I2h = 0;
for k = 1:20
Ih = trapezoid(@fex6_4,0,pi,I2h,k);
if (k > 1 & abs(Ih - I2h) < 1.0e-6)
Integral = Ih
No_of_func_evaluations = 2^(k-1) + 1
return
end
I2h = Ih;
end
error('Too many iterations')

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

function [I,numEval] = romberg(func,a,b,tol,kMax)


% Romberg integration.
% USAGE: [I,numEval] = romberg(func,a,b,tol,kMax)
% INPUT:
% func = handle of function being integrated.
% a,b = limits of integration.
% tol = error tolerance (default is 1.0e-8).
% kMax = limit on the number of panel doublings
% (default is 20).
% OUTPUT:
% I = value of the integral.
% numEval = number of function evaluations.
if nargin < 5; kMax = 20; end
if nargin < 4; tol = 1.0e-8; end
r = zeros(kMax);
r(1) = trapezoid(func,a,b,0,1);
rOld = r(1);
for k = 2:kMax
r(k) = trapezoid(func,a,b,r(k-1),k);
r = richardson(r,k);
if abs(r(1) - rOld) < tol
numEval = 2^(k-1) + 1; I = r(1);
return
end
rOld = r(1);
end
error('Failed to converge')
function r = richardson(r,k)
% Richardsons extrapolation in Eq. (6.14).
for j = k-1:-1:1
c = 4^(k-j); r(j) = (c*r(j+1) - r(j))/(c-1);
end
8. Example 6.6

Use Romberg integration to evaluate f ( x)dx dimana
0
f ( x) sin x .Work with

four decimal places.


Penyelesaian :

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

swith Example 6.4.


Penyelesaian :
function y = fex6_7(x)
% Function used in Example 6.7
y = 2*(x^2)*cos(x^2);

[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

Das könnte Ihnen auch gefallen