Anda di halaman 1dari 7

Penyelesaian Integral dengan Matlab

Diketahui:

f(x):

function y=fx1(X)
y = 8 + 4*cos(X);
end

integral f(x):

function y=fx2(X)
y = 8*X + 4*sin(X);
end

1. Secara Analitis
Source code:
%This code used to calculate integration of the function analytically
function analytic(a,b)
%known a=0 ; b=pi/2 so:
a=0;
b=pi/2;
I = fx2(b) - fx2(a)
end

Hasil Simulasi:

2. Dengan single aplication of the trapezoidal rule serta relative errornya


Source code:
%This code used to calculate integration of the function with single
%aplication of trapezoidal rule
function single_trap(a,b)
%known a=0 ; b=pi/2 so:
a=0;
b=pi/2;
h=(b-a)/2; %for single aplication
exact=16.5664;
I = h*(fx1(a)+fx1(b))
Error = abs(((exact-I)/exact )*100)
end

Penyelesaian Integral dengan Matlab

Hasil Simulasi:

3. Dengan multiple aplication of the trapezoidal rule (n=2 and n=4) serta relative
errornya
Source code:
%This code used to calculate integration of the function with multi
%aplication of trapezoidal rule
function multi_trap(a,b,n)
%known a=0 ; b=pi/2 so:
a=0;
b=pi/2;
n=2; %bisa diubah
exact=16.5664;
h=(b-a)/n; %for multi aplication
sigma = fx1(a); %initial value
for i=1:(n-1);
sigma = sigma + 2*fx1(a+i*h);
end
sigma = sigma + fx1(b);
I = (h/2)*sigma
Error = abs(((exact-I)/exact )*100)
end

Penyelesaian Integral dengan Matlab

Hasil Simulasi:
n=2

n=4

4. Dengan single aplication of Simpson 1/3 rule serta relative errornya


Source code:
%This code used to calculate integration of the function with single
%aplication of Simpson 1/3 rule
function single_simpson13(a,b)
%known a=0 ; b=pi/2 so:
a=0;
b=pi/2;
h=(b-a)/2; %for single aplication
exact=16.5664;
I = (h/3)*(fx1(a) + 4*fx1((a+b)/2) + fx1(b))
Error = abs(((exact-I)/exact )*100)
end

Penyelesaian Integral dengan Matlab

Hasil Simulasi:

5. Dengan multiple aplication of Simpson 1/3 rule (n=4) serta relative errornya
Source code:
%This code used to calculate integration of the function with multi
%aplication of Simpson 1/3 rule
function multi_simpson13(a,b,n)
%known a=0 ; b=pi/2 so:
a=0;
b=pi/2;
n=4; %bisa diganti
exact=16.5664;
h=(b-a)/n; %for multi aplication
%Calculating sigma for odd function
sum_odd=0;
for i=1:2:n-3
sum_odd=sum_odd + fx1(a+i*h) ;
end
sum_odd=sum_odd + fx1(a+(n-1)*h) ; %until fx(n-1)
sum_odd=4*sum_odd ; %for odd function, type by 4
%Calculating sigma for even function
sum_even=0;
for i=2:2:n-4
sum_even=sum_even + fx1(a+i*h) ;
end
sum_even=sum_even + fx1(a+(n-2)*h) ; %until fx(n-2)
sum_even=2*sum_even ; %for odd function, type by 2
%Final
I = (h/3)*(fx1(a) + sum_odd + sum_even + fx1(b))
Error = abs(((exact-I)/exact )*100)
end

Penyelesaian Integral dengan Matlab

Hasil Simulasi:

6. Dengan single aplication of Simpson 3/8 rule serta relative errornya


Source code:
%This code used to calculate integration of the function with single
%aplication of Simpson 3/8 rule
function single_simpson38(a,b)
%known a=0 ; b=pi/2 so:
a=0;
b=pi/2;
h=(b-a)/3; %for single aplication of Simpson 3/8 rule
exact=16.5664;
I = (3*h/8)*(fx1(a) + 3*fx1(a+h) + 3*fx1(a+2*h) + fx1(b))
Error = abs(((exact-I)/exact )*100)
end

Hasil Simulasi:

Penyelesaian Integral dengan Matlab

7. Dengan multiple aplication of Simpson 3/8 rule (n=5) serta relative errornya
Source code:
%This code used to calculate integration of the function with multi
%aplication of Simpson 3/8 rule
function multi_simpson38(a,b,n)
%known a=0 ; b=pi/2 so:
a=0;
b=pi/2;
n=5; %Bisa diganti
h=(b-a)/n; %for single aplication of Simpson 3/8 rule
exact=16.5664;
%Calculating sigma for i=3,6,9.... function
sum_1=0;
for i=3:3:n-3
sum_1=sum_1 + fx1(a+i*h) ;
end
sum_1=2*sum_1 ; %type by 2
%Calculating sigma for i=1,4,7.... function
sum_2=0;
for i=1:3:n-2
sum_2=sum_2 + fx1(a+i*h) ;
end
sum_2=3*sum_2 ; %type by 3
%Calculating sigma for i=2,5,8.... function
sum_3=0;
for i=2:3:n-1
sum_3=sum_3 + fx1(a+i*h);
end
sum_3=3*sum_3 ; %type by 3
%Final
I = (3*h/8)*(fx1(a) + sum_1 + sum_2 + sum_3 + fx1(b))
Error = abs(((exact-I)/exact )*100)
end

Penyelesaian Integral dengan Matlab

Hasil Simulasi:

**Dari hasil simulasi di atas, terlihat bahwa nilai approximation dengan metode Simpson
3/8 dan dengan n=5 didapat error relatif yang cukup tinggi. Hal ini dikarenakan metode
Simpson 3/8 hanya akan berjalan dengan baik apabila nilai n yang digunakan kelipatan 3
(n=3,6,9,). Jadi, bila n=5, maka hasil yang didapat jauh dari nilai eksaknya, namun jika
digunakan n=6, menghasilkan error yang rendah, sebagai berikut:

Anda mungkin juga menyukai