how to sum symbolic equation

6 次查看(过去 30 天)
ehsan
ehsan 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 CenterFile Exchange 中查找有关 Calculus 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by