# LESSON: Logarithmic scales and growth rates

FOCUS QUESTION: How can I use logarithmic scales to understand rates of growth?

This lesson uses logarithmic scales combined with linear fits to understand how fast something is growing

 In this lesson you will: Use logarithmic scales to display data on multiple scales. Compute rates of change per capita to assess exponential growth. Work with logarithmic transformations and semilog scales. Calculate a linear fit lines after logarithmic transformation. Determine how fast something is growing. Set properties of multiple axes. The Metaboloic Theory of Ecology (MTE) says that influence of body size and temperature on metabolic rate are fundamental constraints that govern the behavior of ecosystems.

## 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. WorldPopulation2019.csv This data set contains world population data for the years 10,000BC to 2019 as well as estimates of the population from 2020 to 2050. Data for years before 1950 is calculated as the average of the lower and upper historical estimates of world population for those times. The data set has two columns: The first column is the year The second column is the world population in billions The population data came from the International Data Base (IDB) maintained by the US Census Bureau. The datasets and additional information can be found at: https://www.census.gov/programs-surveys/international-programs/about/idb.html.

## LESSON SETUP

• Create a LogScale directory on your V: drive and make it your current directory.
• Create a new script called LogScaleLesson.m. 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
```

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: Define variables to make code easier to read

Create a new cell in which you type and execute:

```   rWeeks = toRump(:, 1);          % Weeks in the toRump measurements
hWeeks = toHeel(:, 1);          % Weeks in the toHeel measurements
rMass = toRump(:, 3)/1000;      % Mass in kg of toRump measurements
hMass= toHeel(:, 3)/1000;       % Mass in kg of toHeel measurements
```

You should see 4 variables in the Workspace Browser:

• rWeeks - a vector of the week number for weeks 8 through 20
• hWeeks - a vector of the week number for weeks 20 through 43
• rMass - a vector of fetal mass in kg for weeks 8 through 20
• hMass - a vector of fetal mass in kg for weeks 20 through 43

## EXAMPLE 3: Plot fetal mass as a function of gestational week

Create a new cell in which you type and execute:

```   figure
hold on
plot(rWeeks, rMass, 'ko')      % Plot the toRump mass
plot(hWeeks, hMass, 'rs')      % Plot the toHeel mass
xlabel('Week');
ylabel('Mass (kg)');
title('Fetal growth by gestational age');
legend({'Crown-to-rump', 'Crown-to-heel'}, 'Location', 'NorthWest')
hold off
```

You should see a new a Figure Window with the following plot:

## EXAMPLE 4: Plot fetal mass as a function of gestational week using a semilog scale

Create a new cell in which you type and execute:

```   figure
semilogy(rWeeks, rMass, 'ko', hWeeks, hMass, 'rs') % Plot the toRump mass
xlabel('Week');
ylabel('Mass (kg)');
title('Fetal growth by gestational age');
legend({'Crown-to-rump', 'Crown-to-heel'}, 'Location', 'SouthEast')
```

You should see a new a Figure Window with a labeled plot. Notice that semilog labels the y-axis with mass values rather than by the logarithm of the mass values.

## EXAMPLE 5: Estimate fetal mass growth rates using the slope from the linear fit

Create a new cell in which you type and execute:

```   pRump = polyfit(rWeeks, log10(rMass), 1); % Fit weeks 8-20
pHeel = polyfit(hWeeks, log10(hMass), 1); % Fit weeks 20-43
corRump = corr(rWeeks, log10(rMass));     % Find correlation
corHeel = corr(hWeeks, log10(hMass));
```

You should see the following 4 variables in your Workspace Browser:

• pRump - linear fit of log of mass for weeks 8 through 20
• pHeel - linear fit of log of mass for weeks 20 through 43
• corRump - correlation of log of mass with gestational week
• corHeel - correlation of log of mass with gestational week

## EXAMPLE 6: Output the estimates of fetal mass growth rates and R^2 for the linear fits

Create a new cell in which you type and execute:

```   fprintf(['Fetal mass growth rates in g/week:\n' ...
'\t%g weeks 8-20, R^2 = %g\n' ...
'\t%g weeks 20-43, R^2 = %g\n'], ...
pRump(1), corRump*corRump, pHeel(1), corHeel*corHeel);
```

You should see the following output in the Command Window:

```Fetal mass growth rates in g/week:
0.209119 weeks 8-20, R^2 = 0.971853
0.0491613 weeks 20-43, R^2 = 0.972093
```

## EXAMPLE 7: Read in the world population data and define variables

Create a new cell in which you type and execute:

```   data = csvread('WorldPopulation2019.csv');
year = data(:, 1);
population = data(:, 2);
```

You should see the following 3 variables in your Workspace Browser:

• data - world population data
• year - vector containing the years
• population - vector containing the population (in billions) for these years

## EXAMPLE 8: Plot the world population as a function of time

Create a new cell in which you type and execute:

```   figure
plot(year, population)
xlabel('Year');
ylabel('Population (in billions)')
title('World population (10,000 BC to 2050)');
```

You should see a Figure Window containing a labeled plot. Notice that population appears to be "exponentially growing".

## EXAMPLE 9: Plot year vs. world population on a semilog scale

Create a new cell in which you type and execute:

```   figure
semilogy(year, population)
xlabel('Year');
ylabel('Population (in billions)')
title('World population (10,000 BC to 2050)');
```

You should see a Figure Window containing a labeled plot. Notice that the plot still curves upward after taking the logarithm, indicating that the population is growing faster than exponentially.

## EXAMPLE 10: Focus on the population data for 1500-2050

Create a new cell in which you type and execute:

```    yr = year(27:end);                      % Pick out years 1500-2050
pop = population(27:end).*1e+9;         % Convert to units of people so log of log works
popGRate = diff(pop)./diff(yr);         % Annual growth rate
mYr = (yr(1:end-1) + yr(2:end))./ 2;    % The yr midpoints for plotting growth rate
perCapitaGR = popGRate./pop(1:(end-1)); % Growth rate per person (birth rate)
```

You should see the following 5 variables in your Workspace Browser:

• pop - population for the years 1500-2050
• yr - the years 1500-2050
• popGRate - annual growth rate for the years 1500-2050
• mYr - the years 1500.5, 1501.5, ... 2049.5 (for plotting diff)
• perCapitaGR - growth rate per person for 1500-2050

## EXAMPLE 11: Plot the year vs the log of the log of the world population

Create a new cell in which you type and execute:

```   figure
plot(yr, log10(log10(pop)))
xlabel('Year');
ylabel('Log_{10}(Log_{10}(Population))')     % Think about label here
title('World population (1500-2050)')
```

You should see a new a Figure Window with a labeled plot. Note: a function whose log of the log appears linear has an exponential growth rate that is itself exponentially growing.

## EXAMPLE 12: Plot log of the log of population vs its growth rate

Create a new cell in which you type and execute:

```   figure
ax = plotyy(yr, log10(log10(pop)), mYr, perCapitaGR);
title('World population (1500-2050)')
box off

