0% found this document useful (0 votes)
20 views

Matlab Script To Find Root Using Secant Method

This MATLAB script implements the secant method to iteratively find the root of a user-defined function. The script initializes variables, inputs the function and initial guesses, then iteratively updates the next guess using the secant rule until the error tolerance is met or maximum iterations reached. It outputs iteration details and the calculated root.

Uploaded by

Ahmed Hosny
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Matlab Script To Find Root Using Secant Method

This MATLAB script implements the secant method to iteratively find the root of a user-defined function. The script initializes variables, inputs the function and initial guesses, then iteratively updates the next guess using the secant rule until the error tolerance is met or maximum iterations reached. It outputs iteration details and the calculated root.

Uploaded by

Ahmed Hosny
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Matlab Script to Find root using Secant Method

function [root, ea, iter, logTable] = secant_method()

% Ensure Symbolic Math Toolbox is available


assert(~isempty(ver('symbolic')), 'Symbolic Math Toolbox is required.');

% Initialize variables
validFunction = false;

while ~validFunction
% Step 0: Input the function
user_func = input('Enter the function in terms of x: ', 's');
syms x; % Define symbolic variable

try
func_sym = str2sym(user_func); % Convert string to symbolic expression
func = matlabFunction(func_sym); % Convert symbolic expression to function handle

% Check if the function has a valid derivative


xi_minus_1 = input('Enter the first initial guess (x0-1): ');
xi = input('Enter the second initial guess (x0): ');
f_xi_minus_1 = func(xi_minus_1);
f_xi = func(xi);
if isnan(f_xi_minus_1) || isnan(f_xi) || xi_minus_1 == xi
disp('Invalid initial guesses or function. Please enter valid inputs.');
else
validFunction = true;
end
catch
disp('Invalid function. Please enter a valid function.');
end
end

% Step 1: Initialize variables


tolerance = input('Enter the desired tolerance (es): '); % Desired tolerance
% Maximum number of iterations
maxIterations = input('Enter the maximum number of iterations (imax): ');

iter = 1;
ea = Inf; % Initial approximation error (set to infinity)
logTable = [];

fprintf('Iteration xi f(xi) f(xi-1) ea\n');

% Step 2: Secant Method iterations


while ea >= tolerance && iter < maxIterations
xi_plus_1 = xi - func(xi) * (xi - xi_minus_1) / (func(xi) - func(xi_minus_1)); % Update
rule

% Compute approximation error if xi+1 is not 0


if xi_plus_1 ~= 0
ea = abs((xi_plus_1 - xi) / xi_plus_1) * 100;
end
Matlab Script to Find root using Secant Method
% Log current iteration information
logTable = [logTable; iter - 1, xi, func(xi), func(xi_minus_1), ea];

% Display current iteration information


fprintf('%4d %.6f %.6f %.6f %.6f\n', iter - 1, xi, func(xi), func(xi_minus_1),
ea);

if ea < tolerance
break;
end

xi_minus_1 = xi;
xi = xi_plus_1; % Prepare for next iteration
iter = iter + 1;
end

root = xi;

end

1. Use the MATLAB implementation of the Secant method to find a root of the function 𝑓(𝑥) = 𝑥3 − 2𝑥2 − 6𝑥 + 4 = 0 with the
using the initial guesses x0 = -4.0 and x-1 = -3.0. Perform the computations until percentage approximate relative error is
less than 2%. You are required to fill the following table.

2. Repeat step 1 until percentage approximate relative error is less than 0.2%.
Matlab Script to Find root using Secant Method
3. Repeat step 3 using the initial guesses x0 = 2.0 and x-1 = 3.0

You might also like