Main Content

Symbolic Summation

Symbolic Math Toolbox™ provides two functions for calculating sums:

  • sumfinds the sum of elements of symbolic vectors and matrices. Unlike the MATLAB®sum, the symbolicsumfunction does not work on multidimensional arrays. For details, follow the MATLABsumpage.

  • symsumfinds the sum of a symbolic series.

Comparingsymsumandsum

You can find definite sums by using bothsumandsymsum. Thesumfunction sums the input over a dimension, while thesymsumfunction sums the input over an index.

Consider the definite sum S = k = 1 10 1 k 2 . First, find the terms of the definite sum by substituting the index values forkin the expression. Then, sum the resulting vector usingsum.

syms k f = 1/k^2; V = subs(f, k, 1:10) S_sum = sum(V)
V = [ 1, 1/4, 1/9, 1/16, 1/25, 1/36, 1/49, 1/64, 1/81, 1/100] S_sum = 1968329/1270080

Find the same sum by usingsymsumby specifying the index and the summation limits.sumandsymsumreturn identical results.

S_symsum = symsum(f, k, 1, 10)
S_symsum = 1968329/1270080

Computational Speed ofsymsumversussum

For summing definite series,symsumcan be faster thansum. For summing an indefinite series, you can only usesymsum.

You can demonstrate thatsymsumcan be faster thansumby summing a large definite series such as S = k = 1 100000 k 2 .

To compare runtimes on your computer, use the following commands.

symsktic sum(sym(1:100000).^2); toc tic symsum(k^2, k, 1, 100000); toc

Output Format Differences Betweensymsumandsum

symsumcan provide a more elegant representation of sums thansumprovides. Demonstrate this difference by comparing the function outputs for the definite series S = k = 1 10 x k . To simplify the solution, assumex > 1.

syms x assume(x > 1) S_sum = sum(x.^(1:10)) S_symsum = symsum(x^k, k, 1, 10)
S_sum = x^10 + x^9 + x^8 + x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x S_symsum = x^11/(x - 1) - x/(x - 1)

Show that the outputs are equal by using. Thefunction returns logical1(true), meaning that the outputs are equal.

总(S_sum == S_symsum)
ans = logical 1

For further computations, clear the assumptions.

assume(x, 'clear')