Gunakan Metode bagi dua, Newton, dan Secant untuk menemukan solusi dengan tingkat keakuratan
dari Persamaan
Penyelesaian
clearvars
syms x
f=x^3-x^2-3*x-3;
fplot(f)
a=2;
b=3;
tol=10^-10;
max=50;
done=0;
F= @(x) x^3-x^2-3*x-3
FA=F(a);
i=1;
1
while i<=max && done==0
XNS=(a+b)/2;
FXNS=F(XNS);
if i == 1
T(i,:)=[i,a,FA,b,XNS,FXNS];
end
if abs(FXNS)<tol
fprintf(['Hasil estimasi nilai akar dari interval '...
'[%.2f,%.2f]adalah\n%.6f'],2,3,XNS);
done=1;
else
i=i+1;
if FA*FXNS>0
a=XNS;
FA=FXNS;
else
b=XNS;
end
XNS=(a+b)/2;
end
T(i,:)=[i,a,FA,b,XNS,FXNS];
end
T = 36×6
1.000000000000000 2.000000000000000 -5.000000000000000 3.000000000000000
2.000000000000000 2.500000000000000 -1.125000000000000 3.000000000000000
3.000000000000000 2.500000000000000 -1.125000000000000 2.750000000000000
4.000000000000000 2.500000000000000 -1.125000000000000 2.625000000000000
5.000000000000000 2.562500000000000 -0.427490234375000 2.625000000000000
6.000000000000000 2.593750000000000 -0.059234619140625 2.625000000000000
7.000000000000000 2.593750000000000 -0.059234619140625 2.609375000000000
8.000000000000000 2.593750000000000 -0.059234619140625 2.601562500000000
9.000000000000000 2.597656250000000 -0.012275159358978 2.601562500000000
10.000000000000000 2.597656250000000 -0.012275159358978 2.599609375000000
Metode Newton
clearvars; clc
syms x
format long
f(x)=x^3-x^2-3*x-3;
df(x)=diff(f)
df(x) =
Berdasarkan grafik yang sudah dibuat sebelumnya inisial awal yang diambil yaitu 2
2
Penyelesaian
x0=2;
tol=10^-10;
max=50;
i=1;
error=1;
while (i<max && error>tol)
x=x0-(f(x0)/df(x0));
error=abs((x-x0)/x0);
data(i,:) =[i x0 x f(x)];
i=i+1;
x0=x;
end
double(data)
ans = 6×4
1.000000000000000 2.000000000000000 3.000000000000000 6.000000000000000
2.000000000000000 3.000000000000000 2.666666666666667 0.851851851851852
3.000000000000000 2.666666666666667 2.601139601139601 0.029775214343521
4.000000000000000 2.601139601139601 2.598677924626905 0.000041212788568
5.000000000000000 2.598677924626905 2.598674507888131 0.000000000079338
6.000000000000000 2.598674507888131 2.598674507881554 0.000000000000000
if i>max || error>tol
disp('solusi tidak ditemukan')
end
Metode Secant
clearvars
format long
f=@(x) x^3-x^2-3*x-3;
Berdasarkan grafik yang sudah dibuat sebelumnya inisial awal yang diambil sama yaitu 2 dan 3
x0=2;
x1=3;
max=50;
tol=10^-10;
a0=f(x0);
a1=f(x1);
3
for i=1:max
x=x1-a1*(x0-x1)/(a0-a1);
data(i,:)=[x0 a0 x1 a1 x abs((x-x1)/x1)];
if abs((x-x1)/x1)<tol
disp(data)
break
end
x0=x1;
a0=a1;
x1=x;
a1=f(x);
end
if i==max
disp('solusi tidak ditemukan!')
end