function yout=secant1(f,x,numIters) % function yout=secant1(f,x,numIters) % Implements the secant method for f. The input x must be an interval % (two numbers) to start the algorithm. % The output, yout, is of the form: x0 x1 y1, so its size is numIters+1 by 3 % % Example: % f=inline('x^2-1'); % x=[0 2]; % numIters=6; % yout=secant1(f,x,numIters); % if length(x)~=2 error('There should be 2 x values (as a vector) input to secant1') end x0=x(1); x1=x(2); y0=f(x0); y1=f(x1); yout=zeros(numIters+1,3); yout(1,:)=[x0 x1 y1]; for j=1:numIters x2=(x1*y0-x0*y1)/(y0-y1); %Reset the values x0=x1; y0=y1; x1=x2; y1=f(x2); yout(j+1,:)=[x0 x1 y1]; end