Bisection method help.

2 Ansichten (letzte 30 Tage)
Bryce McCord
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?

Antworten (1)

Asad (Mehrzad) Khoddam
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

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by