Sum of a product to arbitrary precision using MATLAB symbolic toolbox

1 view (last 30 days)
Is it intended behavior that MATLAB Symbolic Toolbox evaluates an expression like
vpa(symsum(symprod(myfactor,'q',1,k),'k',0,3),ndigits)
by evaluating the summand before substituting the index k for 0,1,2, or 3?
The problem I am having is that I am using an expression like the above, and MATLAB is evaluating symprod(myfactor,'q',1,k) as a function of k (which takes around a minute and yields a multi-page expression), and then attempting to evaluate this function at 0, 1, 2, and 3, which is an incredibly complicated thing to do that MATLAB doesn't know how to simplify, when really all it needs to do is evaluate it at 0, 1, 2, and 3 and add those up.
To me this feels like a design flaw (I can't see why you would try to evaluate the summand before replacing the index with an integer), but if it isn't (or if it is), is there any way to write a code that still uses symsum and symprod but causes MATLAB to evaluate the summand at 0, 1, 2, and 3 rather than at general k?

Answers (1)

Walter Roberson
Walter Roberson on 20 Jan 2014
symsum() is for symbolic summation. That attempts to find the general formula and only then substitutes in the actual limits. For example symsym(k, k, 0, 3) would calculate k*(k+1)/2 as the general solution and would then substitute in k = 3 to find the answer.
If you are wanting to sum a series of terms, then at the MATLAB level use sum() instead of symsum() -- you can arrayfun() or the like to get the terms to be summed.
At the MuPAD level use _add() (note the underscore). You can also access _add() in MATLAB by using evalin() or feval()
feval(symengine, '_add', symprod(myfactor,'q',1,k), k, 0, 3)
  2 Comments
Jonathan Holmes
Jonathan Holmes on 20 Jan 2014
Edited: Walter Roberson on 20 Jan 2014
While arrayfun "should" work, sym output is not implemented (you also would have to repmat the first three arguments, which is a bit inelegant).
feval(symengine, '_add', symprod(myfactor,'q',1,k), k, 0, 3)
seems to behave just like symsum as far as I can tell. I can actually use a for loop to add up my terms, which is inefficient but should be acceptable for my purposes. I would still like it if there was some elegant way of doing this with MATLAB functions:
mysum=0
for k=0:3
mysum=mysum+symprod(myfactor,'q',1,k);
end
Walter Roberson
Walter Roberson on 20 Jan 2014
arrayfun with 'uniform', 0 giving a cell array that can then be added together

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!