Tugas Metnum 1. Setengah Interval (V) : While
Tugas Metnum 1. Setengah Interval (V) : While
clear;
clc;
disp('------------------------------')
disp('Metode Setengah Interval')
disp('------------------------------')
f = input('Masukkan fungsi f(x) **contoh x^2-4x+3 menjadi @(x)x.^2-4.*x+3 :
');
x1 = input('Masukkan perkiraan x1 : ');
x2 = input('Masukkan perkiraan x2 : ');
batas = input('Masukkan batas ketelitian : ');
while f(x1) * f(x2) > 0 || f(x1) > f(x2)
disp('Mohon maaf inputan data anda tidak memenuhi syarat')
f = input('Masukkan fungsi f(x) **contoh x^2-4x+3 menjadi @(x)x.^2-
4.*x+3 : ');
x1 = input('Masukkan perkiraan x1 : ');
x2 = input('Masukkan perkiraan x2 : ');
batas = input('Masukkan batas ketelitian : ');
end
no = 1;
xt = (x1+x2)/2;
while abs(f(xt)) > batas
xt = (x1+x2)/2;
iter(no,1) = no;
iter(no,2) = x1;
iter(no,3) = x2;
iter(no,4) = xt;
iter(no,5) = f(x1);
iter(no,6) = f(x2);
iter(no,7) = f(xt);
if f(xt) < 0
x1 = xt;
x2 = x2;
else
x1 = x1;
x2 = xt;
end
no = no+1;
end
format shortg;
disp('---------------------------------------------------------------------
-----------------------')
disp('| No | x1 | x2 | xt | f(x1) |
f(x2) | f(xt) |')
disp('---------------------------------------------------------------------
-----------------------')
disp(iter)
2. Interpolasi Linier (V)
clear;
clc;
disp('-------------------------------')
disp('Metode Interpolasi Linear')
disp('-------------------------------')
f = input('Masukkan fungsi f(x) **contoh x^2-4x+3 menjadi @(x)x.^2-4.*x+3 :
');
x1 = input('Masukkan perkiraan x1 : ');
x2 = input('Masukkan perkiraan x2 : ');
batas = input('Masukkan batas ketelitian : ');
while f(x1) * f(x2) > 0 || f(x1) > f(x2)
disp('Mohon maaf inputan data anda tidak memenuhi syarat')
f = input('Masukkan fungsi f(x) **contoh x^2-4x+3 menjadi @(x)x.^2-
4.*x+3 : ');
x1 = input('Masukkan perkiraan x1 : ');
x2 = input('Masukkan perkiraan x2 : ');
batas = input('Masukkan batas : ');
end
no = 1;
xt = x2-((f(x2)/((f(x2))-(f(x1))))*(x2-x1));
while abs(f(xt)) > batas
xt = x2-((f(x2)/((f(x2))-(f(x1))))*(x2-x1));
iter(no,1) = no;
iter(no,2) = x1;
iter(no,3) = x2;
iter(no,4) = xt;
iter(no,5) = f(x1);
iter(no,6) = f(x2);
iter(no,7) = f(xt);
if f(xt) < 0
x1 = xt;
x2 = x2;
else
x1 = x1;
x2 = xt;
end
no = no+1;
end
format shortg;
disp('---------------------------------------------------------------------
-----------------------')
disp('| No | x1 | x2 | xt | f(x1) |
f(x2) | f(xt) |')
disp('---------------------------------------------------------------------
-----------------------')
disp(iter)
3. Newton Rhapson (V)
clear;
clc;
disp('-----------------------')
disp('Metode Newton Rhapson')
disp('-----------------------')
f = input('Masukkan fungsi f(x) **contoh [1 1 1 1]: ');
xn = input('Masukkan perkiraan x1 : ');
x1 = xn;
batas = input('Masukkan batas : ');
turunan=polyder(f);
disp('Hasil Turunan fungsi f(x):'); turunan
fx=polyval(f,xn);
no = 1;
no=no+1;
end
jawab=iter;
hasil=(['akar persamaan yang diperoleh adalah ' num2str(xn)]);
format shortg;
disp('---------------------------------------------------------------------
-----------------------')
disp('| No | x1 | x2 | f(x1) | f(x2)
|')
disp('---------------------------------------------------------------------
-----------------------')
disp(iter)
4. Secant(V)
clc;
clear;
disp('----------------')
disp('Metode Secant')
disp('----------------')
f = input('Masukkan fungsi f(x) **contoh x^2-4x+3 menjadi @(x)x.^2-4.*x+3 :
');
x1 = input('Masukkan perkiraan x1 : ');
x2 = input('Masukkan perkiraan x2 : ');
batas = input('Masukkan batas ketelitian : ');
while f(x1) * f(x2) > 0 || f(x1) > f(x2)
disp('Mohon maaf inputan data anda tidak memenuhi syarat')
f = input('Masukkan fungsi f(x) **contoh x^2-4x+3 menjadi @(x)x.^2-
4.*x+3 : ');
x1 = input('Masukkan perkiraan x1 : ');
x2 = input('Masukkan perkiraan x2 : ');
batas = input('Masukkan batas ketelitian : ');
end
no = 1;
xt = x2-((f(x2)*(x2-x1))/(f(x2)-f(x1)));
while abs(f(xt)) > batas
xt = x2-((f(x2)*(x2-x1))/(f(x2)-f(x1)));
iter(no,1) = no;
iter(no,2) = x1;
iter(no,3) = x2;
iter(no,4) = xt;
iter(no,5) = f(x1);
iter(no,6) = f(x2);
iter(no,7) = f(xt);
x1 = x2;
x2 = xt;
no = no+1;
end
format shortg;
disp('---------------------------------------------------------------------
-----------------------')
disp('| No | x1 | x2 | xt | f(x1) |
f(x2) | f(xt) |')
disp('---------------------------------------------------------------------
-----------------------')
disp(iter)
5. Iterasi
clear;
clc;
disp('----------------')
disp('Metode Iterasi')
disp('----------------')
f = input('Masukkan fungsi f dalam polinomial **contoh [1 1 1 1]: ');
x1 = input('Masukkan perkiraan x1 : ');
batas=input('masukkan batas : ');
g= (f2);
polyval(g,x1);
x2= (polyval(g,x1)/koeftbs).^(1/pkt);
a=abs((x2-x1)/x2)*100;
no=1;
a= abs((x2-x1)/x2)*100;
iter (no,1)=no;
iter (no,2)=x1;
iter (no+1:5,3)=a;
x1=x2;
no = no+1;
end
jawab=iter;
hasil=(['akar persamaan yang diperoleh adalah ' num2str(x1)]);
format shortg;
disp('------------------------------------------')
disp('| No | x1 | x2 |')
disp('------------------------------------------')
disp(iter)
6. Eliminasi Gauss (V)
clear;
clc;
disp('------------------------------');
disp('Program Metode Eliminasi Gauss');
disp('------------------------------');
disp('Masukkan persamaan dalam bentuk matriks');
m = input('**contoh [3 1 -1 5;4 7 -3 20;2 -2 5 10] : ');
j_brs = size(m,1);
j_klm = size(m,2);
for d = 1:j_klm-2;
for brs = d+1:j_brs;
pb = m(brs,d)/m(d,d);
for klm = 1:j_klm
m(brs,klm) = m(brs,klm)-pb*m(d,klm);
end
end
disp(['Tahap ', num2str(d), ' = '])
disp(num2str(m))
disp(' ')
end
x(j_brs,1) = m(j_brs,j_klm)/m(j_brs,j_brs);
for e = j_brs:-1:1
s = 0;
for klm = e+1:j_brs
s = s+m(e,klm)*x(klm,1);
end
x(e,1) = (m(e,j_klm)-s)/m(e,e);
end
format shortg;
disp('--------------------------------------------------------------------'
)
disp('-----------------Matriks hasil
triangularisasi----------------------')
disp(m)
disp('--------------------------------------------------------------------'
)
for i = 1:j_brs
disp(['x', num2str(i), '=', num2str(x(i,1))])
end
7. Gauss Jordan
clear;
clc;
disp('----------------------')
disp('Metode Gauss Jordan')
disp('----------------------')
disp('Masukkan nilai persamaan kedalam bentuk matriks 4x4')
A=input('**contoh [1 1 1 1;2 2 2 2;3 3 3 3;4 4 4 4]=');
B=input('Masukkan hasil dari persamaan,[hasil1;hasil2;hasil3;hasil4]=');
if (size(A))~= 4 5, disp('matriks salah, matrik harus berukuran
4x5 !!!!'),break,end;
A=[A';B']'
disp('Iterasi-1 ' );
A(1,:)=A(1,:)/A(1,1)
disp('Iterasi-2 ' );
A(2,:)=A(2,:)-((A(2,1)*A(1,:)/A(1,1)))
disp('Iterasi-3 ' );
A(3,:)=A(3,:)-((A(3,1)*A(1,:)/A(1,1)))
disp('Iterasi-4 ' );
A(4,:)=A(4,:)-((A(4,1)*A(1,:)/A(1,1)))
disp('Iterasi-5 ' );
A(1,:)=A(1,:)-((A(1,2)*A(2,:)/A(2,2)))
disp('Iterasi-6 ' );
A(2,:)=A(2,:)/A(2,2)
disp('Iterasi-7 ' );
A(3,:)=A(3,:)-((A(3,2)*A(2,:)/A(2,2)))
disp('Iterasi-8 ' );
A(4,:)=A(4,:)-((A(4,2)*A(2,:)/A(2,2)))
disp('Iterasi-9 ' );
A(1,:)=A(1,:)-((A(1,3)*A(3,:)/A(3,3)))
disp('Iterasi-10 ' );
A(2,:)=A(2,:)-((A(2,3)*A(3,:)/A(3,3)))
disp('Iterasi-11 ' );
A(3,:)=A(3,:)/A(3,3)
disp('Iterasi-12 ' );
A(4,:)=A(4,:)-((A(4,3)*A(3,:)/A(3,3)))
disp('Iterasi-13 ' );
A(1,:)=A(1,:)-((A(1,4)*A(4,:)/A(4,4)))
disp('Iterasi-14 ' );
A(2,:)=A(2,:)-((A(2,4)*A(4,:)/A(4,4)))
disp('Iterasi-15 ' );
A(3,:)=A(3,:)-((A(3,4)*A(4,:)/A(4,4)))
disp('Iterasi-16 ' );
A(4,:)=A(4,:)/A(4,4)