View Full Version : summation in matlab
sara_87
Oct30-09, 07:44 PM
1. The problem statement, all variables and given/known data
I want to calculate a sum (where the end value is in the sum), eg:
\sum^{n-1}_{i=1}{2i+n}
2. Relevant equations
I dont want to 'split' the sum, i just want to write this.
3. The attempt at a solution
syms i n
for n=1:5
for i=1:n-1
symsum((2*i+n),i,1,n-1)
end
end
i get the error: ??? Undefined function or method 'symsum' for input arguments of type 'double'.
i think my code is wrong.
Here's something to try:
n = sym('n', r);
for n = 1:5
for i = 1:n-1
symsum((2*sym('i', r) + n), i, n-1)
end
end
No guarantee this will work, since I don't have matlab to test it out on. What I did is patterned after the last example on the reference page in the following link. I have also rewritten your syms command to use the sym command. The r flag specifies that both i and n are rational. Without a flag, they default to complex.
Here's a link to a reference page for symsum (http://www.mathworks.com/access/helpdesk/help/toolbox/symbolic/index.html?/access/helpdesk/help/toolbox/symbolic/symsum.html&http://www.bing.com/search?q=matlab+symsum&src=IE-SearchBox&FORM=IE8SRC).
Here's a link to a page for sym (http://www.mathworks.com/access/helpdesk/help/toolbox/symbolic/index.html?/access/helpdesk/help/toolbox/symbolic/syms.html&http://www.mathworks.com/cgi-bin/texis/webinator/search/?db=MSS&prox=page&rorder=750&rprox=750&rdfreq=500&rwfreq=500&rlead=250&sufs=0&order=r&is_summary_on=1&ResultCount=10&query=syms&submitButtonName=Search).
From the TOC that appears along with the reference page for sym, the next reference page is for syms.
I don't think you have run into it, yet, since you're still trying to get the symsum/syms/sym thing sorted out, but I believe your inside loop will cause problems for you. When n = 1, the inner loop looks like for i=1, 0. The documentation for for (http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/matlab.html) doesn't go into as much detail as I would like, so isn't clear what happens when the starting value is larger than the ending value, and you haven't specified a negative increment. Possibly the loop skips that iteration. Don't know.
Anyway, some things to think about and try out.
vBulletin® v3.7.6, Copyright ©2000-2009, Jelsoft Enterprises Ltd.