Berikut adalah contoh kode untuk mencari akar-akar persamaan kuadrat dengan metode Newton
'Metode Newton'
x=input('masukkan tebakan awal : ');
y=-1+5.5*x-4*x^2+0.5*x^3;
dy=5.5-8*x+1.5*x^2;
x1=x-y/dy;
if y~=0;
e=abs((x1-x)/x);
while e>0.0001;
x=x1;
y=-1+5.5*x-4*x^2+0.5*x^3;
dy=5.5-8*x+1.5*x^2;
x1=x-y/dy;
e=abs((x1-x)/x);
end;
e=e*100;
fprintf('%15.13f\n',x1)
fprintf('%15.13f',e)
else
x
end
a=-1:0.2:7;
b=-1+5.5*a-4*a.^2+0.5*a.^3;
plotyy (a,b,a,b,'plot','stem');
x=input('masukkan tebakan awal : ');
y=-1+5.5*x-4*x^2+0.5*x^3;
dy=5.5-8*x+1.5*x^2;
x1=x-y/dy;
if y~=0;
e=abs((x1-x)/x);
while e>0.0001;
x=x1;
y=-1+5.5*x-4*x^2+0.5*x^3;
dy=5.5-8*x+1.5*x^2;
x1=x-y/dy;
e=abs((x1-x)/x);
end;
e=e*100;
fprintf('%15.13f\n',x1)
fprintf('%15.13f',e)
else
x
end
a=-1:0.2:7;
b=-1+5.5*a-4*a.^2+0.5*a.^3;
plotyy (a,b,a,b,'plot','stem');
Ada komentar, kritik, saran, atau request?