# Bisection method help.

7 Ansichten (letzte 30 Tage)
Bryce McCord am 21 Okt. 2020
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?
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

Asad (Mehrzad) Khoddam am 21 Okt. 2020
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
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Find more on Legend in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by