フィルターのクリア

how to symsum to type follow eqution

1 回表示 (過去 30 日間)
BIN LIN
BIN LIN 2015 年 8 月 11 日
コメント済み: Walter Roberson 2015 年 8 月 13 日
for example:if i know A1=1, A2=3,A3=7,B1=2,B2=4,B3=8 and so on, i want to symsum An+Bn , which n is variable,like A1+B1+A2+B2+A3+B3+...+An+Bn . how to type it in matlab?

採用された回答

Walter Roberson
Walter Roberson 2015 年 8 月 12 日
syms A B n m
total = symsum(A(m)+B(m), m, 1, n)
later you can
subs(total, n, 5) %to make n 5
  2 件のコメント
BIN LIN
BIN LIN 2015 年 8 月 12 日
I test it like follow, but it does not work.
syms A B n m
A1=1
A2=3
A3=7
B1=2
B2=4
B3=8
total = symsum(A(m)+B(m), m, 1, n)
subs(total, n, 2) %to make n 5
it show this: Error using sym/subsindex (line 685) Invalid indexing or function definition. When defining a function, ensure that the body of the function is a SYM object. When indexing, the input must be numeric, logical or ':'.
Error in sym>privformat (line 1490) x = subsindex(x)+1;
Error in sym/subsref (line 707) [inds{k},refs{k}] = privformat(inds{k});
Error in test2 (line 8) total = symsum(A(m)+B(m), m, 1, n)
Walter Roberson
Walter Roberson 2015 年 8 月 13 日
symsum cannot do that. An is not a symbolic expression.
A(n) is a symbolic expression if A is a vector (or matrix) of symbolic values or if A(n) is a function that returns a symbolic expression.
You can use
A = [A1, A2, A3];
B = [B1, B2, B3];
before you do the symsum().
You should be considering using
A = syms('A', [1 3]); %use appropriate value instead of 3
after which A(1) would be A1, A(2) would be A2, and so on, and you would be able to access those elements and even assign values to them if you are careful. For example,
C = A(2) + B(1);
subs(C, A, {1, 3, 7})
would extract the names of the variables in A as the second argument and would use those names as the targets of the substitutions where needed in C
What you asked for is not really symbolic variables: you are trying to use dynamic variable names, which causes much much more trouble than it is worth.

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

その他の回答 (0 件)

カテゴリ

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