1. METODE BAGI DUA:
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
_____________________________________
caranya:
-execute
-ketik: metodebagidua(f, 1, 1.5, 0.000001)
*ket: 1 ama 1.5 itu selangnya, 0.000001 itu epsilon.
2. METODE REGULA-FALSI:
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
_____________________________________
caranya:
-execute
-ketik: regulafalsi(f, 1, 1.5, 0.000001)
*ket: 1 ama 1.5 itu selangnya, 0.000001 itu epsilon.
3. METODE NEWTON-RAPHSON:
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(' i \t xn(i) Error aprox (i) \n');
for j=1:i;
printf('%2d \t %11.7f \t %7.6f \n',j-1,xr(j),er(j));
end
endfunction
_____________________________________
caranya:
-execute
-ketik: newtonraphson(f, 1, 0.000001)
*ket: 1 = x0, 0.000001 = epsilon.
4. METODE SECANT:
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(' i \t\t pn(i) \t\t Error aprox (i) \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
_____________________________________
caranya:
-execute
-ketik: metodesecant(1, 1.5, 0.000001)
*ket: 1 = x0, 1.5 = x1, 0.000001= epsilon.
Lucky Club - Casino site, bonuses, promotions and news
BalasHapusLucky Club. Enjoy top casino games. Play slots at Lucky Club Casino and enjoy exclusive bonuses and promotions! No download or registration required! Rating: 4.8 · luckyclub Review by Lucky Club