Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Symbolic Math Toolbox™ provides two functions for calculating sums:

`symsum`

and `sum`

You can find definite sums by using both `sum`

and `symsum`

.
The `sum`

function sums the input over a dimension,
while the `symsum`

function sums the input over
an index.

Consider the definite sum $$S={\displaystyle \sum _{k=1}^{10}\frac{1}{{k}^{2}}}.$$ First, find the
terms of the definite sum by substituting the index values for `k`

in
the expression. Then, sum the resulting vector using `sum`

.

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 using `symsum`

by specifying
the index and the summation limits. `sum`

and `symsum`

return
identical results.

S_symsum = symsum(f, k, 1, 10)

S_symsum = 1968329/1270080

`symsum`

versus `sum`

For summing definite series, `symsum`

can
be faster than `sum`

. For summing an indefinite
series, you can only use `symsum`

.

You can demonstrate that `symsum`

can be
faster than `sum`

by summing a large definite series
such as $$S={\displaystyle \sum _{k=1}^{100000}{k}^{2}}.$$

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

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

`symsum`

and `sum`

`symsum`

can provide a more elegant representation
of sums than `sum`

provides. Demonstrate this difference
by comparing the function outputs for the definite series $$S={\displaystyle \sum _{k=1}^{10}{x}^{k}}.$$ To simplify the solution, assume ```
x
> 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 `isAlways`

.
The `isAlways`

function returns logical `1`

(`true`

),
meaning that the outputs are equal.

isAlways(S_sum == S_symsum)

ans = logical 1

For further computations, clear the assumptions.

assume(x, 'clear')