You are on page 1of 1

format long

ops=0.001; eps1=0.0001;
x0=0; x1=1; x2=2;
for i=1 : 5
f0=x0^3+2*x0^2+10*x0-20;
f1=x1^3+2*x1^2+10*x1-20;
f2=x2^3+2*x2^2+10*x2-20;

f10 = (f1-f0)/(x1-x0);
f21 = (f2-f1)/(x2-x1);
f210=(f21-f10)/(x2-x0);

a2=f210;
a1=f21 - (x2+x1)*a2;
a0=f2-x2*(f21-x1*a2);

d1=-a1+(a1^2-4*a0*a1)^0.5;
d2=-a1-(a1^2-4*a0*a1)^0.5;

if abs(d1)>abs(d2)
x3=2*a0/d1;
else
x3=2*a0/d2;
end

f3=x3^3+2*x3^2+10*x3-20;
dist=abs(x3-x2);
disp=([x3,dist])

if (dist<eps);
break
else
x0=x1; x1=x2; x2=x3;
end
end

You might also like