% Set the left axis labels and tick marks manually using the ax(1) handle
xlabel(ax(1), 'Year');
ylabel(ax(1), 'Population in billions (loglog scale)')
set(ax(1), 'YLimMode', 'manual', 'YLim', [log10(8), log10(10)]);
set(ax(1), 'YTickMode', 'manual', 'YTickLabelMode', 'manual');
set(ax(1), 'YTick', [log10(8),  log10(9), log10(10)]);
set(ax(1), 'YTickLabel', [0.1, 1, 10]);

% Set the right axis labels and tick marks manually using the ax(2) handle
xlabel(ax(2), '');                             % Turn off x-axis label
ylabel(ax(2), 'Annual growth rate per capita') % Label right y-axis
set(ax(2), 'XTickMode', 'manual', 'XTick', []); % Turn off x ticks
```

You should see a new a Figure Window with a labeled plot. This example shows how to access the properties of the individual axes for plotyy.

## SUMMARY OF SYNTAX

 MATLAB syntax Description `Y = exp(X)` returns an array `Y` that is the same size as the array `X`. Each element of `Y` is e^x where `x` is the corresponding element in the array `X`. The constant e, also known as Euler's number, is the base of the natural logarithm and has a value of approximately 2.7183. The function f(x) = e^x is the only function whose slope is exactly equal to itself at every value of x. Compute the value of e using `exp(1)`. logarithmic scale refers to plotting the logarithm of a variable rather than the variable itself. Log scales are useful for variables that have a large range of values. Log scales in common use include decibals (db) for measuring the power of sound and the Richter scale for measuring the intensity of earthquakes. Typically, the tick marks for a log scale are not equally spaced, but rather are chosen so that the unlogged values are equally spaced. See http://en.wikipedia.org/wiki/Logarithmic_scale for additional explanations and illustrations. `Y = log(X)` returns an array `Y` that is the same size as the array `X`. Each element of `Y` is natural logarithm of the corresponding element of `X`. The natural logarithm uses Euler's number e as its base. Remember that all of the elements of `X` must be positive. `Y = log2(X)` returns an array `Y` that is the same size as the array `X`. Each element of `Y` is logarithm base 2 of the corresponding element of `X`. Remember that all of the elements of `X` must be positive. `Y = log10(X)` returns an array `Y` that is the same size as the array `X`. Each element of `Y` is logarithm base 10 of the corresponding element of `X`. The log base 10 is sometimes called the common log. Remember that all of the elements of `X` must be positive. `loglog(Y)` draws a separate line graph for each column of the array `log10(Y)`. If `Y` is a vector, `loglog` draws a single line graph of `log10(Y)`. These values are plotted against the values `log10(1)`, `log10(2)`, `log10(3)`, `log10(4)`, ... . Both axes use a logarithmic scale. `loglog(X, Y)` draws a separate line graph for each column of the array `log10(Y)`. If `Y` is a vector, `loglog` draws a single line graph of `log10(Y)`. These values are plotted against the corresponding values of `log10(X)`. Both axes use a logarithmic scale. `semilogx(Y)` draws a separate line graph for each column of the array `Y`. If `Y` is a vector, `semilogx` draws a single line graph of `Y`. These values are plotted against the values `log10(1)`, `log10(2)`, `log10(3)`, `log10(4)`, ... . Only the x-axis uses a logarithmic scale. `semilogx(X, Y)` draws a separate line graph for each column of the array `Y`. If `Y` is a vector, `semilogx` draws a single line graph of `Y`. These values are plotted against the corresponding values of `log10(X)`. Only the x-axis uses a logarithmic scale. `semilogy(Y)` draws a separate line graph for each column of the array `log10(Y)`. If `Y` is a vector, `semilogy` draws a single line graph of `log10(Y)`. These values are plotted against the values `1`, `2`, `3`, `4`, ... . Only the y-axis uses a logarithmic scale. `semilogy(X, Y)` draws a separate line graph for each column of the array `log10(X)`. If `Y` is a vector, `semilogy` draws a single line graph of `log10(Y)`. These values are plotted against the corresponding values of `X`. Only the y-axis uses a logarithmic scale.

This lesson was written by Kay A. Robbins of the University of Texas at San Antonio and last modified by Dawn Roberson on 1 March 2014. Please contact kay.robbins@utsa.edu with comments or suggestions. The image is a a copy of the hand-drawn graph of Max Kleiber that appeared in Kleiber M. (1947). Body size and metabolic rate. Physiological Reviews 27: 511-541. The image is available on Wikipedia as <http://en.wikipedia.org/wiki/File:Kleiber1947.jpg>.