# Why doesn't symprod recognize indexed variables?

4 ビュー (過去 30 日間)
Sree 2018 年 12 月 26 日
コメント済み: Sree 2018 年 12 月 27 日
I was expecting to get: k1*k2*k3*k4. Instead . . . .

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

### 採用された回答

Stephan 2018 年 12 月 26 日

Hi,
try:
syms k(n)
symprod(k(n),n,1,4)
this should give you the expected result.
Best regards
Stephan
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Walter Roberson 2018 年 12 月 27 日

However, going from the symbolic function calls k(1)*k(2)*k(3)*k(4) to k1*k2*k3*k4 becomes tricky. And the k(1) and so on in the symprod result will be function calls, not indexing.

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

### その他の回答 (1 件)

Walter Roberson 2018 年 12 月 27 日
MATLAB does not permit symbolic variables to be indices because it does not know how to index a definite matrix at an unresolved location.
When you define k as a matrix and try
symprod(k(n), n, 1, 4)
then that is equivalent to
TTTTTT = k(n);
symprod(TTTTTT, n, 1, 4)
because argument evaluation is always done first before the function itself is invoked. So the k(n) part has to be meaningful outside the context of the symprod() or symsum()... but it isn't.
When you want to do a symprod or symsum that would call for the dummy variable to be used as an index, then what you need to do is to replace the symprod or symsum with creation of a vector of definite values that are then prod() or sum(). For example,
TTTTTT = k(1:4);
prod(TTTTTT)
or more compactly,
prod(k(1:4))
or even
prod(k)
since you defined k as being only 4 long.
If you had something more complicated such as
symprod(sin(k(n)^n), n, 1, 4)
you would again proceed with definite values:
prod( sin(k(1:4)).^(1:4) )
##### 3 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
Stephan 2018 年 12 月 27 日
Since this answer is meeting your requirements much better than mine, you should unaccept my answer and accept this one.
Sree 2018 年 12 月 27 日
No need for that change! Your answer was immediately useful and solved my problem (which was a bit more involved than the example I had included in my question). The other answer was philosophically instructive, for the long term.

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

### カテゴリ

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

### Community Treasure Hunt

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

Start Hunting!

Translated by