How to find first negative solution with the bisection method
1 次查看(过去 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
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)
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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!