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:

Contents
 DATA FOR THIS LESSON
 SETUP FOR LESSON
 EXAMPLE 1: Load the fetal size data
 EXAMPLE 2: Merge the time and weight (mass) variables from the two data sets
 EXAMPLE 3: Calculate the weekly rate of change of fetal weight
 EXAMPLE 4: Plot the weight and rate of change of weight
 EXAMPLE 5: Calculate rate of change (slope) of fetal length in inches per week
 EXAMPLE 6: Find the midpoints of the weeks intervals for plotting
 EXAMPLE 7: Plot the weekly rate of change of fetal length in inches/week
 EXAMPLE 8: Calculate the percentage change in fetal weight
 EXAMPLE 9: Calculate the percentage change in fetal weight
 SUMMARY OF SYNTAX
DATA FOR THIS LESSON
File  Description 
toRump.txt toHeel.txt 
These data sets contain typical fetal size as a function
of gestational week:
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/averagefetallengthweightchart. 
SETUP FOR LESSON
 Create a RatesOfChange directory on your V: drive and make it your current directory
 Download the toRump.txt and toHeel.txt to your RatesOfChange directory.
 Create a RatesOfChangeLesson.m script file in your RatesOfChange directory. Enter each of the examples in a new cell in this script.
EXAMPLE 1: Load the fetal size data
Create a new cell in which you type and execute:
load toRump.txt; % Load the crowntorump data load toHeel.txt; % Load the crowntoheel data
You should see the following 2 variables in your Workspace browser:
 toRump  measures the variation up through week 20
 toHeel  measures the variation from week 20 to term
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:
 weeks  the gestational week
 mass  mass of a typical fetus at that gestational week
 weight  the weight of a typical fetus at that gestational week
Verify that these are the first and third columns of the two data sets after they have been put endtoend 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:(end1)) + weeks(2:end))./2; % Week midpoints for plotting
You should see the following 2 variables in your Workspace browser:
 poundsPerWeek  rate of change of weight per week (in lbs/week)
 weekMid  a vector of points at the half weeks rather than weeks
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 xaxis of left axis ylabel(ax(1), 'Fetal weight (lbs)') % Label yaxis of left axis ylabel(ax(2), 'Fetal growth rate (lbs/wk)') % Label yaxis of right axis title('Characterization of fetal weight during pregnancy') % Title one of the axes
plotyy plots 2 variables on left and right yaxis respectively. The blue line works to the left (blue) yaxis, and the green plot works with the right (green) yaxis. The arguments are given in the order: left xaxis, left yaxis, right xaxis, right yaxis.
You should see a Figure Window with the following 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:
 weeksRump  vector of week numbers in crowntorump measurements
 weeksHeel  vector of week numbers in crowntoheel measurements
 inchesRump  vector of lengths in crowntorump measurements
 inchesHeel  vector of lengths in crowntoheel measurements
 inchesPerWeekRump  vector of slopes inches/week in crowntorump measurements
 inchesPerWeekHeel  vector of slopes inches/week in crowntoheel measurements
EXAMPLE 6: Find the midpoints of the weeks intervals for plotting
Create a new cell in which you type and execute:
rumpMid = (weeksRump(1:(end1)) + weeksRump(2:end))./ 2; heelMid = (weeksHeel(1:(end1)) + weeksHeel(2:end))./ 2;
You should the following two variables in your Workspace Browser:
 rumpMid  a vector of points at the half weeks for early gestation
 heelMid  a vector of points at the half weeks for late gestation
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({'Crowntorump', 'Crowntoheel'}, '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:end1);
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 xaxis of left axis ylabel(ax(1), 'Fetal weight (lbs)') % Label yaxis of left axis ylabel(ax(2), '% change') % Label yaxis 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
nonsingleton 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 twoelement vector holding the
handles for the respective axes.

xlabel(ax, 'label') 
labels the xaxis of the axis designated by ax with the word
label . If you omit ax , MATLAB labels
the current axis.

ylabel(ax, 'label') 
labels the yaxis 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 3Dimage of a fetus created by Gebruiker Mvandergaast.
The image is available on Wikipedia as