[转载]Matlab fmincon函数用法

[转载]Matlab fmincon函数用法原文地址:fmincon函数用法”style=”text-decoration:none;color:rgb(151,38,38)”>Matlab fmincon函数用法作者:长笛人倚楼Gloria这个函数在之前优化工具箱一文中已经介绍过,由于其应用广泛,所以这里通过实例单独整理一下其用法。一、基本介绍求解问题的标准型为minF(X)s.tAXAeqX=

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

这个函数在之前优化工具箱一文中已经介绍过,由于其应用广泛,所以这里通过实例单独整理一下其用法。
一、基本介绍
求解问题的标准型为
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];
[];b [];
Aeq []; beq [];
vlb []; vub [];
[x, fval] fmincon(@fun4, x0, A, b, Aeq, beq, vlb, vub, @mycon)
 
function fun4(x);
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)
[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
  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
length(y); number of training examples
 
You need to return the following variables correctly 
0;
grad zeros(size(theta));
 
====================== YOUR CODE HERE ======================
Instructions: Compute the cost of particular choice of theta.
              You should set 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
%
 
theta;
hx ./ (1 exp(-z));
1/m sum([-y’ log(hx) – (1 – y)’ log(1 – hx)]);
 
for  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/145940.html原文链接:https://javaforall.net

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


相关推荐

  • 程序猿的三高:高并发、高性能、高可用

    程序猿的三高:高并发、高性能、高可用

    2021年10月3日
    48
  • VS2013激活码_vs2008 助手激活成功教程

    VS2013激活码_vs2008 助手激活成功教程下载:MSDNLibraryforVisualStudio2008(MSND)http://www.microsoft.com/downloads/details.aspx?FamilyID=7bbe5eda-5062-4ebb-83c7-d3c5ff92a373&DisplayLang=zh-cnVisualStudioTeamSystem2008Tea…

    2022年8月10日
    5
  • 进程间通信和线程间通信的几种方式是_线程通信方式

    进程间通信和线程间通信的几种方式是_线程通信方式进程和线程的区别:对于进程来说,子进程是父进程的复制品,从父进程那里获得父进程的数据空间,堆和栈的复制品。而线程,相对于进程而言,是一个更加接近于执行体的概念,可以和同进程的其他线程之间直接共享数据,而且拥有自己的栈空间,拥有独立序列。共同点:它们都能提高程序的并发度,提高程序运行效率和响应时间。线程和进程在使用上各有优缺点。线程执行开销比较小,但不利于资源的管理和保护,而进程相反…

    2022年10月6日
    0
  • IDEA 2020 3.3激活码_通用破解码

    IDEA 2020 3.3激活码_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月15日
    404
  • java list转arraylist_进制数之间的转换方法

    java list转arraylist_进制数之间的转换方法一.Array转为List1.实现方法:java中数组转list使用Arrays.asList(T…a)方法。publicclassArray2List{publicstaticvoidmain(String[]args){List<String>listA=Arrays.asList(“dog”,”cat”,”cow”)…

    2022年8月23日
    9
  • 至尊问题「建议收藏」

    称号:已知m、n为整数,且满足下列两个条件:① m、n∈1,2,…,K,(1≤K≤10^9)② (n^ 2-mn-m^2)^2=1编一程序,对给定K,求一组满足上述两个条件的m、n,而且使m^2+n^2的值最大。比如,若K=1995,则m=987,n=1597,则m、n满足条件,且可使m^2+n^2的值最大。输入输入仅一行,K的值。输出…

    2022年4月13日
    43

发表回复

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

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