You are on page 1of 4

Name: PAOLO P.

VALDEZ Date: July 5, 2023


Course Code & Subject: CEM 602 – NUMERICAL METHODS FOR ENGINEERS

ROOTS OF EQUATION (BRACKETING METHOD)


Bisection Method
Given:
6 4
f (x)=5 x −2 x +10 x−2

Use bisection to determine the maximum of this function. Employ initial guesses of x l=0 and x u=1, and
perform iterations until the approximate relative error falls below 5%.

This problem performs the Bisection Method to find the maximum of a given function and then plots the
function along with the estimated maximum.

Solution Using Matlab:

% Function definition iterationTable(i, 1) = i;


f = @(x) 5*x^6 - 2*x^4 + 10*x - 2; iterationTable(i, 2) = xl;
iterationTable(i, 3) = xu;
% Initial guess interval iterationTable(i, 4) = xr;
xl = 0;
xu = 1; % Store previous iteration value
xprev = xr;
% Tolerance for relative error end
tol = 0.05; % 5% relative error
% Trim the iteration table to remove unused
% Maximum number of iterations rows
maxIterations = 1000; iterationTable = iterationTable(1:i, :);

% Initialize iteration table % Display the maximum value and location


iterationTable = zeros(maxIterations, 4); maxValue = f(xr);
fprintf('The maximum value of the function
% Perform bisection iterations is: %.4f\n', maxValue);
for i = 1:maxIterations fprintf('The location of the maximum is:
% Compute midpoint %.4f\n', xr);
xr = (xl + xu) / 2;
% Plotting the function and maximum
% Compute relative error x = linspace(0, 1, 1000);
if i > 1 y = arrayfun(f, x);
relativeError = abs((xr - xprev) /
xr); figure;
plot(x, y, 'b-', 'LineWidth', 2);
% Check convergence hold on;
if relativeError < tol plot(xr, maxValue, 'ro', 'MarkerSize', 8);
break; hold off;
end
end xlabel('x');
ylabel('f(x)');
% Update interval boundaries title('Bisection Method - Maximum of the
if f(xr) > 0 Function');
xu = xr; legend('Function', 'Maximum Estimate');
else
xl = xr; % Display the iteration table
end disp('Bisection Iteration Table:');
disp(iterationTable);
% Store iteration values in the table
6 4
f (x)=5 x −2 x +10 x−2
ROOTS OF EQUATION (BRACKETING METHOD)
False-Position Method
Given:
6 4
f (x)=5 x −2 x +10 x−2

Use false-position iterations to determine the maximum of this function. Employ initial guesses of x l=0 and
x u=1, and perform iterations until the approximate relative error falls below 5%.

This problem performs the False-Position Method to find the maximum of a given function and then plots the
function along with the estimated maximum.

Solution Using Matlab:

% Function definition else


f = @(x) 5*x^6 - 2*x^4 + 10*x - 2; xl = xr;
end
% Initial guess interval
xl = 0; % Store previous iteration value
xu = 1; xprev = xr;

% Tolerance for relative error % Print iteration information in a table


tol = 0.05; % 5% relative error format
fprintf('%d\t\t %.4f\t\t %.4f\t\t
% Maximum number of iterations %.4f\t\t %.4f\n', i, xl, xu, xr,
maxIterations = 1000; relativeError);
end
% Perform false-position iterations
fprintf('Iteration\t xl\t\t xu\t\t xr\ % Display the maximum value and location
t\t Relative Error\n'); maxValue = f(xr);
for i = 1:maxIterations fprintf('\nThe maximum value of the function
% Compute new approximation using false is: %.4f\n', maxValue);
position formula fprintf('The location of the maximum is:
xr = xu - (f(xu) * (xl - xu)) / (f(xl) - %.4f\n', xr);
f(xu));
% Plotting the function and maximum
% Compute relative error x = linspace(0, 1, 1000);
if i > 1 y = arrayfun(f, x);
relativeError = abs((xr - xprev) /
xr); figure;
plot(x, y, 'b-', 'LineWidth', 2);
% Check convergence hold on;
if relativeError < tol plot(xr, maxValue, 'ro', 'MarkerSize', 8);
break; hold off;
end
end xlabel('x');
ylabel('f(x)');
% Update interval boundaries based on title('False-Position Method - Maximum of the
function evaluations Function');
if f(xr) > 0 legend('Function', 'Maximum Estimate');
xu = xr;
START

Initialize:
xl = 0
xu = 1
i=1

Check if i >
max Iterations

Yes

Compute:
xr = xu - (f(xu) * (xl - xu)) / (f(xl) - f(xu))

Update:
if f(xr) > 0
Compute:
No xu = xr
Relative Error = |(xr - xprev) / xr|
else
xl = xr

Check if relative Store:


Error < tol xprev = xr

Yes

END

You might also like