如果变量取决于优化变量,如何编写程序进行优化? [英] How to code a program for optimization if a variable depends on optimization variables?
问题描述
在进行优化时,我在Matlab中有一个问题.假设我想对向量x
I have a question in matlab when doing optimization. Suppose I want to to do an optimization problem on a vector of x
min_x f(x,c)
这样的sum(x)=1
.对于每个固定的x
,c
是一个常量,例如
min_x f(x,c)
such that sum(x)=1
. For each fixed x
, c
is a constant, say for example
(x.*a+c).^(1./alpha)+(x.*b+c).^(1./alpha)=1
其中a,b,alpha
是已知的.
该算法适用于每个固定的x
,因此sum(x)=1
,我们需要从
The algorithm is for each fixed x
such that sum(x)=1
, we need to find c
from
(x.*a+c).^(1./alpha)+(x.*b+c).^(1./alpha)=1
并计算f(x,c)
,然后我们更新一个新的x
.
and compute f(x,c)
, then we update a new x
.
是否可以在matlab中使用fmincon解决问题?我想放
Is it possible to use fmincon in matlab to solve the problem? I want to put
(x.*a+c).^(1./alpha)+(x.*b+c).^(1./alpha)=1
用于 fmincon 中的非线性约束,但我想知道它是否有效,因为我们不知道如何根据x
明确编写c
.
for the non linear constraint in fmincon, but i wonder if it is valid as we do not know how to write c
explicitly in terms of x
.
推荐答案
- 使用
solve
根据x
显式地编写 - 将
f(x,c)
定义为仅x
的函数 -
c
被其表达式替换 - 开始优化
- Use
solve
to writec
explicitly in terms ofx
- Define
f(x,c)
to be a function ofx
only c
is replaced by its expression- Start the optimization
c
请仔细阅读评论
% 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
这篇关于如果变量取决于优化变量,如何编写程序进行优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!