码迷,mamicode.com
首页 > 其他好文 > 详细

fmincon如何使非线性约束函数写成匿名函数的形式

时间:2014-11-13 20:39:25      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   sp   for   strong   

fmincon命令中,可以将目标函数直接写成匿名函数的形式,但是一个匿名函数只有一个输出,而fmincon中的nonlcon写成m文件时是写成[c,ceq],c表示非线性不等式,ceq表示非线性等式。那么如何将约束函数nonlcon写成匿名函数呢,查阅matlab的help文档,查阅优化工具箱中对非线性约束Nonlinear Constraints的介绍,也可以通过fmincon帮助中对nonlcon参数的介绍链接进对Nonlinear Constraints的介绍。

 

Anonymous Nonlinear Constraint Functions

For information on anonymous objective functions, see Anonymous Function Objectives.

Nonlinear constraint functions must return two outputs. The first output corresponds to nonlinear inequalities, and the second corresponds to nonlinear equalities.

Anonymous functions return just one output. So how can you write an anonymous function as a nonlinear constraint?

The deal function distributes multiple outputs. For example, suppose your nonlinear inequalities are

Suppose that your nonlinear equality is

x2 = tanh(x1).

Write a nonlinear constraint function as follows:

c = @(x)[x(1)^2/9 + x(2)^2/4 - 1;
        x(1)^2 - x(2) - 1];
ceq = @(x)tanh(x(1)) - x(2);
nonlinfcn = @(x)deal(c(x),ceq(x));

To minimize the function cosh(x1) + sinh(x2) subject to the constraints in nonlinfcn, use fmincon:

obj = @(x)cosh(x(1))+sinh(x(2));
opts = optimset(‘Algorithm‘,‘sqp‘);
z = fmincon(obj,[0;0],[],[],[],[],[],[],nonlinfcn,opts)

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is 
non-decreasing in feasible directions, to within the default 
value of the function tolerance, and constraints were satisfied 
to within the default value of the constraint tolerance.

z =
   -0.6530
   -0.5737

To check how well the resulting point z satisfies the constraints, use nonlinfcn:

[cout,ceqout] = nonlinfcn(z)

cout =
   -0.8704
   -0.0000

ceqout =
     -2.2204e-016

z indeed satisfies all the constraints to within the default value of the TolCon constraint tolerance, 1e-6.

 

fmincon如何使非线性约束函数写成匿名函数的形式

标签:style   blog   io   color   ar   os   sp   for   strong   

原文地址:http://www.cnblogs.com/MarshallL/p/4095572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!