Tahun 1225 Leonardo da Pisa mencari akar persamaan:
f(x) = x^3 + 2x^2 + 10x – 20 = 0 dan menemukan x = 1.368808107.
Dengan menggunakan Scilab, berikut metode-metode untuk menentukan akar persamaan Leonardo da Pisa, dalam selang [1, 1.5] dengan epsilon = 10^-6 = 0.000001:
Metode Bagi Dua
Listing Scilab:
function y=f(x)
y= x^3 + 2*x^2 + 10*x - 20;
endfunction
function c=metodebagidua(f, x0, x1, aprox)
i=1;
er(1)=100;
if f(x0)*f(x1) < 0
a(1)=x0;
b(1)=x1;
c(1)=(a(1)+b(1))/2;
printf('r.\t\t a\t\t b\t\t c\t\t f(c)\t Error \n');
printf('%2d \t %11.6f \t %11.6f \t %11.6f \t %11.6f \n',i,a(i),b(i),c(i),f(c(i)));
while abs(er(i)) >= aprox
if f(a(i))*f(c(i))< 0
a(i+1)=a(i);
b(i+1)=c(i);
end
if f(a(i))*f(c(i))> 0
a(i+1)=c(i);
b(i+1)=b(i);
end
c(i+1)=(a(i+1)+b(i+1))/2;
er(i+1)=abs((c(i+1)-c(i))/(c(i+1)));
printf('%2d \t %11.6f \t %11.6f \t %11.6f \t %11.6f \t %7.6f \n',i+1,a(i+1),b(i+1),c(i+1),f(c(i+1)),er(i+1));
i=i+1;
end
else
printf(' ');
end
endfunction
Output Metode Bagi Dua:
Output Metode Regula Falsi:
Output Metode Newton-Raphson:
Output Metode Secant:
Metode Regula Falsi
Listing Scilab:
function y=f(x) y=x^3 + 2*x^2 + 10*x - 20; endfunction function xn=regulafalsi(f, a1, b1, aprox) i=1; ea(1)=100; if f(a1)*f(b1) < 0 x0(1)=a1; x1(1)=b1; xn(1)=x0(1)-f(x0(1))*(x1(1)-x0(1))/(f(x1(1))-f(x0(1))); printf('r.\t\t x0\t\t xn\t\t x1\t Error \n'); printf('%2d \t %11.7f \t %11.7f \t %11.7f \n',i,x0(i),xn(i),x1(i)); while abs(ea(i))>=aprox, if f(x0(i))*f(xn(i))< 0 x0(i+1)=x0(i); x1(i+1)=xn(i); end if f(x0(i))*f(xn(i))> 0 x0(i+1)=xn(i); x1(i+1)=x1(i); end xn(i+1)=x0(i+1)-f(x0(i+1))*(x1(i+1)-x0(i+1))/(f(x1(i+1))-f(x0(i+1))); ea(i+1)=abs((xn(i+1)-xn(i))/(xn(i+1))); printf('%2d \t %11.7f \t %11.7f \t %11.7f \t %7.7f \n', i+1,x0(i+1),xn(i+1),x1(i+1),ea(i+1)); i=i+1; end else printf(' '); end endfunction
Metode Newton-Raphson
Listing Scilab:
function y=f(x)
y=x^3 + 2*x^2 + 10*x - 20;
endfunction
function y=df(x)
y=3*x^2 + 4*x + 10;
endfunction
function xr=newtonraphson(f, x0, aprox);
i=1;
er(1)=1;
xr(1)=x0;
while abs(er(i))>=aprox;
xr(i+1)=xr(i)-f(xr(i))/df(xr(i));
er(i+1)=abs((xr(i+1)-xr(i))/xr(i+1));
i=i+1;
end
printf(' r \t xn(r) Error aprox (r) \n');
for j=1:i;
printf('%2d \t %11.7f \t %7.6f \n',j-1,xr(j),er(j));
end
endfunction
Metode Secant
Listing Scilab:
function y=g(x)
y=x^3 + 2*x^2 + 10*x - 20;
endfunction
function pn=metodesecant(x0, x1, aprox)
j=2;
i=1;
pn(1)=x0;
pn(2)=x1;
er(i)=1;
while abs(er(i))>=aprox
pn(j+1)=(pn(j-1)*f(pn(j))-pn(j)*f(pn(j-1)))/(f(pn(j))-f(pn(j-1)));
er(i+1)=abs((pn(j+1)-pn(j))/pn(j+1));
j=j+1;
i=i+1;
end
printf(' r \t pn(r)\t Error aprox (r) \n');
printf('%2d \t %11.7f \t\t \n',0,pn(1));
for k=2:j;
printf('%2d \t %11.7f \t %7.8f \n',k,pn(k),er(k-1));
end
endfunction
1 comments :
kak mksh ya
Post a Comment