Maximization error Error using sym/subsindex Invalid indexing

2 ビュー (過去 30 日間)
Nordine Bouchelia
Nordine Bouchelia 2022 年 4 月 27 日
コメント済み: Kyle 2023 年 10 月 20 日
Hello,
I have an issue while trying to maximize an objective function, I get the following error
Error using sym/subsindex
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must
be symbolic variables, and function body must be sym expression.
Error in indexing (line 1075)
R_tilde = builtin('subsref',L_tilde,Idx);
My code is first to define all the variable P1 P2 P3 omega 2 omega 3 which contains symbolic expressions
and all the other variable using syms
P3 = ((theta*A*(3*alpha*k3+2*betaG*sigma)+2*alpha*(alpha*k3+betaG*sigma))/((2*alpha*k3+2*betaH*sigma)*(2*alpha*k3+2*betaG*sigma)-(alpha*k3)^2))
omega3 = ((theta*A*(3*alpha*k3+2*betaH*sigma)+alpha*alpha*k3)/((2*alpha*k3+2*betaH*sigma)*(2*alpha*k3+2*betaG*sigma)-(alpha*k3)^2))
P2 = ((theta*A*(3*alpha*k2+2*betaG*sigma)+(betaG*sigma+alpha*k2)*(2*alpha+3*alpha*phi*k2))/((2*alpha*k2+2*betaH*sigma)*(2*alpha*k2+2*betaG*sigma)-(alpha*k2)^2))
omega2 = ((theta*A*(3*alpha*k2+2*betaH*sigma)+alpha*alpha*k2+(2*betaH*sigma*betaG*sigma+alpha*k2*(2*betaH*sigma+2*betaG*sigma+3*alpha*k2))*phi)/((2*alpha*k2+2*betaH*sigma)*(2*alpha*k2+2*betaG*sigma)-(alpha*k2)^2))
P1 = (alpha+theta*A)/(2*betaH*sima)
and then setting the problem
>> prob = optimproblem('ObjectiveSense','max');
>> x = optimvar('A')
prob.Objective = (alpha-betaH*sigma*P1+theta*A)*P1-((I*A^2)/2)+delta*(alpha*(1-k2*(P2-omega2))-betaH*sigma*P2+theta*A)*P2+delta*(alpha*k2(P2-omega2)+theta*A-betaG*sigma*omega2)*phi+delta^2*(alpha*(1-k3*(P3-omega3))-betaH*sigma*P3+theta*A)*P3
I don't understand where this error comes from,
thanks for your help
  2 件のコメント
Simeon Pienaar
Simeon Pienaar 2022 年 5 月 13 日
I get the same error even when copying the examples from MathWorks. See below and attached:
syms x
c = coeffs(16*x^2 + 19*x + 11)
Did you manage to sort this problem out?
Kyle
Kyle 2023 年 10 月 20 日
Hello Nordine and Simeon,
I believe you are both experiencing the same error as the user in this question. In short, MATLAB thinks you are trying to do indexing into an array when you did not intend to. I will try to illustrate what I imagine happened using Simeon's example first:
% First, run the example in a clean workspace.
syms x
c = coeffs(16*x^2 + 19*x + 11)
c = 
which coeffs % confirm that coeffs is defined as a sym method
/MATLAB/toolbox/symbolic/symbolic/@sym/coeffs.m % sym method
coeffs = 1:10; % create an array called coeffs in the workspace (maybe unintentionally)
which coeffs % confirm that coeffs is now pointing to an array variable
coeffs is a variable.
syms x
c = coeffs(16*x^2 + 19*x + 11) % now we see the error in Simeon's example
Error using indexing
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.
In Simeon's case, I believe that the issue was caused by accidentally defining an array called coeffs in the workspace. MATLAB determines which function to call in a specific order, and variables always beat out functions. You can check which function MATLAB will use by passing the name into which, as seen in the above code.
Now, in Nordine's case, I believe the same general problem exists. In the line which errors, take a careful look at this section:
prob.Objective = (alpha-betaH*sigma*P1+theta*A)*P1-((I*A^2)/2)+delta*(alpha*(1-k2*(P2-omega2))-betaH*sigma*P2+theta*A)*P2+delta*(alpha*k2(P2-omega2)+theta*A-betaG*sigma*omega2)*phi+delta^2*(alpha*(1-k3*(P3-omega3))-betaH*sigma*P3+theta*A)*P3
% ^^^^^^^^^^^^^
% Here is that snippet without the rest of the equation:
% k2(P2-omega2) % MATLAB thinks you are trying to index into an array named k2
% k2*(P2-omega2) % This syntax tells MATLAB you intend to multiply instead
I could not detect any other reason for the error in your code example, so the code should start to work with this change to your syntax (assuming everything else in your environment is working).

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by