15.11.2014 Views

MATLAB Mathematics - SERC - Index of

MATLAB Mathematics - SERC - Index of

MATLAB Mathematics - SERC - Index of

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Minimizing Functions and Finding Zeros<br />

Stopping an Optimization Based on Data in optimValues. The output function can stop<br />

an optimization at any iteration based on the current data in optimValues. For<br />

example, the following code sets stop to true if the objective function value is<br />

less than 5:<br />

function stop = myoutput(x, optimValues, state)<br />

stop = false;<br />

% Check if objective function is less than 5.<br />

if optimValues.fval < 5<br />

stop = true;<br />

end<br />

Stopping an Optimization Based on GUI Input. If you design a GUI to perform<br />

optimizations, you can make the output function stop an optimization when a<br />

user clicks a Stop button on the GUI. The following code shows how to do this,<br />

assuming that the Stop button callback stores the value true in the optimstop<br />

field <strong>of</strong> a handles structure called hObject stored in appdata.<br />

function stop = myoutput(x, optimValues, state)<br />

stop = false;<br />

% Check if user has requested to stop the optimization.<br />

stop = getappdata(hObject,'optimstop');<br />

Finding Zeros <strong>of</strong> Functions<br />

The fzero function attempts to find a zero <strong>of</strong> one equation with one variable.<br />

You can call this function with either a one-element starting point or a<br />

two-element vector that designates a starting interval. If you give fzero a<br />

starting point x0, fzero first searches for an interval around this point where<br />

the function changes sign. If the interval is found, fzero returns a value near<br />

where the function changes sign. If no such interval is found, fzero returns<br />

NaN. Alternatively, if you know two points where the function value differs in<br />

sign, you can specify this starting interval using a two-element vector; fzero is<br />

guaranteed to narrow down the interval and return a value near a sign change.<br />

The following sections contain two examples that illustrate how to find a zero<br />

<strong>of</strong> a function using a starting interval and a starting point. The examples use<br />

the function humps, which is provided with <strong>MATLAB</strong>. The following figure<br />

shows the graph <strong>of</strong> humps.<br />

4-21

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!