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

Matlab Script To Find Root Using Newton's Raphson Method

The document describes a Matlab script that implements Newton's method to find the root of a function. The script takes a user-input function, computes its derivative, initializes variables including a starting guess, then iteratively computes updates until the error is below a tolerance or max iterations is reached, outputting the results.

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)
42 views

Matlab Script To Find Root Using Newton's Raphson Method

The document describes a Matlab script that implements Newton's method to find the root of a function. The script takes a user-input function, computes its derivative, initializes variables including a starting guess, then iteratively computes updates until the error is below a tolerance or max iterations is reached, outputting the results.

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/ 2

Matlab Script to Find root using Newton’s Raphson Method

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


% 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
func_deriv = matlabFunction(diff(func_sym, x)); % Derive and convert to function handle
validFunction = true;
catch
disp('Invalid function or derivative. Please enter a valid function.');
end
end
% Step 1: Input initial guess
xi = input('Enter the initial guess (x0): ');

% Step 2: Initialize variables


% Desired tolerance
tolerance = input('Enter the desired tolerance (es): ');
% Maximum number of iterations
maxIterations = input('Enter the maximum number of iterations (imax): ');
iter = 0;
ea = 100; % Initial approximation error (set to a large value)
logTable = [];
fprintf('Iteration xi f(xi) f''(xi) ea\n');

% Step 3: Newton-Raphson iterations


while ea >= tolerance && iter < maxIterations
xip1 = xi - func(xi)/func_deriv(xi); % Update rule
% Compute approximation error if xi+1 is not 0
if xip1 ~= 0
ea = abs((xip1 - xi) / xip1) * 100;
end
% Log current iteration information
logTable = [logTable; iter, xi, func(xi), func_deriv(xi), ea];
% Display current iteration information
fprintf('%4d %.6f %.6f %.6f %.6f\n', iter, xi, func(xi), func_deriv(xi), ea);

if ea < tolerance
break;
end

xi = xip1; % Prepare for the next iteration


iter = iter + 1;
end
root = xi;
end
Matlab Script to Find root using Newton’s Raphson Method
1. Use the MATLAB implementation of Newton-Raphson method to find a root of the function 𝑓(𝑥) = 𝑥3 − 2𝑥2 − 6𝑥 + 4 = 0
with the initial guess x0 = 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%.

3. Repeat step 1 using the initial guess x0 = -1.0.

You might also like