% Root finding using Secant method % function y = fcn(x) y = x^4 - 5*x^3 - 124*x^2 + 380*x + 1200; end % proceed with finding a root: x1 = 0; % 'Enter first guess of root location; x2 = 20; % 'Enter second guess of root location; itermax = 100; % max # of iterations iter = 0; errmax = 0.001; % convergence tolerance error = 1; write('x1 ','x2 ','x3 ','rel error'); while error > errmax & iter < itermax iter = iter + 1; f1 = fcn(x1); f2 = fcn(x2); x3 = x2 - f2*(x2-x1) / (f2 - f1); % here is new root estimate error = abs((x3 - x2)/x3) * 100; % find change from previous write(x1,' ',x2,' ',x3,' ',error); x1 = x2; % set up for next iteration x2 = x3; end