Calculating Summation in MATLAB

  • Thread starter Thread starter sara_87
  • Start date Start date
  • Tags Tags
    Matlab Summation
Click For Summary
SUMMARY

This discussion focuses on calculating summation in MATLAB using the 'symsum' function. The user encountered an error stating "Undefined function or method 'symsum' for input arguments of type 'double'," indicating a misuse of symbolic variables. A corrected approach involves defining 'i' and 'n' as symbolic variables using the 'sym' function, ensuring they are treated as rational numbers. The provided code snippets illustrate the correct implementation of symbolic summation within nested loops.

PREREQUISITES
  • Understanding of MATLAB syntax and functions
  • Familiarity with symbolic mathematics in MATLAB
  • Knowledge of loops and control structures in programming
  • Basic concepts of summation notation and its application
NEXT STEPS
  • Learn about MATLAB's 'syms' and 'sym' functions for symbolic computation
  • Explore the 'symsum' function documentation for advanced summation techniques
  • Investigate error handling in MATLAB to troubleshoot function calls
  • Study nested loops in MATLAB to understand their behavior with varying limits
USEFUL FOR

Students, educators, and researchers working with MATLAB who need to perform symbolic summation and troubleshoot related errors in their code.

sara_87
Messages
748
Reaction score
0

Homework Statement



I want to calculate a sum (where the end value is in the sum), eg:
\sum^{n-1}_{i=1}{2i+n}

Homework Equations



I don't want to 'split' the sum, i just want to write this.

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.
 
Physics news on Phys.org
Here's something to try:

Code:
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.
Here's a link to a page for sym.
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 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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
2K
Replies
2
Views
2K
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K