11.1. For loops#

If we want to repeat the execution of a set of commands a given number of times then we can use a for loop. The MATLAB syntax for a for loop is

for variable = list
    % commands
end

The for declaration requires a loop variable and a list. The loop variable takes on the value of the first element in list and the commands in the indented lines below the for loop declaration are executed for this value. Then the loop variable takes on the value of the second element in list and the commands in the intended lines below are repeated for this value. The loop continues in this way for all of the elements in list.

Create a MATLAB file called 11_Loops.m and save it to your OneDrive folder. Enter the following code into your program.

% 11. Loops

clear % Clear all variables
clc   % Clear command window

% For loops
subjects = ["linear algebra", "programming", "calculus"];

for subject = subjects
    fprintf("%s\n", subject)
end

Run your program and your should see the following printed to the console.

linear algebra
programming
calculus

We can use the command for generating a range of numbers to loop through sequential values. To demonstrate this enter the following code into your program.

fprintf("\n")

for i = 1 : 5
    fprintf("%d\n", i)
end

Run your program and you should see the following added to the console.

0
1
2
3
4

11.1.1. The Fibonacci sequence#

To demonstrate how useful for loops are we will use one to generate the first \(n\) numbers of the Fibonacci sequence \(F_0, F_1, \ldots, F_n\) which is defined as

\[\begin{split} F_n &= \begin{cases} 0, & n = 0, \\ 1, & n = 1, \\ F_{n-1} + F_{n-2}, & n \geq 2. \end{cases}\end{split}\]

Lets write a program to create an array containing the first 20 Fibonacci numbers. Enter the following code into your program.

% Fibonacci sequence
a = 0;
b = 1;
fprintf("\n%i\n%i", a, b)

for i = 3 : 20
    c = a + b;
    fprintf("%1i\n", c)
    a = b;
    b = c;
end

Here we use two variables a and b to contain two successive Fibonacci numbers which are initialise to 0 and 1 respectively. We then use a for loop to loop through values from 3 to 20 since we already know the first two Fibonacci numbers. Inside the for loop we update calculate the next number in the sequence and print it out and update the values of a and b.

Run your program and your should see the following added to the console output.

0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181

11.1.2. Exercises#

Create a new MATLAB file called M11_Loops_exercises.m and save it to your OneDrive folder. Use it to answer the following exercises.

Exercise 11.1

Use a for loop to print “hello world” 10 times.

Exercise 11.2

The factorial of a number \(n\) is denoted by \(n!\) and defined as the product of the numbers 1 to \(n\)

\[ n! = 1 \times 2 \times 3 \cdots (n - 1) \times n. \]

Use a for loop to calculate 52!, the number of ways a regular pack of cards can be shuffled[1].

Exercise 11.3

The series expansion of \(\sin(x)\) is

\[\sin(x) = \sum_{n = 0}^\infty \frac{(-1)^n}{(2 n + 1)!} x^{2n+1}. \]

Use a for loop to compute \(\sin(\frac{\pi}{4}) \approx 0.707\) by summing this series up to the first 5 terms.

Hints: the following commands may come in useful:

  • pi returns the value of \(\pi\)

  • factorial(n) returns \(n!\)