LESSON 7: Rates of change

FOCUS QUESTION: How can I characterize rates of change?

The rate of change, derivative, or slope measures how quickly a variable changes as a function of the independent variable. Per capita growth rates are useful for comparing values across populations that are changing in size over time.

In this lesson you will:
  • Learn about rates of change.
  • Use the MATLAB diff function.
  • Use end with colons to pick out elements of an array.
  • Calculate the slope.
  • Calculate per capita rates of change.
3D fetal sonography

File Description
toRump.txt
toHeel.txt
These data sets contain typical fetal size as a function of gestational week.
  • The first column contains the week
  • The second column contains the fetal length in cm
  • The third column contains the fetal weight (mass actually) in grams.
  • The toRump.txt data measures length from baby crown to baby rump during early gestation.
  • The toHeel.txt measures the length from baby crown to baby heel during later gestation.
  • The data came from http://www.babycenter.com/average-fetal-length-weight-chart.
  • population.txt This data gives actual and projected population data for the US, China, and India for the period 1950 to 2050.
    • The first column contains the year
    • The second column contains the US population
    • The third column contains the Chinese population
    • The fourth column contains the Indian population.
  • The data came from the International Data Base (IDB) of the US Census Bureau at http://www.census.gov/ipc/www/i db/.
  • Contents

    SETUP FOR LESSON 7


    EXAMPLE 1: Load the fetal size data

    Create a new cell in which you type and execute:

        load toRump.txt;  % Load the crown-to-rump data
        load toHeel.txt;  % Load the crown-to-heel data
    

    You should see the following 2 variables in your Workspace browser:

    In the space below, draw a picture of the toRump and toHeel arrays and label their rows and columns. Explain how these two arrays fit together.  


    EXAMPLE 2: Merge the time and weight (mass) variables from the two data sets

    Create a new cell in which you type and execute:

       weeks = [toRump(:, 1); toHeel(2:end, 1)]; % Remove first row of second dataset
       mass =  [toRump(:, 3); toHeel(2:end, 3)];
       weight = mass .* 0.00220462262;           % Convert grams to pounds
    

    You should see the following 3 variables in your Workspace browser:

    Verify that these are the first and third columns of the two data sets after they have been put end-to-end without duplicates.


    EXAMPLE 3: Calculate the weekly rate of change of fetal weight

    Create a new cell in which you type and execute:

       poundsPerWeek = diff(weight) ./ diff(weeks);       % Weekly rate of change
       weekMid = (weeks(1:(end-1)) + weeks(2:end))./2;    % Week midpoints for plotting
    

    You should see the following 2 variables in your Workspace browser:

    In the space below:


    EXAMPLE 4: Plot the weight and rate of change of weight

    Create a new cell in which you type and execute:

       figure('Color', [1, 1, 1])                  % New figure
       ax = plotyy(weeks, weight, weekMid, poundsPerWeek); % Save axes
       xlabel(ax(1), 'Weeks')                      % Label x-axis of left axis
       ylabel(ax(1), 'Fetal weight (lbs)')         % Label y-axis of left axis
       ylabel(ax(2), 'Fetal growth rate (lbs/wk)') % Label y-axis of right axis
       title('Characterization of fetal weight during pregnancy') % Title one of the axes
    

    You should see a Figure Window with the following graph:


    Create a new cell right here (beginning of a cell starts with %%). Copy the code for EXAMPLE 4 into the cell and modify the code so that the weight is displayed as a bar chart rather than a line graph.


    EXAMPLE 5: Calculate rate of change (slope) of fetal length in inches per week

    Create a new cell in which you type and execute:

       weeksRump = toRump(:, 1);                        % Pick out the weeks
       weeksHeel = toHeel(:, 1);
       inchesRump = toRump(:, 2).* 0.393800888;         % Convert length to inches
       inchesHeel = toHeel(:, 2).*  0.393800888;
       inchesPerWeekRump = diff(inchesRump) ./ diff(weeksRump); % Calculate rate of change
       inchesPerWeekHeel = diff(inchesHeel) ./ diff(weeksHeel);
    

    You should see the following 6 variables in your Workspace Browser:

    In the space below:


    EXAMPLE 6: Find the midpoints of the weeks intervals for plotting

    Create a new cell in which you type and execute:

       rumpMid = (weeksRump(1:(end-1)) + weeksRump(2:end))./ 2;
       heelMid = (weeksHeel(1:(end-1)) + weeksHeel(2:end))./ 2;
    

    You should the following two variables in your Workspace Browser:


    EXAMPLE 7: Plot the weekly rate of change of fetal length in inches/week

    Create a new cell in which you type and execute:

       figure('Color', [1, 1, 1])
                                                        % Top panel
       subplot(2, 1, 1, 'XGrid', 'on')
       hold on
       plot(weeksRump, inchesRump, 'k');
       plot(weeksHeel, inchesHeel, 'r');
       ylabel('Length (in)')
       title ('Characterization of fetal length during pregancy')
       box on
       hold off
                                                        % Bottom panel
       subplot(2, 1, 2, 'XGrid', 'on')
       hold on
       plot(rumpMid, inchesPerWeekRump, 'k');
       plot(heelMid, inchesPerWeekHeel, 'r');
       xlabel('Gestational week')
       ylabel('Growth rate (in/wk)')
       box on
       legend({'Crown-to-rump', 'Crown-to-heel'}, 'Location', 'SouthOutside', ...
           'Orientation', 'Horizontal')
       hold off
    

    You should see a new Figure Window with the following plot:


    EXAMPLE 8: Load population data for US, China and India

    Create a new cell in which you type and execute:

        pop = load('population.txt');
    

    You should see the following variable in your Workspace Browser:


    EXAMPLE 9: Calculate the year midpoints for plotting

    Create a new cell in which you type and execute:

       years = pop(:, 1);                               % Pick out the years column
       yearMid = (years(1:(end-1)) + years(2:end))./ 2; % Calculate year midpoints
    

    You should see the following 2 variables in your Workspace Browser:


    EXAMPLE 10: Calculate the annual growth rate for each country

    Create a new cell in which you type and execute:

       USGR = diff(pop(:, 2))./ diff(years);     % US annual rate of change
       chinaGR = diff(pop(:, 3)) ./ diff(years); % China annual rate of change
    

    You should see the following 2 variables in your Workspace Browser:

    In the space below:


    EXAMPLE 11: Plot the annual population growth rates for the three countries

    Create a new cell in which you type and execute:

       figure('Color', [1, 1, 1])
       hold on
       plot(yearMid, USGR, 'k')
       plot(yearMid, chinaGR, 'r')
       xlabel('Year')
       ylabel('Annual growth rate')
       title('Comparison of population growth in two countries')
       legend({'US', 'China'})
       box on
       hold off
    

    You should see a Figure Window with the following plot:


    Modify the code of EXAMPLE 11 so that the plot includes the poluation growth of India plotted in blue. Modify the title and legend appropriately.


    EXAMPLE 12: Calculate the per capita annual population growth rates for the three countries

    Create a new cell in which you type and execute:

       USPerCapGR = USGR ./ pop(1:(end-1), 2);
       chinaPerCapGR = chinaGR ./ pop(1:(end-1), 3);
    

    You should see the following 2 variables in your Workspace Browser:

    In the space below:


    EXAMPLE 13: Plot the per capita annual population growth rates for the three countries

    Create a new cell in which you type and execute:

       figure('Color', [1, 1, 1])
       hold on
       plot(yearMid, USPerCapGR, 'k')
       plot(yearMid, chinaPerCapGR, 'r')
       xlabel('Year')
       ylabel('Annual per capita growth rate')
       title('Comparison of population growth in two countries')
       legend({'US', 'China'})
       box on
       hold off
    

    You should see a Figure Window with the following plot:


    Modify the code of EXAMPLE 13 so that the plot includes the per capita poluation growth of India plotted in blue. Modify the title and legend appropriately.

    SUMMARY OF SYNTAX

    MATLAB syntax Description
    end designates the last position in a particular dimension when used as an array index.
    y = diff(x) returns the difference of adjacent elements along the first non-singleton dimension of x.
    ax = plotyy(X1, Y1, X2, Y2) creates a graph with two axes, one for X1 versus Y1 and the other for X2 versus Y2. The ax variable is a two-element vector holding the handles for the respective axes.
    xlabel(ax, 'label') labels the x-axis of the axis designated by ax with the word label. If you omit ax, MATLAB labels the current axis.
    ylabel(ax, 'label') labels the y-axis of the axis designated by ax with the word label. If you omit ax, MATLAB labels the current axis.

    This lesson was written by Kay A. Robbins of the University of Texas at San Antonio and last modified on 31-Dec-2010. Please contact krobbins@cs.utsa.edu with comments or suggestions. The image is a sonographic 3D-image of a fetus created by Gebruiker Mvandergaast. The image is available on Wikipedia as <http://commons.wikimedia.org/wiki/File:Fetal_3D-Sonography.jpg>.