matlab中的fmincon函数的用法!急

matlab中的fmincon函数的用法!急,第1张

一、fmincon函数基本介绍

求解问题的标准型为

min F(X)

st

AX <= b

AeqX = beq

G(x) <= 0

Ceq(X) = 0

VLB <= X <= VUB

其中X为n维变元向量,G(x)与Ceq(X)均为非线性函数组成的向量,其它变量的含义与线性规划,二次规划中相同,用Matlab求解上述问题,基本步骤分为三步:

1 首先建立M文件funm定义目标函数F(X):

function f = fun(X);

f = F(X)

2 若约束条件中有非线性约束:G(x) <= 0 或 Ceq(x) = 0,则建立M文件nonlconm定义函数G(X)和Ceq(X);

function [G, Ceq] = nonlcon(X)

G =

Ceq =

3 建立主程序,非线性规划求解的函数时fmincon,命令的基本格式如下:

[转载]Matlab fmincon函数用法

注意:

(1)fmincon函数提供了大型优化算法和中型优化算法。默认时,若在fun函数中提供了梯度(options 参数的GradObj设置为'on'),并且只有上下界存在或只有等式约束,fmincon函数将选择大型算法,当既有等式约束又有梯度约束时,使用中型算法。

(2)fmincon函数的中型算法使用的是序列二次规划法。在每一步迭代中 求解二次规划子问题,并用BFGS法更新拉格朗日Hessian矩阵。

(3)fmincon函数可能会给出局部最优解,这与初值X0的选取有关。

二、实例

1 第一种方法,直接设置边界

主要是指直接设置A,b等参数。

例1:min f = -x1 - 2x2 + 1/2x1^2 + 1/2 x2^2

2x1 + 3x2 <= 6

x1 + 4x2 <= 5

x1, x2 >= 0

function ex131101

x0 = [1; 1];

A = [2, 3; 1, 4];

b = [6, 5];

Aeq = [];

beq = [];

VLB = [0; 0];

VUB = [];

[x, fval] = fmincon(@fun3, x0, A, b, Aeq, beq, VLB, VUB)

function f = fun3(x)

f = -x(1) - 2x(2) + (1/2)x(1)^2 + (1/2)x(2)^2;

2 第二种方法,通过函数设置边界

例2: min f(x) = exp(x1) (4x1^2 + 2x2^2 + 4x1x2 + 2x2 + 1)

x1 + x2 = 0

15 + x1 x2 - x1 - x2 <= 0

-x1x2 - 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)) (4x(1)^2 + 2x(2)^2 + 4x(1)x(2) + 2x(2) + 1);

function [g, ceq] = mycon(x)

g = [15 + 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

% wrt 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 wrt 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);

matlab fmincon函数用法如下:

1、给定初值x0,求解fun函数的最小值x。fun函数的约束条件为Ax<= b,x0可以是标量或向量。

2、返回exitflag参数,描述函数计算的有效性,意义同无约束调用。

3、返回包含优化信息的输出参数output。

扩展资料

fmincon函数用法注意事项:

对于默认的“内点”算法,fmincon将违反边界lb≤x≤ub或等于边界的x0分量设置为边界区域的内部。对于“信赖域反射”算法,fmincon将违反分量设置为边界区域的内部。对于其他算法,fmincon将违反组件设置为最接近的界。

fmincon作用:

fmincon用于求解非线性多元函数最小值的matlab函数,优化工具箱提供fmincon函数用于对有约束优化问题进行求解。

参考资料:fmincon-百度百科

从我的经验看,一般来说BFGS从稳定性还是速度都很好,貌似最快的svm算法也是BFGS的变种。当然这个写code稍微比梯度下降之类的非点劲,但既然他那code里有二类的了,改成多累的应该也不难吧我猜

以上就是关于matlab中的fmincon函数的用法!急全部的内容,包括:matlab中的fmincon函数的用法!急、matlab fmincon函数用法、matlab logistic regression运行后输入参数数目不足等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9679098.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存