How to find first negative solution with the bisection method

조회 수: 3 (최근 30일)
ken
ken 2022년 5월 9일
답변: Chunru 2022년 5월 9일
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