D42112276
function y=bagidua(xawal,xakhir,iter)
if fx(xawal)*fx(xakhir)<0
xc=(xawal+xakhir)/2;
for i=1:iter
xc=(xawal+xakhir)/2;
if fx(xawal)*fx(xc)<0
xakhir=xc;
while fx(xc)<=-0.0001
xc=(xawal+xakhir)/2;
if fx(xawal)*fx(xc)<0
xakhir=xc;
elseif fx(xawal)*fx(xc)>0
xawal=xc;
end
end
elseif fx(xawal)*fx(xc)>0
xawal=xc;
while fx(xc)<=-0.0001
xc=(xawal+xakhir)/2;
if fx(xawal)*fx(xc)<0
xakhir=xc;
elseif fx(xawal)*fx(xc)>0
xawal=xc;
end
end
end
disp([i' xc']);
end
y=fx(xc);
elseif fx(xawal)*fx(xakhir)>0
disp('tidak ada akar dalam interval tersebut');
end
Fungsi diatas akan menerima 3 angka inputan yakni, nilai nilai x kiri dan nilai x kanan serta nilai iterasi.
Program akan mengembalikan nilai akar jika terdapat akar diantara dua interval tersebut atau
menampilkan teks bahwa tidak ada akar di antara interval tersebut.
>> bagidua(10,15,15)
1.0000
12.5000
2.0000
12.1875
3.0000
12.1484
4.0000
12.1289
5.0000
12.1240
6.0000
12.1216
7.0000
12.1204
8.0000
12.1201
9.0000
12.1200
10.0000
12.1200
11.0000
12.1200
12.0000
12.1200
13.0000
12.1200
14.0000
12.1200
15.0000
12.1200
ans =
-6.2059e-07
function y=posisipalsu(xawal,xakhir,iter)
if fx(xawal)*fx(xakhir)<0
xc=xawal-((fx(xawal)*(xawal-xakhir))/(fx(xawal)fx(xakhir)));
for i=1:iter
xc=xawal-((fx(xawal)*(xawalxakhir))/(fx(xawal)-fx(xakhir)));
if fx(xawal)*fx(xc)<0
xakhir=xc;
while fx(xc)<=-0.0001
xc=(xawal+xakhir)/2;
if fx(xawal)*fx(xc)<0
xakhir=xc;
elseif fx(xawal)*fx(xc)>0
xawal=xc;
end
end
elseif fx(xawal)*fx(xc)>0
xawal=xc;
while fx(xc)<=-0.0001
xc=(xawal+xakhir)/2;
if fx(xawal)*fx(xc)<0
xakhir=xc;
elseif fx(xawal)*fx(xc)>0
xawal=xc;
end
end
end
disp([i' xc']);
end
y=fx(xc);
elseif fx(xawal)*fx(xakhir)>0
disp('tidak ada akar dalam interval tersebut');
Fungsi diatas hampir sama dengan fungsi bagi dua namun fungsi ini memiliki metode berbeda dalam
mencari nilai akar .
>> posisipalsu(10,15,15)
1.0000
12.7308
2.0000
12.1574
3.0000
12.1342
4.0000
12.1271
5.0000
12.1200
6.0000
12.1200
7.0000
12.1200
8.0000
12.1200
9.0000
12.1200
10.0000
12.1200
11.0000
12.1200
12.0000
12.1200
13.0000
12.1200
14.0000
12.1200
15.0000
12.1200
ans =
2.8422e-14
xawal1=1;
xawal2=5;
Niter=15;
iter=1:Niter+2;
a=bagidua(xawal1,xawal2,Niter);
b=posisipalsu(xawal1,xawal2,Niter);
figure;
plot(iter,a,'b+-',iter,b,'k>:')
legend('Metode Bagidua','Metode Posisi Palsu')
xlabel('iterasi,x')
ylabel('x(k)')
title('Membandingkan Metode Bagidua dan Posisi Palsu')
grid
-6
x 10
Metode Bagidua
Metode Posisi Palsu
1
x(k)
-1
-2
-3
-4
0
8
10
iterasi,x
12
14
16
18
Metode Newton-Raphson
function
[x,err]=newr(xawal, iterasi)
x(1)=xawal;
err(1)=fx(xawal);
for k=1:iterasi
x(k+1)=x(k)-fx(x(k))/fxdot(x(k));
err(k+1)=fx(x(k)-0);
end
disp([x' err']);
newr(15,15)
15.0000 236.2660
13.2774 236.2660
12.4195
60.2267
12.1488
11.7725
12.1203
1.0263
12.1200
0.0109
12.1200
0.0000
12.1200
0.0000
12.1200
-0.0000
12.1200
0.0000
12.1200
0.0000
12.1200
0.0000
12.1200
0.0000
12.1200
0.0000
12.1200
0.0000
12.1200
0.0000
ans =
Columns 1 through 9
15.0000
13.2774
12.4195
12.1200
12.1200
12.1200
12.1488
12.1203
12.1200
12.1200
12.1200
12.1200
Columns 10 through 16
12.1200
12.1200
12.1200
Metode Secant
function [x,err]=talibusur(xawal,xakhir,Niterasi)
x(1)=xawal;
x(2)=xakhir;
err(1)=fx(xawal);
err(2)=fx(xakhir);
for k=1:Niterasi
x(k+2)=x(k+1)-(fx(x(k+1))*(x(k+1)x(k)))/(fx(x(k+1))-fx(x(k)));
err(k+2)=fx1(x(k+2)-0);
end
disp([x' err']);
>> talibusur(15,10,12)
15.0000 236.2660
10.0000 -24.0340
10.4617
16.2223
4.7593
-6.6667
8.8603
3.2271
8.4606
0.7834
8.5227
1.1423
8.5200
1.1267
8.5200
1.1266
8.5200
1.1266
8.5200
1.1266
-Inf
Inf
NaN
NaN
NaN
NaN
ans =
Columns 1 through 9
15.0000
10.0000
10.4617
8.5227
8.5200
8.5200
4.7593
8.8603
NaN
NaN
Columns 10 through 14
8.5200
8.5200
-Inf
8.4606
xawal1=15;
xawal2=10;
Niter=12;
iter=1:Niter+2;
[xn,errn]=newr(xawal2,Niter+1);
[xs,errs]=talibusur(xawal1,xawal2,Niter);
figure;
plot(iter,xn,'b+-',iter,xs,'ko:')
legend('Metode Newton-Raphson','Metode Secant')
xlabel('iterasi,x')
ylabel('x(k)')
title('Membandingkan Metode Newton-Raphson dan Secant')
x(k)
12
10
8
iterasi,x
10
12
14
function y=bagidua1(x1,x2,iter)
if fx2(x1)*fx2(x2)<0
x3=(x1+x2)/2;
for i=1:iter
x3=(x1+x2)/2;
if fx2(x1)*fx2(x3)<0
x2=x3;
while fx2(x3)<=-0.0001
x3=(x1+x2)/2;
if fx2(x1)*fx2(x3)<0
x2=x3;
elseif fx2(x1)*fx2(x3)>0
x1=x3;
end
end
elseif fx2(x1)*fx2(x3)>0
x1=x3;
while fx2(x3)<=-0.0001
x3=(x1+x2)/2;
if fx2(x1)*fx2(x3)<0
x2=x3;
elseif fx2(x1)*fx2(x3)>0
x1=x3;
end
end
end
disp([i' x3']);
end
y=fx2(x3);
elseif fx2(x1)*fx2(x2)>0
y=Inf ;
disp('tidak ada akar dalam interval tersebut');
end
function y=posisipalsu2(x1,x2,iter)
if fx2(x1)*fx2(x2)<0
x3=x1-((fx2(x1)*(x1-x2))/(fx2(x1)-fx2(x2)));
for i=1:iter
x3=x1-((fx2(x1)*(x1-x2))/(fx2(x1)-fx2(x2)));
if fx2(x1)*fx2(x3)<0
x2=x3;
while fx2(x3)<=-0.0001
x3=(x1+x2)/2;
if fx2(x1)*fx2(x3)<0
x2=x3;
elseif fx2(x1)*fx2(x3)>0
x1=x3;
end
end
elseif fx2(x1)*fx2(x3)>0
x1=x3;
while fx2(x3)<=-0.0001
x3=(x1+x2)/2;
if fx2(x1)*fx2(x3)<0
x2=x3;
elseif fx2(x1)*fx2(x3)>0
x1=x3;
end
end
end
disp([i' x3']);
end
y=fx2(x3);
elseif fx2(x1)*fx2(x2)>0
y=Inf;
disp('tidak ada akar dalam interval tersebut');
end
Metode bagi dua dan posisi palsu tidak menemukan akar persamaan tersebut dan hanya menampilkan
tidak ada akar dalam interval tersebut
Metode Newton-Rapshon
function [x err]=newr1(x1,iterasi)
x(1)=x1;
err(1)=fx2(x1);
for k=1:iterasi
x(k+1)=x(k)-fx2(x(k))/fx2dot(x(k));
err(k+1)=fx2(x(k)-0);
end
disp([x' err']);
>> newr1(15,15)
1.0e+04 *
0.0015
0.0011
0.0007
0.0004
0.0002
0.0001
0.0000
-0.0000
0.0001
0.0001
-0.0000
0.0002
0.0001
0.0000
-0.0000
0.0001
ans =
-6.4541
-6.4541
-2.1741
-0.6998
-0.2140
-0.0629
-0.0187
-0.0065
-0.0049
-0.0206
-0.0070
-0.0047
-0.0582
-0.0173
-0.0062
-0.0052
Columns 1 through 9
15.0000
10.6008
6.9853
0.4827
-0.2283
1.3224
4.2690
2.4170
1.2427
1.1855
0.4419
-0.2936
Columns 10 through 16
0.5384
0.9390
-0.1491
2.3256
Metode Secant
function [x,err]=talibusur1(x1,x2,Niterasi)
x(1)=x1;
x(2)=x2;
err(1)=fx2(x1);
err(2)=fx2(x2);
for k=1:Niterasi
x(k+2)=x(k+1)-(fx2(x(k+1))*(x(k+1)x(k)))/(fx2(x(k+1))-fx2(x(k)));
err(k+2)=fx2(x(k+2)-0);
end
disp([x' err']);
>> talibusur1(10,15,15)
1.0e+04 *
0.0010
0.0015
0.0008
0.0007
0.0004
0.0003
0.0002
0.0001
0.0001
0.0000
-0.0000
0.0003
-0.0001
-0.0001
-0.0000
0.0001
-0.0001
-1.8376
-6.4541
-0.9988
-0.6366
-0.2390
-0.1076
-0.0441
-0.0189
-0.0085
-0.0048
-0.0060
-0.0935
-0.0079
-0.0121
-0.0045
-0.0071
-0.0124
ans =
Columns 1 through 9
10.0000
15.0000
8.0097
2.0202
1.2505
0.6761
6.7298
4.4800
3.1279
xawal1=15;
xawal2=10;
Niter=12;
iter=1:Niter+2;
[xn,errn]=newr1(xawal2,Niter+1);
[xs,errs]=talibusur1(xawal1,xawal2,Niter);
figure;
plot(iter,xn,'b+-',iter,xs,'ko:')
legend('Metode Newton-Raphson','Metode Secant')
xlabel('iterasi,x')
ylabel('x(k)')
title('Membandingkan Metode Newton-Raphson dan Secant')
14
12
10
x(k)
8
6
4
2
0
-2
8
iterasi,x
10
12
14