Code For Secant Method
Code For Secant Method
MATLAB #2
% Get the function from the user as a string and convert it to a symbolic expression
func_str = input('Enter the function f(x) as a function of x (e.g., x^3 - 6*x^2 + 11*x - 9 ): ', 's');
f_sym = str2sym(func_str); % Convert input string to symbolic expression
f = matlabFunction(f_sym); % Convert symbolic expression to function handle
% Pause for a few seconds to allow the user to see the root found message
pause(3); % Pauses for 3 seconds; you can adjust the duration
clc;
clear;
% Get the function from the user as a string and convert it to a symbolic expression
func_str = input('Enter the function f(x) as a function of x (e.g., x^3 - 6*x^2 + 11*x - 6): ', 's');
f_sym = str2sym(func_str); % Convert input string to symbolic expression
f = matlabFunction(f_sym); % Convert symbolic expression to function handle
x1 = x0 - f(x0) / derivative_value;
if abs(x1 - x0) < tol
fprintf('Root found at x = %.6f\n', x1);
break;
end
x0 = x1;
end