Symbolic Math Toolbox™ provides two functions for calculating sums:
symsum
andsum
You can find definite sums by using bothsum
andsymsum
. Thesum
function sums the input over a dimension, while thesymsum
function sums the input over an index.
Consider the definite sum
First, find the terms of the definite sum by substituting the index values fork
in 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 usingsymsum
by specifying the index and the summation limits.sum
andsymsum
return identical results.
S_symsum = symsum(f, k, 1, 10)
S_symsum = 1968329/1270080
symsum
versussum
For summing definite series,symsum
can be faster thansum
. For summing an indefinite series, you can only usesymsum
.
You can demonstrate thatsymsum
can be faster thansum
by summing a large definite series such as
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
symsum
andsum
symsum
can provide a more elegant representation of sums thansum
provides. Demonstrate this difference by comparing the function outputs for the definite series
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总
. The总
function 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')