0% found this document useful (0 votes)
18 views6 pages

Code For Secant Method

The document contains MATLAB code for implementing the Secant and Newton's methods for root-finding of functions. It guides users through inputting a function, initial guesses, and tolerance, and includes plotting the function along with the identified root. Examples and graphs for both methods are also provided.

Uploaded by

damslambino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

Code For Secant Method

The document contains MATLAB code for implementing the Secant and Newton's methods for root-finding of functions. It guides users through inputting a function, initial guesses, and tolerance, and includes plotting the function along with the identified root. Examples and graphs for both methods are also provided.

Uploaded by

damslambino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

EARL WAYNE JAMES CORPUZ BSEE – 3A

FELIX DAMIAN LAMBINO


KYLE ANDRE MENDOZA

MATLAB #2

CODE FOR SECANT AND NEWTONS METHOD

A) CODE FOR SECANT METHOD

% Secant Method Root-Finding Code


clc;
clearvars;

% 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

% Get initial guesses and tolerance from the user


x0 = input('Enter initial guess x0: ');
x1 = input('Enter initial guess x1: ');
tol = input('Enter tolerance: ');

% Secant Method iteration


while true
x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0));
if abs(x2 - x1) < tol
fprintf('Root found at x = %.6f\n', x2);
break;
end
x0 = x1;
x1 = x2;
end

% 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

% Plotting the function and the identified root


x_vals = linspace(x0 - 2, x1 + 2, 100); % Define a range around guesses
y_vals = f(x_vals);
figure;
plot(x_vals, y_vals, 'b-', 'LineWidth', 1.5);
hold on;
plot(x2, f(x2), 'ro', 'MarkerSize', 8); % Plot the root found
xlabel('x');
ylabel('f(x)');
title('Secant Method: Function Plot with Root');
grid on;
hold off;
EXAMPLE USING SECANT METHOD
GRAPH OF THE SECANT METHOD
B) CODE FOR NEWTON’S METHOD

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

% Calculate the derivative of the function automatically


df_sym = diff(f_sym); % Calculate derivative symbolically
df = matlabFunction(df_sym); % Convert derivative to function handle

% Get initial guess and tolerance from the user


x0 = input('Enter initial guess x0: ');
tol = input('Enter tolerance: ');

% Newton-Raphson Method iteration


while true
derivative_value = df(x0);
if abs(derivative_value) < eps % Check for near-zero derivative
error('Derivative is zero or near-zero; choose a different initial guess.');
end

x1 = x0 - f(x0) / derivative_value;
if abs(x1 - x0) < tol
fprintf('Root found at x = %.6f\n', x1);
break;
end
x0 = x1;
end

% Pause before showing the graph


pause(2); % Pause for 2 seconds (adjust as needed)

% Plotting the function and the identified root


x_vals = linspace(x0 - 2, x0 + 2, 100); % Define a range around the guess
y_vals = f(x_vals);
figure;
plot(x_vals, y_vals, 'b-', 'LineWidth', 1.5);
hold on;
plot(x1, f(x1), 'ro', 'MarkerSize', 8); % Plot the root found
xlabel('x');
ylabel('f(x)');
title('Newton-Raphson Method: Function Plot with Root');
grid on;
hold off;
EXAMPLE USING NEWTON’S METHOD
GRAPH OF THE NEWTONS METHOD

You might also like