Calculating Summation in MATLAB

In summary, the conversation revolves around calculating a sum where the end value is included in the sum, and the desired code is written using MATLAB's symsum function. However, the code is not working and the user is unsure if it is correct. They provide a potential solution and ask for feedback. The expert suggests some changes to the code and also points out a potential issue with the inner loop.
  • #1
sara_87
763
0

Homework Statement



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

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
  • #2
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.
 
  • #3


I would suggest using the built-in function "sum" in MATLAB instead of symsum. Symsum is used for symbolic summation, which is not necessary in this case. The correct syntax for using the "sum" function would be:

sum(2*i + n, i, 1, n-1)

This will give you the sum from i=1 to n-1 of 2*i+n. Also, make sure to declare n and i as numeric variables before using them in the loop. Your final code could look something like this:

n = 5; % end value of the sum
sum = 0; % initialize sum variable
for i=1:n-1
sum = sum + (2*i + n);
end
disp(sum) % display the final sum value

I hope this helps. Keep in mind that there may be other ways to solve this problem in MATLAB, so feel free to explore different options.
 

FAQ: Calculating Summation in MATLAB

What is summation in MATLAB?

Summation in MATLAB is a mathematical operation that calculates the total of a given set of numbers. It is represented by the symbol "∑" and is commonly used in statistical analysis and other mathematical calculations.

How do I calculate summation in MATLAB?

To calculate summation in MATLAB, you can use the "sum" function. This function takes in an array of numbers as an input and returns the sum of all the elements in the array. For example, if you have an array of numbers stored in the variable "x", you can calculate the summation by using the command "sum(x)".

Can I perform summation on a specific range of numbers in MATLAB?

Yes, you can perform summation on a specific range of numbers in MATLAB by using the colon operator ":". For example, if you want to calculate the summation of numbers from 1 to 10, you can use the command "sum(1:10)". This will calculate the summation of all the numbers from 1 to 10.

Can I calculate summation with conditional statements in MATLAB?

Yes, you can use conditional statements in MATLAB to calculate summation based on certain conditions. For example, you can use the "if" statement to only add certain numbers to the summation if they meet a specific condition. You can also use the "for" loop to iterate through a set of numbers and add them to the summation only if they meet a certain condition.

Are there any other functions or methods for calculating summation in MATLAB?

Yes, there are other functions and methods available in MATLAB for calculating summation, such as "cumsum" for calculating the cumulative summation and "trapz" for calculating the numerical integration of a function. You can also create custom functions to calculate summation in a specific way, depending on your needs.

Similar threads

Replies
3
Views
1K
Replies
1
Views
1K
Replies
1
Views
3K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
6
Views
2K
Back
Top