zoukankan      html  css  js  c++  java
  • fmincon如何使非线性约束函数写成匿名函数的形式

    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.

     实际上nonlcon中匿名函数的形式的函数不一定要写成字符串的形式,匿名函数句柄也是可以的(中括号内)。ceq即使为空也要有@

  • 相关阅读:
    在线学习git操作
    logstash使用ruby 修改事件戳时间
    mysql磁盘问题记录
    mkdir --help
    php过滤前端post提交过滤html标签
    【摸鱼范式】【一】UVM入门教程【文字版】
    第一次运行svlib
    svlib文档翻译(第五章)
    svlib文档翻译(第一至四章)
    【三】基于Montgomery算法的高速、可配置RSA密码IP核硬件设计系列
  • 原文地址:https://www.cnblogs.com/MarshallL/p/4095572.html
Copyright © 2011-2022 走看看