% 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);