How to find first negative solution with the bisection method

3 次查看(过去 30 天)
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

回答(1 个)

Chunru
Chunru 2022-5-9
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

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by