$$ \newcommand{\Oof}[1]{\mathcal{O}(#1)} \newcommand{\F}{\boldsymbol{F}} \newcommand{\J}{\boldsymbol{J}} \newcommand{\x}{\boldsymbol{x}} \renewcommand{\c}{\boldsymbol{c}} $$

For loops

Many computations are repetitive by nature and programming languages have certain loop structures to deal with this. Here we will present what is referred to as a for loop (another kind of loop is a while loop, to be presented afterwards). Assume you want to calculate the square of each integer from 3 to 7. This could be done with the following two-line program.

for i = 3:7
    i^2
end

What happens when Matlab interprets your code here? First of all, the word for is a reserved word signalling to Matlab that a for loop is wanted. Matlab then sticks to the rules covering such constructions and understands that, in the present example, the loop should run 5 successive times (i.e., 5 iterations should be done), letting the variable i take on the numbers \( 3, 4, 5, 6, 7 \) in turn. During each iteration, the statement inside the loop (i.e. i^2) is carried out. After each iteration, i is automatically (behind the scene) updated. When the last number is reached, the last iteration is performed and the loop is finished. When executed, the program will therefore print out \( 9, 16, 25, 36 \) and \( 49 \). The variable i is often referred to as a loop index, and its name (here i) is a choice of the programmer.

Note that, had there been several statements within the loop, they would all be executed with the same value of i (before i changed in the next iteration). Make sure you understand how program execution flows here, it is important.

The specification of the values desired for the loop variable (here 3:7) is more generally given as start:step:stop, meaning that the loop variable should take on the integers from start to stop, inclusive at both ends, in steps of step. If step is skipped, the default value is 1, as in the example above. Note that decreasing integers may be produced by letting start > stop combined with a negative step. This makes it easy to, e.g., traverse arrays in either direction.

Let us modify ball_plot.m from the chapter A Matlab program with vectorization and plotting to illustrate how useful for loops are if you need to traverse arrays. In that example we computed the height of the ball at every milli-second during the first second of its (vertical) flight and plotted the height versus time.

Assume we want to find the maximum height during that time, how can we do it with a computer program? One alternative may be to compute all the thousand heights, store them in an array, and then run through the array to pick out the maximum. The program, named ball_max_height.m, may look as follows.

g = 9.81;
v0 = 5;
t = linspace(0, 1, 1000);
y = v0*t - 0.5*g*t.^2;

% At this point, the array y with all the heights is ready.
% Now we need to find the largest value within y.

largest_height = y(1);   % Preliminary value
for i = 2:1000
   if y(i) > largest_height
       largest_height = y(i);
   end
end

fprintf('The largest height achieved was %f m \n',largest_height);

% We might also like to plot the path again just to compare
plot(t,y);
xlabel('Time (s)');
ylabel('Height (m)')

There is nothing new here, except the for loop construction, so let us look at it in more detail. As explained above, Matlab understands that a for loop is desired when it sees the word for. The value in y(1) is used as the preliminary largest height, so that, e.g., the very first check that is made is testing whether y(2) is larger than this height. If so, y(2) is stored as the largest height. The for loop then updates i to \( 2 \), and continues to check y(3), and so on. Each time we find a larger number, we store it. When finished, largest_height will contain the largest number from the array y. When you run the program, you get

The largest height achieved was 1.274210 m
which compares favorably to the plot that pops up.

To implement the traversing of arrays with loops and indices, is sometimes challenging to get right. You need to understand the start, stop and step length choices for an index, and also how the index should enter expressions inside the loop. At the same time, however, it is something that programmers do often, so it is important to develop the right skills on these matters.

Having one loop inside another, referred to as a double loop, is sometimes useful, e.g., when doing linear algebra. Say we want to find the maximum among the numbers stored in a \( 4 \times 4 \) matrix A. The code fragment could look like

largest_number = A(1,1);

for i = 1:length(A)
    for j = 1:length(A)
        if A(i,j) > largest_number
            largest_number = A(i,j);
        end
    end
end
Here, all the j indices (1 - 4) will be covered for each value of index i. First, i stays fixed at i = 1, while j runs over all its indices. Then, i stays fixed at i = 2 while j runs over all its indices again, and so on. Sketch A on a piece of paper and follow the first few loop iterations by hand, then you will realize how the double loop construction works. Using two loops is just a special case of using multiple or nested loops, and utilizing more than two loops is just a straightforward extension of what was shown here. Note, however, that the loop index name in multiple loops must be unique to each of the nested loops. Note also that each nested loop may have as many code lines as desired, both before and after the next inner loop.

The vectorized computation of heights that we did in ball_plot.m (the chapter A Matlab program with vectorization and plotting) could alternatively have been done by traversing the time array (t) and, for each t element, computing the height according to the formula \( y = v_0t - \frac{1}{2}gt^2 \). However, it is important to know that vectorization goes much quicker. So when speed is important, vectorization is valuable.

Use loops to compute sums.

One important use of loops, is to calculate sums. As a simple example, assume some variable \( x \) given by the mathematical expression $$ \begin{equation*} x = \sum_{i=1}^{N}2\cdot i , \nonumber \end{equation*} $$ i.e., summing up the \( N \) first even numbers. For some given \( N \), say \( N = 5 \), \( x \) would typically be computed in a computer program as:

N = 5;
x = 0;
for i = 1:N
    x = x + 2*i;
end
x

Executing this code will print the number 30 to the screen. Note in particular how the accumulation variable x is initialized to zero. The value of x then gets updated with each iteration of the loop, and not until the loop is finished will x have the correct value. This way of building up the value is very common in programming, so make sure you understand it by simulating the code segment above by hand. It is a technique used with loops in any programming language.