matlab中fmincon函数的使用

fmincon是用于求解非线性多元函数最小值的matlab函数。其语法格式比较多,参数也是各有各的意思,我就举几个例子,大家可从代码中去体会其使用方法。

  • 示例一

%% 只是大概写个板式,这里的fun2和fun3要自己去定义,这里就不写出来了,前面
%有介绍过
clc
clear
A=[3 2 1;1 2 3; 2 0 0;0 3 0;0 0 2]
b=[120;80;96;102;40]
lb=zeros(3,1)
x0=[1;1;1]
[X fval]=fmincon(@fun3,x0,A,b,[],[],lb,[])
x0=[1;1];
 A=[2 3 ;1 4]; b=[6;5];
 Aeq=[];beq=[];
 VLB=[0;0]; VUB=[];
 [x,fval]=fmincon('fun2',x0,A,b,Aeq,beq,VLB,VUB)
  • 示例二
%%开始采用默认设置
options = optimoptions('fmincon');
%% 修改参数设置
options = optimoptions(options,'Display', 'off');
options = optimoptions(options,'Algorithm', 'sqp');
options = optimoptions(options,'GradObj', 'on');
options = optimoptions(options,'GradConstr', 'on');
[x,fval,exitflag,output,lambda,grad,hessian] =fmincon(@fit1,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);

示例三

%% Start with the default options
options = optimoptions('fmincon');
%% Modify options setting
options = optimoptions(options,'Display', 'off');
options = optimoptions(options,'Algorithm', 'sqp');
[x,fval,exitflag,output,lambda,grad,hessian] = ...
fmincon(@fun3,x0,Aineq,bineq,[],[],lb,[],[],options);
  • 示例四
%% Start with the default options
options = optimoptions('fmincon');
%% Modify options setting
options = optimoptions(options,'Display', 'off');
options = optimoptions(options,'Algorithm', 'sqp');
options = optimoptions(options,'GradConstr', 'on');
[x,fval,exitflag,output,lambda,grad,hessian] = ...
fmincon(@fun3,x0,Aineq,bineq,[],[],lb,[],[],options);

  • 示例五

%%开始采用默认设置
options = optimoptions('fmincon');
%% 修改参数设置
options = optimoptions(options,'Display', 'off');
options = optimoptions(options,'Algorithm', 'sqp');
options = optimoptions(options,'GradObj', 'on');
options = optimoptions(options,'GradConstr', 'on');
[x,fval,exitflag,output,lambda,grad,hessian] = ...
fmincon(@fun3,x0,Aineq,bineq,[],[],lb,[],[],options);

  • 示例六
function [x,fval,exitflag,output,lambda,grad,hessian] = fmincon代码(x0,Aineq,bineq)
%% This is an auto generated MATLAB file from Optimization Tool.

%% Start with the default options
options = optimoptions('fmincon');
%% Modify options setting
options = optimoptions(options,'Display', 'off');
options = optimoptions(options,'Algorithm', 'sqp');
[x,fval,exitflag,output,lambda,grad,hessian] = ...
fmincon(@fun3,x0,Aineq,bineq,[],[],[],[],[],options);

不要迷信别人写的,最重要的是自己思考。

©️2020 CSDN 皮肤主题: 猿与汪的秘密 设计师:上身试试 返回首页