Page 302 - MATLAB an introduction with applications
P. 302
Optimization ——— 287
>> [x0_u,f0_u]=fminunc(f,x0) % MATLAB built-in fminunc
Warning: Gradient must be provided for trust-region method;
using line-search method instead.
> In fminunc at 243
Maximum number of function evaluations exceeded;
increase options.MaxFunEvals
x0_u =
1.4311 0.6599
f0_u =
0.3637
>> [fc_u,f0_u,c0_u]=f321p(x0_u) % its results
fc_u =
0.3637
f0_u =
0.3637
c0_u =
–1.4311
–0.6599
–0.0114
–1.4778
–0.0880
function [fc,f,c]=f321p(x)
f=((x(1)+2)^2+5*(x(2)–2)^2)*((x(1)–1.5)^2+0.5*(x(2)–0.5)^2);
c=[–x(1); –x(2); 3*x(1)–x(1)*x(2)+4*x(2)–6;2*x(1)+x(2)–5;
3*x(1)–4*x(2)^2–4*x(2)]; % Constraint vector
v=[1 1 1 1 1];e=[1 1 1 1 1]’;% Weighting coefficient vector
fc=f+v*((c>0).*exp(e.*c)); % New objective function
function [xo,fo]=opt_Nelder(f,x0,TolX,TolFun,MaxIter)
N=length(x0);
if N==1 %for 1-dimensional case
[xo,fo]=opt_quad(f,x0,TolX,TolFun); return
end
S= eye(N);
for i=1:N %repeat the procedure for each subplane
i1=i+1; if i1>N, i1=1; end
abc=[x0; x0+S(i,:); x0+S(i1,:)]; %each directional subplane
fabc=[feval(f,abc(1,:)); feval(f,abc(2,:)); feval(f,abc(3,:))];
[x0,fo]=Nelder0(f,abc,fabc,TolX,TolFun,MaxIter);
if N<3, break; end %No repetition needed for a 2-dimensional case
end
xo=x0;