How to Generate a Summation of a Series in MATLAB

Ammar Ali Feb 15, 2024
  1. How to Generate a Summation of a Series in MATLAB Using the symsum() Function
  2. How to Generate a Summation of a Series in MATLAB Using Vectorized Operations
  3. How to Generate a Summation of a Series in MATLAB Using a for Loop
  4. Conclusion
How to Generate a Summation of a Series in MATLAB

In mathematical and scientific computations, it’s often necessary to compute the summation of a series. MATLAB, a powerful computational software, offers various methods to accomplish this task.

Whether you’re dealing with simple arithmetic series or complex mathematical expressions, MATLAB provides tools and techniques to generate series summations efficiently.

In this article, we’ll explore different approaches to generating series summations in MATLAB, covering methods such as using built-in functions, vectorized operations, and iterative loops. Through detailed examples and explanations, we’ll demonstrate how each method works and when to use them.

How to Generate a Summation of a Series in MATLAB Using the symsum() Function

The symsum() function, available in the Symbolic Math Toolbox, offers a powerful tool to perform series summation with ease.

This is a symbolic math function in MATLAB that calculates the summation of symbolic expressions over a specified range. It allows you to work with symbolic variables and perform mathematical operations symbolically rather than numerically.

This is particularly useful when dealing with infinite series or when you need precise symbolic representations of mathematical expressions.

The basic syntax of the symsum() function is as follows:

result = symsum(f, n, a, b);

Here’s a breakdown of the parameters:

  • f: Represents the mathematical expression defining the series.
  • n: Denotes the index variable of the series.
  • a and b: Indicate the lower and upper bounds of the summation, respectively.

The symsum() function computes the summation of the series represented by f, with the index variable n ranging from a to b. It employs symbolic computation to handle mathematical expressions symbolically.

Example 1: Summation of a Simple Series

Let’s start with a straightforward example: computing the summation of the first n natural numbers.

clc
clear

syms n
result = symsum(n, n, 1, 100)

In this example, we first declare a symbolic variable n using the syms command, allowing MATLAB to perform symbolic computations involving n.

Next, we utilize the symsum() function to calculate the summation of n from 1 to 100 and store the result in the variable result. This function iterates over each integer value of n, adds them up, and returns the final summation.

Output:

Summation of a Simple Series Output

The output 5050 represents the summation of the first 100 natural numbers.

Example 2: Handling Series With Multiple Variables

Next, let’s consider a series involving multiple variables and compute its summation.

clc
clear

syms n k
result = symsum(n^k, k, 1, 10)

In this example, we tackle a series involving multiple variables, specifically n^k, where n and k are symbolic variables. After initializing the workspace, we declare both n and k as symbolic variables using the syms command.

Then, we employ the symsum() function to compute the summation of n^k for values of k ranging from 1 to 10. MATLAB iterates over each integer value of k, evaluates n^k accordingly, and sums up the results.

Output:

Handling Series With Multiple Variables

The output represents the summation of the series n^k with k ranging from 1 to 10.

Example 3: Series Summation Without Specifying Bounds

In some cases, we may not have explicit bounds for the series. Let’s see how to compute the summation in such scenarios.

clc
clear

syms n
result = symsum(1/n^2, n, 1, Inf)

Here, we demonstrate how to compute the summation of a series without specifying explicit bounds. This example is a well-known convergent series called the Basel problem.

After clearing the workspace, we declare a symbolic variable n using the syms command. Then, we utilize the symsum() function to calculate the summation of 1/n^2 over all possible values of n.

Unlike the previous examples, here we do not specify upper and lower bounds for the summation. MATLAB performs the summation considering all integer values of n.

Output:

Series Summation Without Specifying Bounds Output

The output pi^2/6 represents the summation of the series 1/n^2.

Example 4: Computing Summation of Polynomial Series

Lastly, let’s compute the summation of a polynomial series.

clc
clear

syms n k
result(n) = symsum(n^k, k, 1, 5)
result(3)

In this example, we compute the summation of a polynomial series n^k, where k ranges from 1 to 5. After initializing the workspace, we declare symbolic variables n and k using the syms command.

Next, we use the symsum() function to calculate the summation of n^k for k ranging from 1 to 5 and store the result in the variable result(n). Subsequently, we evaluate the result for n = 3. MATLAB computes the summation by substituting each integer value of k and n and sums up the terms.

Output:

Computing Summation of Polynomial Series Output

The output represents the polynomial summation n^5 + n^4 + n^3 + n^2 + n and its evaluation for n = 3.

These examples illustrate the versatility of the symsum() function in MATLAB, enabling users to handle various types of series and compute their summations accurately and efficiently.

How to Generate a Summation of a Series in MATLAB Using Vectorized Operations

In addition to using functions like symsum(), MATLAB offers the flexibility to generate series summations using vectorized operations. This approach leverages MATLAB’s optimized array operations to compute summations efficiently.

Vectorized operations involve performing operations on entire arrays or matrices at once rather than looping over individual elements. This is achieved by utilizing MATLAB’s element-wise operators and functions, which are designed to work with arrays seamlessly.

To generate series summations using vectorized operations, we typically define the series as arrays and then apply array operations to compute the summation.

Example 1: Summation of a Simple Series

Let’s start with a simple example: computing the summation of the first n natural numbers using vectorized operations.

n = 1:100;
result = sum(n)

In this example, we first define an array n using the colon operator 1:100, representing the numbers from 1 to 100. This array serves as the series over which we’ll perform summation.

