How to find first negative solution with the bisection method

3 vues (au cours des 30 derniers jours)
ken
ken le 9 Mai 2022
f(x) = x + 1 −2 sin(πx) = 0
How to find first negative solution with the given bisection method
function [c, n, err] = Bisection_method(g, a, b, tol, N)
c = [];
n = 0;
err = inf;
FA = g(a);
FB = g(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 = g(c);
err = abs(fmid);
if(fmid * g(a) > 0)
a = c;
else
b = c;
end
end
end
end

Réponses (1)

Chunru
Chunru le 9 Mai 2022
g = @(x) x + 1 -2 * sin(pi*x) ;
% Use interval [-1.5, 0] for example
[c, n, err] = Bisection_method(g, -1.5, 0, 1e-6, 1000)
c = -1.0000
n = 22
err = 8.6822e-07
function [c, n, err] = Bisection_method(g, a, b, tol, N)
c = [];
n = 0;
err = inf;
FA = g(a);
FB = g(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 = g(c);
err = abs(fmid);
if(fmid * g(a) > 0)
a = c;
else
b = c;
end
end
end
end

Catégories

En savoir plus sur Numerical Integration and Differential Equations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by