% Finding Local Minima for a Function
% In Matlab, the fminbnd function is used to minimize functions. To use fminbnd to minimize the function y=1-x+x2 on the interval 0<x<3, we can use the following script:

ezplot('1-x+x^2',[0 1])
set(gca,'FontSize',20)
[xm, fm]=fminbnd('1-x+x^2',0,3)

% Alternatively, we could place the function in an m-file and just call it. The function would be:

[xm,fm]=fminbnd(@f,0,3)

%

function func=f(x)
    func=1-x+x^2;
end

% This code searches for a minimum in the range 0<x<3. Note that this will only find one local minimum on this interval. It does not attempt to find all local minima, or even the global minimum.
% © James Blanchard, University of Wisconsin-Madison, blanchard@engr.wisc.edu