Next, we utilize the sum() function, a vectorized operation, to compute the summation of all elements in the array n. The sum() function iterates over the entire array and adds up all elements efficiently, resulting in the summation of the series.

The computed summation is stored in the variable result.

Output:

Summation of a Simple Series - Vectorized Operations

The output 5050 represents the summation of the first 100 natural numbers.

Example 2: Handling Series With Mathematical Operations

Next, let’s consider a series involving mathematical operations and compute its summation using vectorized operations.

n = 1:10;
series = 2 * n + 3;
result = sum(series)

In this example, we define an array n containing the first 10 integers. Then, we define the series 2n + 3 by applying mathematical operations to the array n.

MATLAB automatically performs element-wise multiplication and addition between each element of n and the constants 2 and 3, respectively, resulting in the series. Finally, we compute the summation of the series by utilizing the sum() function, which efficiently sums up all elements of the array series.

Output:

Handling Series With Mathematical Operations - Vectorized Operations

The output 140 represents the summation of the series 2n + 3 for n ranging from 1 to 10.

Example 3: Series Summation With Conditional Operations

Lastly, let’s compute the summation of a series involving conditional operations using vectorized operations.

n = 1:100;
series = n .* mod(n, 2);
result = sum(series)

In this example, we define an array n containing the first 100 natural numbers using the colon operator 1:100. Then, we define the series based on a conditional operation using array operations and the mod() function.

MATLAB evaluates the expression n .* mod(n, 2) element-wise, resulting in n if n is even and 0 otherwise. Finally, we compute the summation of the series by utilizing the sum() function, which efficiently sums up all elements of the array series.

Output:

Series Summation With Conditional Operations - Vectorized Operations

The output 2500 represents the summation of the series based on the defined conditional operation. Using vectorized operations in MATLAB provides a convenient and efficient way to generate series summations.

How to Generate a Summation of a Series in MATLAB Using a for Loop

While MATLAB provides efficient vectorized operations and symbolic math functions for series summation, using a for loop is another approach to compute the summation of a series. Although for loops are generally less efficient compared to vectorized operations, they offer flexibility and simplicity, making them suitable for handling series with complex or dynamic terms.

A for loop allows you to iterate over a series of values and perform computations at each iteration. This iterative approach is useful when dealing with series where terms depend on previous values or when the series has a dynamic structure.

By looping through the terms of the series and accumulating the summation, you can compute the total sum efficiently.

Example 1: Summation of a Simple Series

Let’s start with a simple example: computing the summation of the first n natural numbers using a for loop.

n = 100;
sum = 0;
for i = 1:n
    sum = sum + i;
end
sum

In this example, we specify the number of terms n in the series, which is 100 in this case. Then, we initialize a variable sum to 0, which will accumulate the sum of the series.

Next, we use a for loop to iterate over each term in the series, represented by the index variable i. Within each iteration, we add the current term i to the cumulative sum sum.

After the loop completes execution, the variable sum contains the summation of the series.

Output:

Summation of a Simple Series - for Loop

The output 5050 represents the summation of the first 100 natural numbers.

Example 2: Handling Series With Mathematical Operations

Next, let’s consider a series involving mathematical operations and compute its summation using a for loop.

n = 10;
sum = 0;
for i = 1:n
    term = 2 * i + 3;
    sum = sum + term;
end
sum

In this example, we specify the number of terms n in the series, which is 10 in this case. Then, we initialize a variable sum to 0 to accumulate the sum.

Within the for loop, we define the term of the series 2i + 3 for each iteration. We add each term to the cumulative sum sum within the loop.

After the loop completes execution, the variable sum contains the summation of the series.

Output:

Handling Series With Mathematical Operations - for Loop

The output 140 represents the summation of the series for the first 10 terms.

Example 3: Series Summation With Conditional Operations

Lastly, let’s compute the summation of a series involving conditional operations using a for loop.

n = 100;
sum = 0;
for i = 1:n
    if mod(i, 2) == 0
        term = i;
    else
        term = 0;
    end
    sum = sum + term;
end
sum

In this example, we specify the number of terms n in the series, which is 100 in this case. We initialize a variable sum to 0 to accumulate the sum.

Within the for loop, we check if each term i is even using the mod() function. If i is even, we define the term as i; otherwise, we define it as 0.

We then add each term to the cumulative sum sum within the loop. After the loop completes execution, the variable sum contains the summation of the series.

Output:

Series Summation With Conditional Operations - for Loop

The output 2550 represents the summation of the series based on the defined conditional operation.

The for loop in MATLAB provides a straightforward approach to generating series summations. By iterating over each term of the series and accumulating the sum, we can compute the summation efficiently for various types of series.

Conclusion

In conclusion, MATLAB offers multiple approaches to generate the summation of a series, each suited to different scenarios and preferences.

The symsum() function from the Symbolic Math Toolbox is a powerful tool for handling symbolic computations and is particularly useful for complex mathematical expressions and symbolic variables. Vectorized operations leverage MATLAB’s optimized array operations, providing a convenient and efficient way to compute series summations when dealing with arrays and matrices.

Using a for loop offers a straightforward and flexible method for generating series summations, allowing users to handle various types of series and customize computations as needed.

Ultimately, the choice of method depends on factors such as the complexity of the series, computational efficiency requirements, and personal preference. By understanding these different approaches, we can effectively generate series summations to suit their specific needs in mathematical and scientific computations.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook