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

Matlab Script To Find Root Using Fixed Point Iteration Method

The document describes a Matlab script that uses the fixed point iteration method to find roots of functions. It defines variables, checks that the function and fixed point function are valid, runs the fixed point iteration until error or iterations are reached, and logs the results. It also provides examples of using the script to find roots of cos(x)-x with different initial guesses and tolerances.

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

Matlab Script To Find Root Using Fixed Point Iteration Method

The document describes a Matlab script that uses the fixed point iteration method to find roots of functions. It defines variables, checks that the function and fixed point function are valid, runs the fixed point iteration until error or iterations are reached, and logs the results. It also provides examples of using the script to find roots of cos(x)-x with different initial guesses and tolerances.

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 Fixed Point Iteration Method

function [root, ea, iter, logTable] = fixed_point_iteration_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 = input('Enter the initial guess (x0): ');
g_func = input('Enter the fixed-point function g(x): ', 's');
g = str2func(['@(x)', g_func]); % Convert g(x) string to function handle

if abs(g(xi) - xi) >= 1


disp('Invalid fixed-point function. Please enter a valid g(x) such that |g(x) - x|
< 1.');
else
validFunction = true;
end
catch
disp('Invalid function or fixed-point function. Please enter valid inputs.');
end
end

% Step 1: Initialize variables


tolerance = input('Enter the desired tolerance (es): '); % Desired tolerance
maxIterations = input('Enter the maximum number of iterations (imax): '); % Maximum number of
iterations
iter = 1;
ea = Inf; % Initial approximation error (set to infinity)
logTable = [];

fprintf('Iteration xi g(xi) ea\n');

% Step 2: Fixed-Point Iteration iterations


while ea >= tolerance && iter <= maxIterations
xiplus1 = g(xi); % Compute next iteration using g(xi)

% Compute approximation error if xi+1 is not 0


if xiplus1 ~= 0
ea = abs((xiplus1 - xi) / xiplus1) * 100;
end

% Log current iteration information


Matlab Script to Find root using Fixed Point Iteration Method
logTable = [logTable; iter - 1, xi, xiplus1, ea];

% Display current iteration information


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

if ea < tolerance || iter >= maxIterations


break;
end

xi = xiplus1; % Prepare for next iteration


iter = iter + 1;
end

root = xi;

end

1. Use the MATLAB implementation of fixed-point method to find a root of the function 𝑓(𝑥) = cos(𝑥) − 𝑥 when the initial
guess x0 = 0 and 𝑔(𝑥) = cos(𝑥). Perform the computations until percentage approximate relative error (a(%)) is less than
εs=5%. You are required to fill the following table.

2. Find a root of the function 𝑓(𝑥) = cos(𝑥) − 𝑥 when the initial guess x0 = 0 and . You need to modify the
MATLAB code of gfunc and dgfunc accordingly. Perform the computations until percentage approximate relative error is
less than εs=1%. You are required to fill the following table.

You might also like