【问题标题】:How to code a program for optimization if a variable depends on optimization variables?如果变量取决于优化变量,如何编写优化程序?
【发布时间】:2019-06-20 16:45:41
【问题描述】:

我在 matlab 做优化时有一个问题。假设我想对x的向量做一个优化问题

min_x f(x,c) 这样sum(x)=1。对于每个固定的xc 是一个常数,比如说

(x.*a+c).^(1./alpha)+(x.*b+c).^(1./alpha)=1

a,b,alpha 是已知的。

算法是针对每个固定的x,使得sum(x)=1,我们需要从

中找到c
(x.*a+c).^(1./alpha)+(x.*b+c).^(1./alpha)=1 

并计算f(x,c),然后我们更新一个新的x

是否可以在matlab中使用fmincon来解决这个问题?我想放

(x.*a+c).^(1./alpha)+(x.*b+c).^(1./alpha)=1 

对于 fmincon 中的非线性约束,但我想知道它是否有效,因为我们不知道如何根据 x 显式编写 c

【问题讨论】:

    标签: matlab optimization nonlinear-optimization


    【解决方案1】:
    • 使用solvec 明确写入x
    • f(x,c) 定义为仅x 的函数
    • c 被其表达式替换
    • 开始优化

    请通读 cmets

    % Given a, b, alpha
    a = 2; b = 5; alpha = 1;
    
    % Unknown x, c
    syms x c
    
    % Relation between x and c
    eq = (x.*a+c).^(1./alpha)+(x.*b+c).^(1./alpha)== 1 ;
    
    % Mention only c, x will be considered as independent variable
    % The solution gives c in terms of x
    c = solve(eq, c);
    
    % Transfom syms variable into function handle variable 
    c = matlabFunction(c);
    % c(x) = x.*(-7.0./2.0)+1.0./2.0
    
    
    % Define f as a function of x only, c is a constant having x as parameter
     fun =@(x)f(x, c(x));
    
    % optimization starts here 
    
     [x, fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
    
    
    % Given function in terms of x and c
    function y = f(x,c)
        y = 2.*x + c;
    end
    
    

    【讨论】:

    • 太棒了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 2011-01-29
    • 1970-01-01
    相关资源
    最近更新 更多