在Matlab中编写求和表达式

发布于 2025-01-02 04:11:09 字数 237 浏览 2 评论 0原文

如何在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

数理化全能战士 2025-01-09 04:11:09

试试这个:

E = sum(sum( C.*r + C2.*r.^2 ));

其中 CC2r 是相同形状的矩阵。

Try this:

E = sum(sum( C.*r + C2.*r.^2 ));

where C, C2 and r are matrices of the same shape.

朕就是辣么酷 2025-01-09 04:11:09

fmincon 和其他优化函数不需要您将所有内容编写为表达式,它们也可以针对函数进行优化。

function E = criterion(r, C, C2)
  e  = C.*r + C2.*r.^2;
  E  = sum(e(:));

我不完全确定 fmincon 所需的语法,但我猜它类似于 E = f(theta),其中 theta 是您想要调整的参数向量,以使 E 最小。由于我没有发现您的问题得到明确描述,因此我假设您的参数是 CC2 (在 r 是您的参数的情况下,情况类似且更简单)。

由于 fmincon 使用向量来存储系数,因此我们需要一个函数来获取这样的向量并将其转换为上面的 Criterion 函数所需的大小。

function E = criterionRolledC(theta,r)
  assert(numel(theta)==2*numel(r), 'The size of theta has to be twice the size of r');
  [M N] = size(r);
  C  = theta(1:M*N);      
  C2 = theta(M*N+1:end);
  C  = reshape(C , M, N);
  C2 = reshape(C2, M, N);

  E = criterion(r,C,C2);

这样,您就可以创建一个轻松符合优化器接口的匿名函数:当变量 rValues@(theta)(criterionRolledC(theta,rValues)) 将执行此操作> 在当前工作区中包含您的 r 值。

如果您想要完全相反,即您的参数是 r,则更简单:

function E = criterionRolledR(theta,C,C2)
  assert(numel(theta)==numel(C), 'The size of theta has to be the same size as C');
  assert(all(size(C)==size(C2)), 'C and C2 need to have the same size');
  [M N] = size(C);
  r = reshape(theta, M, N);

  E = criterion(r,C,C2);

您可以与其他情况类似地构造一个匿名函数。

fmincon and other optimization functions do not require you to write everything as an expression, they can optimize for functions as well.

function E = criterion(r, C, C2)
  e  = C.*r + C2.*r.^2;
  E  = sum(e(:));

I'm not completely sure about the syntax required by fmincon, but I guess it's something like E = f(theta), where theta is a parameter vector you want adjusted such that E is minimal. Since I don't find your problem clearly described, I will assume your parameters are C and C2 (in the case that r 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 the criterion function above.

function E = criterionRolledC(theta,r)
  assert(numel(theta)==2*numel(r), 'The size of theta has to be twice the size of r');
  [M N] = size(r);
  C  = theta(1:M*N);      
  C2 = theta(M*N+1:end);
  C  = reshape(C , M, N);
  C2 = reshape(C2, M, N);

  E = criterion(r,C,C2);

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 variable rValues in your current workspace contains your r values.

In case you want the exact opposite, i.e. your parameters are r, it is simpler:

function E = criterionRolledR(theta,C,C2)
  assert(numel(theta)==numel(C), 'The size of theta has to be the same size as C');
  assert(all(size(C)==size(C2)), 'C and C2 need to have the same size');
  [M N] = size(C);
  r = reshape(theta, M, N);

  E = criterion(r,C,C2);

And you can construct an anonymous function similarly to the other case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文