function p=secant_table(f,p0,p1,tol,max_iterations) % Secant method: See section 2.3. % This version prints the iterations in a table. % Use variables q0 and q1 to store f(p0) and f(p1). % This reduces the number of times we need to evaluate f. q0=f(p0); q1=f(p1); fprintf(' n | x1 | x2 | p_n\n'); fprintf('-----------------------------------------------\n'); for i=1:max_iterations % Store the last two iterations in p0 and p1. p = p1 - q1 * (p1-p0)/(q1-q0); q = f(p); fprintf('%2d | %+12.8f | %+12.8f | %+12.8f\n', i, p0, p1, p); if abs(p-p1)