LESSON QUESTIONS: Rates of change

FOCUS QUESTION: How can I characterize rates of change?


EXAMPLE 1: Load the fetal size data

    load toRump.txt;
    load toHeel.txt;

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

   weeks = [toRump(:, 1); toHeel(2:end, 1)];
   mass =  [toRump(:, 3); toHeel(2:end, 3)];
   weight = mass .* 0.00220462262;

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);
   weekMid = (weeks(1:(end-1)) + weeks(2:end))./2;

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])
   ax = plotyy(weeks, weight, weekMid, poundsPerWeek);
   xlabel(ax(1), 'Weeks')
   ylabel(ax(1), 'Fetal weight (lbs)')
   ylabel(ax(2), 'Fetal growth rate (lbs/wk)')
   title('Characterization of fetal weight during pregnancy')

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);
   weeksHeel = toHeel(:, 1);
   inchesRump = toRump(:, 2).* 0.393800888;
   inchesHeel = toHeel(:, 2).*  0.393800888;
   inchesPerWeekRump = diff(inchesRump) ./ diff(weeksRump);
   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])
   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
   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

EXAMPLE 8: Calculate the percentage change in fetal weight

Create a new cell in which you type and execute:

     percentChangeWeight = 100.*diff(weight)./weight(1:end-1);

EXAMPLE 9: Calculate the percentage change in fetal weight

Create a new cell in which you type and execute:

   figure('Color', [1, 1, 1])                  % New figure
   ax = plotyy(weeks, weight, weekMid, percentChangeWeight); % 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), '% change') % 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:

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.

These lesson questions were written by Kay A. Robbins of the University of Texas at San Antonio and last modified on 24-May-2015.