Matlab fmincon函数用法

Matlab fmincon函数用法原文地址:fmincon函数用法”>Matlab fmincon函数用法作者:长笛人倚楼Gloria这个函数在之前优化工具箱一文中已经介绍过,由于其应用广泛,所以这里通过实例单独整理一下其用法。一、基本介绍求解问题的标准型为minF(X)s.tAXAeqX=beqG(x)Ceq(X)=0VLB 其中X为n维变元向量,G(x)与Ceq(X)均为非线性函数组成的

大家好,又见面了,我是你们的朋友全栈君。

原文地址:长笛人倚楼Gloria
这个函数在之前优化工具箱一文中已经介绍过,由于其应用广泛,所以这里通过实例单独整理一下其用法。
一、基本介绍
求解问题的标准型为
min F(X)
s.t
AX <= b
AeqX = beq
G(x) <= 0
Ceq(X) = 0
VLB <= X <= VUB
 
其中X为n维变元向量,G(x)与Ceq(X)均为非线性函数组成的向量,其它变量的含义与线性规划,二次规划中相同,用Matlab求解上述问题,基本步骤分为三步:
1. 首先建立M文件fun.m定义目标函数F(X):
function f = fun(X);
f = F(X)
 
2. 若约束条件中有非线性约束:G(x) <= 0 或 Ceq(x) = 0,则建立M文件nonlcon.m定义函数G(X)和Ceq(X);
function [G, Ceq] = nonlcon(X)
G = …
Ceq = …
 
3. 建立主程序,非线性规划求解的函数时fmincon,命令的基本格式如下:
 
2. 第二种方法,通过函数设置边界
例2: min f(x) = exp(x1) * (4*x1^2 + 2*x2^2 + 4*x1*x2 + 2*x2 + 1)
x1 + x2 = 0
1.5 + x1 * x2 – x1 – x2 
<= 0
-x1*x2 – 10 <= 0
function youh3
clc;
x0 = [-1, 1];
A = [];b = [];
Aeq = []; beq = [];
vlb = []; vub = [];
[x, fval] = fmincon(@fun4, x0, A, b, Aeq, beq, vlb, vub, @mycon)
 
function f = fun4(x);
f = exp(x(1)) * (4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + 1);
 
function [g, ceq] = mycon(x)
g = [1.5 + x(1)*x(2) – x(1) – x(2); -x(1)*x(2) – 10];
ceq = [x(1) + x(2)];
 
3. 进阶用法,增加梯度以及传递参数
这里用无约束优化函数fminunc做示例,对于fmincon方法相同,只需将边界项设为空即可。
(1)定义目标函数
function [J, grad] = costFunction(theta, X, y)
%COSTFUNCTION Compute cost and gradient for logistic regression
%   J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the
%   parameter for logistic regression and the gradient of the cost
%   w.r.t. to the parameters.
 
% Initialize some useful values
m = length(y); % number of training examples
 
% You need to return the following variables correctly 
J = 0;
grad = zeros(size(theta));
 
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
%               You should set J to the cost.
%               Compute the partial derivatives and set grad to the partial
%               derivatives of the cost w.r.t. each parameter in theta
%
% Note: grad should have the same dimensions as theta
%
 
z = X * theta;
hx = 1 ./ (1 + exp(-z));
J = 1/m * sum([-y’ * log(hx) – (1 – y)’ * log(1 – hx)]);
 
for  j = 1: length(theta)
    grad(j) = 1/m * sum((hx – y)’ * X(:,j));
end
 
 
% =============================================================
 
end
 
(2)优化求极小值
%  Set options for fminunc
options = optimset(‘GradObj’, ‘on’, ‘MaxIter’, 400);
 
%  Run fminunc to obtain the optimal theta
%  This function will return theta and the cost 
[theta, cost] = …
    fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
 
% [theta, cost] = …
%   fminunc(@(t)(costFunction(t, X, y)), initial_theta);
% Print theta to screen
fprintf(‘Cost at theta found by fminunc: %fn’, cost);
fprintf(‘theta: n’);
fprintf(‘ %f n’, theta);
 
 
 
 
 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150533.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号