필터 지우기
필터 지우기

how to sum symbolic equation

조회 수: 8 (최근 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 CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by