% secant method example

clear

f=inline('sin(x)'); % try sin(x).^2 to see how it changes outcome

x0=3.1; % try other starting value that are farther away from pi
x1=3.2;
count=0;

while abs(x0-x1)>1e-8 && count<100 % iterate until values are very close
    x2=x1-f(x1)*(x1-x0)/(f(x1)-f(x0));
    x0=x1;
    x1=x2;
    count=count+1;
end

disp(['Iterations: ' num2str(count)])
disp(['Estimate: ' num2str(x1,'%0.12f')])
disp(['Actual:   ' num2str(pi,'%0.12f')])
