Skip to main content
Library homepage
 
Engineering LibreTexts

4.9: Recursive for Loops

  • Page ID
    84398
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    By Carey A. Smith

    A "recursive" for loop is one in which the calculations for each iteration depend on the results of the previous iteration. The summation of a series is one example.

    Example \(\PageIndex{1}\) Cumulative Sum Vector Plot

    %% Cumulative_Sum.m
    format compact; format short g;
    clear all; close all; clc;
    %%
    %term = zeros(1,10);
    term(1) = 0;
    cum_sum(1) = 0; % Initialize the sum
    for m = 2:8
       term(m) = 1/m^2;
       cum_sum(m) = cum_sum(m-1) + term(m);
    end
    %% Display the final value
    disp(['The final sum= ',num2str(cum_sum(end))])
    % Display the last term
    disp(['The last term= ',num2str(term(end))])

    %% Plot the terms
    figure;
    plot(term,'o')
    grid on;
    title('Terms of 1/(x+1)^2')
    xlabel('Iteration number')
    ylabel('Term Values')

    %% Plot the cumulative sums
    figure;
    plot(cum_sum,'o')
    grid on;
    title('Cumulative Sums of 1/(x+1)^2')
    xlabel('Iteration number')
    ylabel('Cumulative Sum')

    Answer

    Cumulative_Sum.png

    .

    The following example compares the code for a scalar total (the total is a single value) to the code for a vector total (the total vector includes the totals for each iteration).

    When there are 2 data sets plotted on 1 figure, you can use the legend() function that identifies each data set, as shown in the following example.

    It also shows the use of the text() function to place information on the figure. This is used in combination with the num2str() function to convert a computed value to a character string that can become part of a title or text, using concatenation of character strings.

    Example \(\PageIndex{2}\) Scalar vs. Vector Sums

    %% Scalar loop sum
    n = 4; % n = the number of terms in the series
    total_scalar = 0; % This total is updated on each iteration
    for k = 1:n
        term = 1/k^3;
        total_scalar = total_scalar + term;
        % Update total by adding the current term to the previous value of total.
        % Recall that "=" means assignment, not equality.
    end
    total_scalar % echo the final total
    % total_scalar = 1.1777
    % This is a scalar loop sum, because termk and total are single values which change on each iteration

    %% Vector loop sum
    n = 4; % n = the number of terms in the series
    total_vec = zeros(1,n); % This total is updated on each iteration
    % total_vec(k) will be the partial sum of the terms up thru the kth term.
    % Initial the 1st term and the total:
    k = 1;
    termk(k) = 1/k^3;
    total_vec(k) = termk(k);
    for k = 2:n % Start with k=2, becuz the loop uses total_vec(k-1)
        termk(k) = 1/k^3;
        total_vec(k) = total_vec(k-1) + termk(k);
        % Update total by adding the current term to the previous value of total.
        % New value is total(k). Previous value is total(k-1)
        % Recall that "=" means assignment, not equality.
    end
    total_vec % Echo total_vec

    %% Since termk and total_vec are vectors, we can plot them after the for loop.
    kk = 1:n; % This is the x-axis vector
    figure;
    plot(kk, termk, '-*')
    hold on;
    plot(kk, total_vec, '-s')
    grid on;
    legend('termk','total\_vec','Location','East')
    title('Vector Power Series of 1/k^3')

    total_end = round(total_vec(end),2) % Round to 2 decimal places
    total_str = num2str(total_end) % Convert to a string
    text(2,1,['The total is converging to ',total_str]) % concatenate the words with the number

    text(2,0.2,'The terms are converging to 0')

    Solution

    % total_scalar = 1.1777

    % total_vec = 1 1.125 1.162 1.1777

    Vector_Sums.png

    .

    This is an example of a different kind of recursive loop.

    Example \(\PageIndex{3}\) Ball Rolling on a Parabola with Friction

    % A ball starts at height h1 on the ramp:

    h1 = 10 % cm Initial height on the ramp

    % Each time it rolls back and forth, it loses some energy due to friction

    ff = 0.08 % The fraction of friction lost each iteration

    % Use a for loop to compute the height on successive iterations.

    % Let the number of iterations be:

    n = 30

    % Store the heights of each iteration in a vector.

    % Initialize this vector to all zeros

    h = zeros(1,n) % a row vector that is 8 long.

    h(1) = h1

    for k = 1:(n-1)

        h_loss = ff*h(k);

        h(k+1) = h(k) - h_loss;

    end

    %% Draw a parabolic ramp
    x = -10:0.5:10;
    y = (0.1)*x.^2;
    figure;
    plot(x,y,'LineWidth',2)

    % Add a ball
    bxc = -8.9; % ball center
    byc = 9; % ball center
    hold on;
    plot(bxc, byc,'ro','MarkerSize',16,'LineWidth',2)
    title('Ball on a Parabolic Ramp')

    %% Plot the vector of heights with '+' markers

    figure

    plot(h, '+')

    grid on

    title('height vs, iteration')

    ylim([0,h1])

    Solution

    Add example text here.

    Ball_Parabola.png

    Ball_Height.png

    Example \(\PageIndex{4}\) Population Growth of Chipmunks

    chipmuncks_wikimedia.jpg

    Figure \(\PageIndex{i}\): https://commons.wikimedia.org/wiki/File:Chipmunk_in_PP_(93909).jpg
    Creative Commons Attribution-Share Alike 4.0 International license.

    % Population growth of chipmunks
    n = 5 % The number of years (iterations)
    % (1 pt) Create this vector:
    chipmunks = zeros(1,n);
    % The population starts with 40 individuals, so set the 1st value = 40.
    chipmunks(1) = 40;
    % Assume that there are approximately equal numbers of males and females.

    % Write a for loop that models the size of this population for 5 years.
    % Let the for loop index be k; k will go from 1 to (n-1), because it
    % computes the population for year k+1, based on year k.
    for k = 1:(n-1)
        % Inside the for loop do the following to update the number of chipmunks
        % for each for successive year:
        % Each year, on average, 40% of the chipmunks die.
        % So, subtract 0.40*population from the population each iteration.
        % Let the resulting number be called c1.
        c1 = chipmunks(k) - 0.40*chipmunks(k)
        % Round the result to the nearest integer.
        c1 = round(c1)

        % Add an average of 3 baby chipmunks are born each year for each pair of surviving chipmunks
        % ==> new chipmunks = 3*(c1/2)
        % Add this the c1. Let the resulting number be called c2.
        c2 = c1 + 3*(c1/2)
        % Round the result to the nearest integer. Use
        c2 = round(c2)
        % Set chipmunks(k+1) = c2
        chipmunks(k+1) = c2
    end % end the loop

    % After the loop end, open a new figure and plot the number of chipmunks each year.
    % Use the ‘v’ marker for each data point.
    figure
    plot(chipmunks,'v')
    % (1 pt) Turn the grid on and add a title and axis labels:
    grid on
    title('Chipmunk Population Growth')
    xlabel('years')
    ylabel('Chipmunks')

    Solution

    Add example text here.

    Population_growth_of_chipmunks.png

    Many variations on the population growth example are possible.

    Exercise \(\PageIndex{1}\) Modified Population Growth

    Modify the chipmunk population growth example by changing the death rate and/or the birth rate, such that the number of chipmunks after 5 years is a value between 1.5 and 3 times the initial population.

    This will require some trial and error.

    Answer

    Figure this out on your own.


    This page titled 4.9: Recursive for Loops is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.