% matlab script to show the usage of fzero

% some of the different ways to call fzero
% in every case, we will solve for the cube root of 2
% using the equation f(x) = x^3 - 2 

% we can simply input one point as an initial guess
% and fzero will find a bracket
x0 = 1;
x = fzero('x^3-2',x0)

% or we can input a bracket
x0 = [1,2];
x = fzero('x^3-2',x0)

% but no cheating!
%x0 = [0,1];
%x = fzero('x^3-2',x0)

% you can specify other behaviour besides default
options = optimset('Disp','iter','TolX',1e-2);
x0 = 1;
x = fzero('x^3-2',x0,options)

% fzero can return more information besides just the root
% indeed you should look at it!

[x,fVal,exitFlag] = fzero('x^3-2',x0)

% you can look at more output in a structure

[x,fVal,exitFlag,output.Structure] = fzero('x^3-2',x0);
output.Structure

% note: and you can do all of the above with
% "anonymous functions" ...
x = fzero(@(x) x^3-2,x0)

% ... and by defining the function in a separate routine
x = fzero('functionToZero',x0)
