how to sum symbolic equation
7 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Walter Roberson
on 18 Dec 2017
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.
0 Comments
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!