MATLAB: New to MATLAB – trying to write bisection method

bisectionfunctions

Hello, I'm brand new to MATLAB and am trying to understand functions and scripts, and write the bisection method based on an algorithm from our textbook. However, I'm running into problems. Could anyone help me please?
Here is my code:
function [f] = Bisection(a,b,Nmax,TOL)
f = x^3 - x^2 + x;
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');
end
i=i+1;
if BisectA*BisectP > 0
a=p;
BisectA=BisectP;
else
b=p;
end
end
disp('Method failed after num2str(Nmax) iterations, Nmax=', Nmax);
Thanks.

Best Answer

Your line
f = x^3 - x^2 + x;
does not define a function. Try
f = @(x) x^3 - x^2 + x;
Related Question