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:
|
|
| File | Description |
toRump.txttoHeel.txt |
These data sets contain typical fetal size as a function
of gestational week.
toRump.txt data measures length from baby crown
to baby rump during early gestation.toHeel.txt measures the length
from baby crown to baby heel during later gestation. |
population.txt |
This data gives actual and projected population data for the US, China,
and India for the period 1950 to 2050.
|
Contents
- SETUP FOR LESSON 7
- 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: Load population data for US, China and India
- EXAMPLE 9: Calculate the year midpoints for plotting
- EXAMPLE 10: Calculate the annual growth rate for each country
- EXAMPLE 11: Plot the annual population growth rates for the three countries
- EXAMPLE 12: Calculate the per capita annual population growth rates for the three countries
- EXAMPLE 13: Plot the per capita annual population growth rates for the three countries
- SUMMARY OF SYNTAX
SETUP FOR LESSON 7
- Set the Current Directory to Z:\working\MATLAB\Lesson7. (You will need to make a new directory for Lesson7.)
- Download the toRump.txt and toHeel.txt data files to your Lesson7 directory.
- Download the population.txt data file to your Lesson7 directory.
- Create a new script called Lesson7Script.m. (Use File->New->Blank M-File from the main MATLAB menubar.) You will 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 crown-to-rump data load toHeel.txt; % Load the crown-to-heel 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
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 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:
- poundsPerWeek - rate of change of weight per week (in lbs/week)
- weekMid - a vector of points at the half weeks rather than weeks
- Define a variable called kgPerWeek that contains the weekly rate of change of mass of a fetus during gestation in units of kg/week.
- Enter your definitions in this cell and execute the cell to create this variable.
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:
- weeksRump - vector of week numbers in crown-to-rump measurements
- weeksHeel - vector of week numbers in crown-to-heel measurements
- inchesRump - vector of lengths in crown-to-rump measurements
- inchesHeel - vector of lengths in crown-to-heel measurements
- inchesPerWeekRump - vector of slopes inches/week in crown-to-rump measurements
- inchesPerWeekHeel - vector of slopes inches/week in crown-to-heel measurements
- Define a variable called cmPerWeekRump that contains the weekly rate of change of the length of a fetus in cm during early gestation.
- Define a variable called cmPerWeekHeel that contains the weekly rate of change of the length of a fetus in cm during later gestation.
- Enter your definitions in this cell and execute the cell to create these variables.
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:
- 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]) % 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:
- pop - an array containing population information for 3 countries
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:
- years - vector of years for 1950 to 2050
- yearMid - vector of year midpoints for 1950 to 2050
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:
- USGR - vector of population growth of the US in people/year
- chinaGR - vector of population growth of the China in people/year
- Define a variable called indiaGR that contains the yearly rate of change of the population of India for 1950 to 2050.
- Enter your definitions in this cell and execute the cell to create this variable.
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:
- USPerCapGR - population growth rate per capital for the US
- chinaPerCapGR - population growth rate per capital for the China
- Define a variable called indiaPerCapGR that contains growth rate per capita of India for 1950 to 2050.
- Enter your definitions in this cell and execute the cell to create this variable.
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>.