LESSON: 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

Contents

DATA FOR THIS LESSON

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.

SETUP FOR LESSON

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:

EXERCISE 1: Diagramming an array
Using sentences, describe 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:

EXERCISE 2: Find the weekly rate of change of mass in kg/week.


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

plotyy plots 2 variables on left and right y-axis respectively. The blue line works to the left (blue) y-axis, and the green plot works with the right (green) y-axis. The arguments are given in the order: left x-axis, left y-axis, right x-axis, right y-axis.

You should see a Figure Window with the following graph:

EXERCISE 3: Display weight 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:

EXERCISE 4: Find the weekly rate of change of fetal length during early gestation (in cm/week).


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])
   subplot(2, 1, 1, 'XGrid', 'on')                % Top panel
   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')                 % Bottom panel
   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: 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:

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 24 May 2015. Please contact kay.robbins@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 .