Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Use the bisection method to approximate the first negative solution, the negative root that is closest to the origin. The accuracy must be of the order 10−4.

1 visualización (últimos 30 días)
the given bisection code. and the given equation()
function [c, n, err] = bisection_method(f, a, b, tol, N)
c = [];
n = 0;
err = inf;
FA = f(a);
FB = f(b);
if(a > b)
err = inf;
c = [];
elseif (FA*FB>= 0)
else
while ((abs(err) > abs(tol)) && (n <= N))
n = n+1;
c = (a + b) / 2;
fmid = f(c);
err = abs(fmid);
if(fmid * f(a) > 0)
a = c;
else
b = c;
end
end
end
end

Respuestas (1)

Nivedita
Nivedita el 14 de Dic. de 2023
Hi Ken,
I understand that you are trying to find the first negative solution using the bisection method, which is closest to the origin and has an accuracy of the order 10^(-4). We need to identify an interval [a, b] that brackets this root in a manner that "f(a)*f(b)<0". I have considered the interval [-1.5, -0.5] as the starting point for the bisection method.
Here is an updated version of the code to do the same:
%% Calling the function within the interval [-1.5, 0.5]
% Define the function f(x)
f = @(x) x + 1 - 2 * sin(pi * x);
% Define the initial interval [a, b]
a = -1.5;
b = -0.5;
% Define the tolerance
tol = 1e-4;
% Define the maximum number of iterations
N = 1000;
% Call the bisection_method function
[c, n, err] = bisection_method(f, a, b, tol, N);
% Display the result
if isnan(c)
fprintf('The root was not found within the specified tolerance and iteration limit.\n');
else
fprintf('The root is approximately at x = %.4f after %d iterations with an error of %.4f\n', c, n, err);
end
The root is approximately at x = -1.0001 after 14 iterations with an error of 0.0001
%% Function to find the first negative root using the bisection method
function [c, n, err] = bisection_method(f, a, b, tol, N)
c = (a + b) / 2; % Initial midpoint
n = 0; % Iteration counter
err = inf; % Initial error
FA = f(a);
FB = f(b);
% Check if the interval is valid
if (FA * FB > 0)
c = NaN; % Return NaN to indicate the root is not bracketed
return;
end
% Bisection method loop
while ((b - a) > tol) && (n < N)
n = n + 1;
c = (a + b) / 2;
FC = f(c);
if FC == 0
% c is a root
err = 0;
break;
elseif (FA * FC < 0)
b = c;
FB = FC;
else
a = c;
FA = FC;
end
err = b - a; % Update the error estimate
end
if (n == N) && (err > tol)
c = NaN; % Set c to NaN to indicate the root was not found within tolerance
end
end
I hope this helps!
Regards,
Nivedita.

La pregunta está cerrada.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by