Bisection method help.
3 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Asad (Mehrzad) Khoddam
on 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
0 Comments
See Also
Categories
Find more on Language Support 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!