MATLAB Mathematics - SERC - Index of

MATLAB Mathematics - SERC - Index of MATLAB Mathematics - SERC - Index of

serc.iisc.ernet.in
from serc.iisc.ernet.in More from this publisher
15.11.2014 Views

4 Function Functions end function stop = myoutput(x,optimvalues,state); stop = false; if state == 'iter' history = [history; x]; end end function z = objfun(x) z = exp(x(1))*(4*x(1)^2+2*x(2)^2+x(1)*x(2)+2*x(2)); end 3 Save the file as myproblem.m in a directory on the MATLAB path. 4 At the MATLAB prompt, enter [x fval history] = myproblem([-1 1]) The function fminsearch returns x, the optimal point, and fval, the value of the objective function at x. x = 0.1290 -0.5323 fval = -0.5689 In addition, the output function myoutput returns the matrix history, which contains the points generated by the algorithm at each iteration, to the MATLAB workspace. The first four rows of history are history(1:4,:) ans = -1.0000 1.0000 -1.0000 1.0500 -1.0750 0.9000 -1.0125 0.8500 4-18

Minimizing Functions and Finding Zeros The final row of points is the same as the optimal point, x. history(end,:) ans = 0.1290 -0.5323 objfun(history(end,:)) ans = -0.5689 Fields in optimValues The following table lists the fields of the optimValues structure that are provided by all three optimization functions, fminbnd, fminsearch, and fzero. The function fzero also provides additional fields that are described in its reference page. The “Command-Line Display Headings” column of the table lists the headings, corresponding to the optimValues fields that are displayed at the command line when you set the Display parameter of options to 'iter'. optimValues Field (optimValues.field) funcount fval Description Cumulative number of function evaluations. Function value at current point. Command-Line Display Heading Func-count min f(x) iteration Iteration number — starts at 0. Iteration procedure Procedure messages Procedure 4-19

4 Function Functions<br />

end<br />

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

stop = false;<br />

if state == 'iter'<br />

history = [history; x];<br />

end<br />

end<br />

function z = objfun(x)<br />

z = exp(x(1))*(4*x(1)^2+2*x(2)^2+x(1)*x(2)+2*x(2));<br />

end<br />

3 Save the file as myproblem.m in a directory on the <strong>MATLAB</strong> path.<br />

4 At the <strong>MATLAB</strong> prompt, enter<br />

[x fval history] = myproblem([-1 1])<br />

The function fminsearch returns x, the optimal point, and fval, the value <strong>of</strong><br />

the objective function at x.<br />

x =<br />

0.1290 -0.5323<br />

fval =<br />

-0.5689<br />

In addition, the output function myoutput returns the matrix history, which<br />

contains the points generated by the algorithm at each iteration, to the<br />

<strong>MATLAB</strong> workspace. The first four rows <strong>of</strong> history are<br />

history(1:4,:)<br />

ans =<br />

-1.0000 1.0000<br />

-1.0000 1.0500<br />

-1.0750 0.9000<br />

-1.0125 0.8500<br />

4-18

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

Saved successfully!

Ooh no, something went wrong!