Page 315 - MATLAB an introduction with applications
P. 315
300 ——— MATLAB: An Introduction with Applications
Consider the original unconstrained problem solved first
Minimize f(x)= exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+0.9
This time we will solve it more accurately by overriding the default
termination criteria (options.TolX and options.TolFun).
Create an anonymous function of the objective to be minimized:
>> fun=@(x) exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+0.9);
>> fun
fun =
@(x) exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+0.9)
Make a guess at the solution as
>> x0=[–1,1];
Set the optimization options: turn off the large-scale algorithms (the
default):
>> options=optimset(‘LargeScale’, ‘off’);
Override the default termination criteria:
% Termination tolerances on X and f.
>> options=optimset(options, ‘TolX’,1e–3, ‘TolFun’,1e–3);
Call the optimization algorithm:
>> [x,fval,exitflag,output]=fminunc(fun,x0,options);
Optimization terminated: relative infinity-norm of gradient less than
options.TolFun.
The optimizer has found a solution at
>> x
x =
0.5246 –1.0248
The function value at the solution is
>> fval
fval =
–0.1669
The total number of function evaluations was
>> output.funcCount
ans =
48
Set optimization options: turn off the large scale algorithms (the default):
>> options=optimset(options,‘Display’,‘iter’);
>> [x,fval,exitflag,output]=fminunc(fun,x0,options);