首页 > 代码库 > fmincon如何使非线性约束函数写成匿名函数的形式
fmincon如何使非线性约束函数写成匿名函数的形式
fmincon命令中,可以将目标函数直接写成匿名函数的形式,但是一个匿名函数只有一个输出,而fmincon中的nonlcon写成m文件时是写成[c,ceq],c表示非线性不等式,ceq表示非线性等式。那么如何将约束函数nonlcon写成匿名函数呢,查阅matlab的help文档,查阅优化工具箱中对非线性约束Nonlinear Constraints的介绍,也可以通过fmincon帮助中对nonlcon参数的介绍链接进对Nonlinear Constraints的介绍。
Anonymous Nonlinear Constraint FunctionsFor 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 areSuppose that your nonlinear equality isx2 = 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.5737To check how well the resulting point z satisfies the constraints, use nonlinfcn:[cout,ceqout] = nonlinfcn(z)cout = -0.8704 -0.0000ceqout = -2.2204e-016z indeed satisfies all the constraints to within the default value of the TolCon constraint tolerance, 1e-6.
fmincon如何使非线性约束函数写成匿名函数的形式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。