在Matlab中编写求和表达式
如何在Matlab代码中编写涉及变量求和的表达式,然后如何最小化表达式? 前任。我需要最小化以下函数
E= \sum_{i,j}[C_{ij}(r_{ij}) + C2_{ij}(r_{ij})^2]
我需要最小化上述表达式r_{ij}s 的值,其中 i 和 j 不同。
我可以在 MATLAB 中使用 fmincon(),但我无法适当地编写表达式以将其作为 fmincon() 的输入。
谢谢。
How do I write an expression in Matlab code involving summation of a variable and then how do I minimize the expression?
ex. I need to minimize the following function
E= \sum_{i,j}[C_{ij}(r_{ij}) + C2_{ij}(r_{ij})^2]
I need to minimize the above expression for any values of r_{ij}s where i and j vary.
I could use the fmincon() in MATLAB but I am unable to write my expression suitably to give it as input to the fmincon().
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
其中
C
、C2
和r
是相同形状的矩阵。Try this:
where
C
,C2
andr
are matrices of the same shape.fmincon
和其他优化函数不需要您将所有内容编写为表达式,它们也可以针对函数进行优化。我不完全确定
fmincon
所需的语法,但我猜它类似于E = f(theta)
,其中theta
是您想要调整的参数向量,以使E
最小。由于我没有发现您的问题得到明确描述,因此我假设您的参数是C
和C2
(在r
是您的参数的情况下,情况类似且更简单)。由于 fmincon 使用向量来存储系数,因此我们需要一个函数来获取这样的向量并将其转换为上面的 Criterion 函数所需的大小。
这样,您就可以创建一个轻松符合优化器接口的匿名函数:当变量
rValues
@(theta)(criterionRolledC(theta,rValues)) 将执行此操作> 在当前工作区中包含您的r
值。如果您想要完全相反,即您的参数是
r
,则更简单:您可以与其他情况类似地构造一个匿名函数。
fmincon
and other optimization functions do not require you to write everything as an expression, they can optimize for functions as well.I'm not completely sure about the syntax required by
fmincon
, but I guess it's something likeE = f(theta)
, wheretheta
is a parameter vector you want adjusted such thatE
is minimal. Since I don't find your problem clearly described, I will assume your parameters areC
andC2
(in the case thatr
are your parameters, the case is similar and simpler).As
fmincon
uses a vector to store the coefficients, we need a function that takes such a vector and transforms it into the sizes required by thecriterion
function above.That way, you can make an anonymous function that easily conforms to the interface of the optimizer:
@(theta)(criterionRolledC(theta,rValues))
will do when the variablerValues
in your current workspace contains yourr
values.In case you want the exact opposite, i.e. your parameters are
r
, it is simpler:And you can construct an anonymous function similarly to the other case.