Bisection method help.

1 vue (au cours des 30 derniers jours)
Bryce McCord
Bryce McCord le 21 Oct 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?

Réponses (1)

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

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by