%
%   Esercizio 1 - MILANO 16 FEBBRAIO 2017
%
clear all

f=inline('(x-1).*exp(x)','x');
g1=inline('x-1+1./x','x');
g1der=inline('x-1+1./x','x');
g2=inline('(exp(x)+x)./(exp(x)+1)','x');

x0=2; nmax=100; toll=1e-4;
[x1,n1]=steff(g1,f,x0,nmax,toll);
e_1=abs(g1(x1(end))-x1(end));

x0=2; nmax=100; toll=1e-4;
[x2,n2]=steff(g2,f,x0,nmax,toll);
e_2=abs(g2(x2(end))-x2(end));



fprintf('n_1=%2d  x_1=%10.8f  e_G1=%12.6e \n',n1,x1(end),e_1);
fprintf('n_2=%2d  x_2=%10.8f  e_G2=%12.6e \n',n2,x2(end),e_2);

%n_1= 5  x_1=1.00000198  e_G1=1.977844e-006 
%n_2= 3  x_2=1.00002580  e_G2=1.885953e-005 

%function [x,iter]=steff(g,f,x0,nmax,toll);
%x(1)=x0;  iter=1; test=1;
%while test>toll &iter<=nmax
%   iter=iter+1;
%   xk=x(iter-1);
%   gk=g(xk);
%   rapp_num=(gk-xk)^2;
%   rapp_den=g(gk)-2*gk+xk;
%   rapp=rapp_num/rapp_den;
%   x(iter)=x(iter-1)-rapp;
%   test=abs(f(x(iter)));
%   end