Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views2 pages

Matlab

The document defines a function and provides a graphical representation of it. It implements the bisection method and the false position method to find the root of the function within a specified interval, reporting the root and the number of iterations for each method. Both methods check for the existence of a root and apply iterative calculations until the stopping criteria are met.

Uploaded by

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

Matlab

The document defines a function and provides a graphical representation of it. It implements the bisection method and the false position method to find the root of the function within a specified interval, reporting the root and the number of iterations for each method. Both methods check for the existence of a root and apply iterative calculations until the stopping criteria are met.

Uploaded by

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

% Define the function

f = @(x) -12 - 21*x + 18*x^2 - 2.75*x^3;

% Graphical representation
x = linspace(-2, 2, 1000);
y = f(x);

figure;
plot(x, y);
xlabel('x');
ylabel('f(x)');
title('Graphical Representation of f(x)');

% Bisection method
xl = -1; % Lower guess
xu = 0; % Upper guess
tolerance = 0.01; % Stopping criterion

if f(xl) * f(xu) > 0


error('No root within the given interval');
end

maxIterations = 100;
xr = xl;
for iteration = 1:maxIterations
xr_previous = xr;
xr = (xl + xu) / 2;

if f(xr) == 0 || (xu - xl) / 2 < tolerance


break;
elseif f(xl) * f(xr) < 0
xu = xr;
else
xl = xr;
end
end

fprintf('Bisection Method:\n');
fprintf('Root = %.4f\n', xr);
fprintf('Iterations = %d\n\n', iteration);

% False position method


xl = -1; % Lower guess
xu = 0; % Upper guess

if f(xl) * f(xu) > 0


error('No root within the given interval');
end

xr = xl;
for iteration = 1:maxIterations
xr_previous = xr;
xr = xu - (f(xu) * (xl - xu)) / (f(xl) - f(xu));

if f(xr) == 0 || abs((xr - xr_previous) / xr) < tolerance


break;
elseif f(xl) * f(xr) < 0
xu = xr;
else
xl = xr;
end
end

fprintf('False Position Method:\n');


fprintf('Root = %.4f\n', xr);
fprintf('Iterations = %d\n', iteration);

You might also like