how to sum symbolic equation
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Walter Roberson
le 18 Déc 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 commentaires
Voir également
Catégories
En savoir plus sur Calculus dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!