# Bisection method help.

조회 수: 6 (최근 30일)
Bryce McCord . 2020년 10월 21일
답변: Asad (Mehrzad) Khoddam . 2020년 10월 21일
function [f] = Bisection(a, b, Nmax, TOL);
f=@(x) x.*x.*x.-2;
i=1;
BisectA = f(a)
while i <= Nmax
p=a+(b-a)/2;
BisectP=f(p);
if BisectP == 0 || (b-a)/2 < TOL
disp('p');
endif
i = i+1;
if BisectA*BisectP > 0
a = p;
BisectA = BisectP;
else
b = p;
endif
end
fprintf('method failed after %d iterations\n', Nmax);
endfunction
After entering four numerical values this is the output I'm recieving.
Bisection(2, 4, 6, 8)
BisectA = 6
p
p
p
p
p
p
method failed after 6 iterations
ans =
@(x) x .* x .* x - 2
Is this correct?

댓글을 달려면 로그인하십시오.

### 답변 (1개)

function x = Bisection(a, b, Nmax, TOL)
f=@(x) x.*x.*x-2;
i=1;
ya = f(a);
while i <= Nmax
m=(a+b)/2;
ym=f(m);
if ym == 0 || (b-a)/2 < TOL
disp(m);
break;
end
i = i+1;
if ya*ym > 0
a = m;
ya = ym;
else
b = m;
end
end
if i==Nmax
fprintf('method failed after %d iterations\n', Nmax);
x=NaN;
else
x=m;
end

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by