LESSON 7 QUESTIONS: Rates of change

FOCUS QUESTION: How can I characterize rates of change?

Contents

EXAMPLE 1: Load the fetal size data

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


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

   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

Questions Answers
What does end mean? The end identifier names the last element along a dimension of an array.
If x is a vector, what does x(2:end) mean? This expression is a vector with the same elements as those in x except that the first element has been omitted.
Why do we omit the first row of toHeel in forming weeks? Both toRump and toHeel have a row corresponding to week 20.


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

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

Questions Answers
What does the diff function do? The diff function calculates the difference between adjacent values in an array. (See the handout on the diff function for more details.)
What does the variable poundsPerWeek correspond to? The variable poundsPerWeek contains a numerical estimate of the slope of fetal weight as a function of gestational week. Since gestational week is a time variable, you can also interpret it as the weekly fetal growth rate.
If x is a vector, how many elements does diff(x) have? The result of diff(x) has one fewer value than x does. (Think of the values of x as being fence posts and the differences corresponding to the spaces between them. There is one fewer space than post.)
How big is weeks(1:(end-1))? The weeks variable contains a vector with 36 rows. the result of weeks(1:(end-1)) is a vector containing the first 35 rows of weeks.
What is the first element of the vector weeks(2:end)? The expression weeks(2:end) is a vector with the same values as weeks starting with the second element of weeks. Therefore, its first element has the value weeks(2).
Why does weekMid contain a vector containing locations half-way between the values in weeks? The two vectors weeks(1:(end-1)) and weeks(2:end)) each contain one less element than the vector weeks. The first element of weeks(1:(end-1)) is weeks(1), while the first element of weeks(2:end) is weeks(2). The expression adds the two vectors and divides by 2. The first element of this evaluation is just (weeks(1) + weeks(2))/2.


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

   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

Questions Answers
Why did we plot poundsPerWeek versus the weekly midpoints rather than the weeks themselves? The rate of change or slope is a relationship between pairs of points, and there is one fewer slope value than points on the curve. It makes sense to plot the slopes versus points halfway between these rather than associating the slopes with the smallest end or the largest end.
What is the relationship between the blue curve and the green curve? The green curve is the slope of the blue curve.
When is the slope 0? The slope is 0 at points where the curve has a relative maximum or minimum.
When does the slope have its largest positive value? The slope has its largest positive value at points where the curve is climbing most steeply upward.
When does the slope have its largest negative value? The slope has its largest negative value at points where the curve is climbing most steeply downward. The slope is never negative in this particular case (babies don't lose weight during gestation).
How does the Color property affect the Figure Window? Setting the Color property in the figure command sets the color of the border region around the axes. A color value of three 1's represents white.


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

   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);

Questions Answers
Why didn't we append the toRump length measurements and the toHeel length measurements together in the same way as we did with the weight measurements? The method of measuring the length is different in the two data files, so these length measurements need to be treated as distinct curves.


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

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


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

   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)')
   legend({'Crown-to-rump', 'Crown-to-heel'}, 'Location', 'SouthOutside', ...
       'Orientation', 'Horizontal')
   box on
   hold off

Questions Answers
How can I add grid lines to a plot? Set the 'XGrid' and/or 'YGrid' properties of the axes. The subplot function creates a new axes. This example shows how to set XGrid property at axes creation. You can also add the grid using the plottools or in your script using the set command.
Why did only one subplot have a legend? Since both subplots use the same color scheme, a second legend would have been redundant and resulted in over-crowding.


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

    pop = load('population.txt');


EXAMPLE 9: Calculate the year midpoints for plotting

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


EXAMPLE 10: Calculate the annual growth rate for each country

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


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

   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


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

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

Questions Answers
What does USPerCapGR represent? This represents the annual growth rate of the US population, divided by the number of people at the beginning of the year. This quantity is called the per capita growth rate.
Why are per capita growth rates used? Per capita growth rates give a better sense of growth when comparing populations that are changing or when comparing different populations. Another common measure of population growth is growth rate per woman of child bearing age.


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

   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

Questions Answers
How does the US Census Bureau know what the population of China will be in 2020? Obviously, the values of future population growth are projections or estimates based on current knowledge of the population. It should be pointed out that the US Census is conducted every 10 years, with the next one coming up in 2010. The population estimates between those census points are probably less accurate than those of actual census years. The Wikipedia census page http://en.wikipedia.org/wiki/Census has a listing of when various countries have conducted a census.


These lesson questions were written by Kay A. Robbins of the University of Texas at San Antonio and last modified on 20-Sep-2010.