how to sum symbolic equation

7 ビュー (過去 30 日間)
ehsan
ehsan 2017 年 12 月 18 日
回答済み: Walter Roberson 2017 年 12 月 18 日
Dear all,
I would like to sum a symbolic function as follow:
J = sigma_j=1 ^J=5 (X{j+1} - X{j})
at the end It should answer should be as follow:
(X2 - X1) + (X3 - X2) + (X4 - X3) + (X5 - X4)
I tried with symsum but it didn't work.
any help would be greatly appreciated.

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 12 月 18 日
X = sym('X', [1,N]);
J = X(end) - X(1);
More generally,
syms f(x)
J = sum(arrayfun(f, x(2:end)-x(1:end-1)))
In earlier MATLAB you might need
temp = arrayfun(f, x(2:end) - x(1:end-1), 'uniform, 0);
J = sum([temp{:}]);
Notice the complete lack of symsum. symsum can never be used to index variables: you need to form the list of definite elements and sum them. symsum is for finding theoretical formula such as symsum(k) being k*(k+1)/2 not for adding definite terms.

カテゴリ

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