clear, clc, close all f=@(x)x.^3-2*x.^2+1; a=0;b=-1;N=4; xx=linspace(a,b); fa=f(a); fb=f(b); x=b-f(b)*(b-a)/(f(b)-f(a)); fx=f(x); for n=1:N figure(1) clf hold on plot(xx,f(xx)) plot([-1 0],[0,0],'k-') set(gca,'XTick',[],'YTick',[]) box on title('Secante','Fontsize',16,'Fontweight','b') if f(a)*f(b)<0 plot([a b],[f(a) f(b)],'r-','linewidth',2) else if abs(f(a))>abs(f(b)) plot([a b-f(b)*(b-a)/(f(b)-f(a))],[f(a) 0],'r-','linewidth',2) else plot([b b-f(b)*(b-a)/(f(b)-f(a))],[f(b) 0],'r-','linewidth',2) end end plot([a a],[0 f(a)],'b-','linewidth',2) text(a+0.01,0.1,['x_',num2str(n-1)],'Fontsize',16,'Fontweight','b') plot([b b],[0 f(b)],'b-','linewidth',2) text(b+0.01,0.1,['x_',num2str(n)],'Fontsize',16,'Fontweight','b') pause x=b-f(b)*(b-a)/(f(b)-f(a)); fx=f(x); plot([x x],[0 f(x)],'b-','linewidth',2) text(x+0.01,0.1,['x_',num2str(n+1)],'Fontsize',16,'Fontweight','b') pause a=b;b=x; end