Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function secant_method()
- f = @(x) x^2 - 9;
- eps = 1e-6;
- x0 = 1000; x1 = x0 - 1;
- [solution,no_iterations] = secant(f, x0, x1, eps);
- if no_iterations > 0 % Solution found
- fprintf('Number of function calls: %d\n', 2 + no_iterations);
- fprintf('A solution is: %f\n', solution)
- else
- fprintf('Abort execution.\n')
- end
- end
- function [solution,no_iterations] = secant(f, x0, x1, eps)
- f_x0 = f(x0);
- f_x1 = f(x1);
- iteration_counter = 0;
- while abs(f_x1) > eps && iteration_counter < 100
- try
- denominator = (f_x1 - f_x0)/(x1 - x0);
- x = x1 - (f_x1)/denominator;
- catch
- fprintf('Error! - denominator zero for x = \n', x1)
- break
- end
- x0 = x1;
- x1 = x;
- f_x0 = f_x1;
- f_x1 = f(x1);
- iteration_counter = iteration_counter + 1;
- end
- % Here, either a solution is found, or too many iterations
- if abs(f_x1) > eps
- iteration_counter = -1;
- end
- solution = x1;
- no_iterations = iteration_counter;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement