How to find first negative solution with the bisection method

3 Ansichten (letzte 30 Tage)
ken
ken am 9 Mai 2022
Beantwortet: Chunru am 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

Antworten (1)

Chunru
Chunru am 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

Kategorien

Mehr zu Numerical Integration and Differential Equations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by