MATLAB无约束优化
f='2*exp(-x).*sin(x)';
fplot(f,[0,8]); %作图语句
[xmin,ymin]=fminbnd (f, 0,8)
f1='-2*exp(-x).*sin(x)';
[xmax,ymax]=fminbnd (f1, 0,8)MATLAB
%%%%%%%%%%%%%%%%%%%%
function f = fun(x)
f=-((100-x(1)- 0.1*x(2))-(30*exp(-0.015*x(1))+20))*x(1)-((280-0.2*x(1)- 2*x(2))-(100*exp(-0.02*x(2))+30))*x(2)
clear
x0=[50,70];
x=fminunc('fun',x0)
z=-fun(x)
%%%%%%%%%%%%%%%%%%%%%%%%
[x,y]=meshgrid(-2:0.1:2,-1:0.1:3);
z=100*(y-x.^2).^2+(1-x).^2;
mesh(x,y,z)
contour(x,y,z,20)
drawnow %画出当前图形
hold on
plot(-1.2,2,' o ');
text(-1.2,2,'start point')
plot(1,1,'o')
text(1,1,'solution')
%%%%%%%%%%%%%%%%%%%%%%%%%%%
f='100*(x(2)-x(1)^2)^2+(1-x(1))^2';
[x,fval,exitflag,output]=fminsearch(f, [-1.2 2])
%%%%%%%%%%%%%%%%%%%%%%%%%%
命令格式为:
(1)x= fminunc(fun,X0 );或x=fminsearch(fun,X0 )
(2)x= fminunc(fun,X0 ,options);
或x=fminsearch(fun,X0 ,options)
(3)[x,fval]= fminunc(...);
或[x,fval]= fminsearch(...)
(4)[x,fval,exitflag]= fminunc(...);
或[x,fval,exitflag]= fminsearch
(5)[x,fval,exitflag,output]= fminunc(...);
或[x,fval,exitflag,output]= fminsearch(...)

浙公网安备 33010602011771号