KEMBAR78
Code For Root Finding Methods | PDF | Algorithms And Data Structures | Mathematical Relations
0% found this document useful (0 votes)
5 views4 pages

Code For Root Finding Methods

The document contains MATLAB functions for various root-finding methods including the bisection method, false position method, secant method, and Newton's method. Each function iteratively approximates the root of a predefined function based on specified parameters and conditions. The methods utilize different approaches for convergence and error checking to ensure accurate root approximations.

Uploaded by

Darnell Vuong
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)
5 views4 pages

Code For Root Finding Methods

The document contains MATLAB functions for various root-finding methods including the bisection method, false position method, secant method, and Newton's method. Each function iteratively approximates the root of a predefined function based on specified parameters and conditions. The methods utilize different approaches for convergence and error checking to ensure accurate root approximations.

Uploaded by

Darnell Vuong
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/ 4

% Function for bisection method

function [c, fc] = bisect(a, b, n)


% Evaluate the function at the left endpoint of the interval
fa = f(a);

% Perform n iterations to approximate the root


for i = 1:n
% Compute the midpoint of the interval
c = (a + b) / 2;
% Evaluate the function at the midpoint
fc = f(c);

% Check if the exact root is found


if fc == 0
return;
elseif fa * fc < 0 % Root is in the left subinterval
% Update the right endpoint
b = c;
else % Root is in the right subinterval
% Update the left endpoint
a = c;
fa = fc; % Update function value at a
end
end

% Compute the final approximation of the root after n iterations


c = (a + b) / 2;
fc = f(c);
end
% Function for false position method
function [c, fc] = false_position(a, b, n)
% Evaluate the function at the initial interval endpoints
fa = f(a); % Assume f is a predefined function
fb = f(b);

% Perform n iterations to approximate the root


for i = 1:n
% Compute the false position (Regula Falsi) formula
c = double(b - fb * (b - a) / (fb - fa));
fc = double(f(c));

% Check if we have found the exact root


if fc == 0 % If root is found, exit early
return;
elseif fa * fc < 0 % Root is in the left subinterval
% Update the right endpoint
b = c;
fb = fc; % Update function value at b
else % Root is in the right subinterval
% Update the left endpoint
a = c;
fa = fc; % Update function value at a
end
end

% Compute the final approximation of the root after n iterations


c = double(b - fb * (b - a) / (fb - fa));
fc = double(f(c));
end
% Function for secant method
function [x2, f2] = secant(f, x0, x1, p)
% Evaluate the function at the initial points
f0 = double(f(x0));
f1 = double(f(x1));

% Initialize relative error to ensure the loop runs at least once


re = 1;

% Iterate until the relative error is within the desired tolerance


while re >= (0.5) * 10^(-p)
% Compute the next approximation using the secant method formula
x2 = x1 - f1 * (x1 - x0) / (f1 - f0);

% Compute the relative error to check for convergence


re = abs((x2 - x1) / x2);

% Update values for the next iteration


x0 = x1; % Move x1 to x0
f0 = f1; % Move f1 to f0
x1 = x2; % Move x2 to x1
f1 = double(f(x1)); % Recalculate function value at new x1
end

% Store the final approximated root and its function value


f2 = f1;
end

% Function for Newton method


function [xr, fr] = newton(f, x0, p)
% Evaluate the function at the initial guess
f0 = double(f(x0));

% Compute the derivative of the function


g = diff(f);

% Initialize relative error to ensure the loop runs at least once


re = 1;

% Perform iterations until the desired precision is reached


while re >= (0.5) * 10^(-p)
% Store the old approximation
x_old = x0;

% Compute the next approximation using Newton's method formula


x0 = double(x0 - f(x0) / g(x0));

% Compute the relative error to check for convergence


re = abs((x0 - x_old) / x0);

% Update function value at new x0


f0 = double(f(x0));
end

% Store the final root approximation and function value


xr = x0;
fr = double(f(x0));
end

You might also like