Mathematics
Linear, Geometric and Exponential Growth

How fast things grow (and the opposite, how fast things decay) is an important issue for scientists in many areas. With this lesson we will use Mathematica to explore the differences between Linear, Geometric and Exponential growth.
	The Scenario:  A long-lost, wealthy aunt has recently passed away, leaving you as one of three beneficiaries in her will. You are given the choice between three stock investments, which pay the following returns over the next year-and-a-half.
	Lines & Things: $10,000 per month for 18 months.
	American Explus : $1,500, first month. Add $1,500 to the previous payment for each following month ($3,000 the second, $4,500 the third, $6,000 the fourth, and so on) for 18 months
	Ratio Shack:: $1 (yes, one dollar), first month. Multiply the previous payment by two for each following month ($2 the second, $4 the third, $8, $16 and so on) for 18 months.
	The stocks you do not choose will be used to maintain a bountiful supply of Luv Your Kitty gourmet cat food for your aunt's great white persian, Cujette, and to maintain the family mansion for the her beloved komodo dragon, Fluffles. Which choice is best for your pocketbook?

Mathematica is an excellent calculator for a problem like this, especially because we can keep track of results as we go along and use plotting functions to help visualize the mathematical relationships. Lets begin by defining the number of months for payment.

NumMonths  =  18;

Starting with the first option, we define a variable to keep track of the total amount paid out (LTtotal), which at first is zero.

LTtotal  =  0;

We will also use a variable to keep track of how much is paid out each month, which in the case of Lines & Things, is a constant value of $10,000.

LTmonthly  =  10000;

To figure out how much we have made after the first month, we simply add the current monthly income to the current total.

LTtotal  +  LTmonthly

In order to keep track of this value, however, Mathematica needs to assign it to a variable.  We assign the value to LTtotal, because it is, after all, the variable we are using to keep track of our total!

LTtotal  =  LTtotal  +  LTmonthly

Our total after one month is $10,000.  Executing the same command gives us the total after two months:

LTtotal  =  LTtotal  +  LTmonthly

There is a short-cut syntax for adding a value to a running total.  The following command adds LTmonthly to the current LTtotal to get the 'next' value, in this case for month three.

LTtotal  +=  LTmonthly

We want to repeat this process some number of times equal to NumMonths (in this case a total of 18 times). We let Mathematica do the work for us by using a loop function. One common loop function takes the form Do[expr,{i,imin,imax}]. The loop uses a counter, in this case represented by the variable i, to count from imin  to imax, each time evaluating expr.  To repeat our addition for the right number of months, we simply place the command inside a Do loop. Notice that we reset LTtotal to zero before doing the loop (Why?)

LTmonthly  =  10000;
LTtotal  =  0;
Do[
LTtotal  +=  LTmonthly, 
{i, 
        1, NumMonths}]
LTtotal

As expected, the total payout is $180,000 (18 months times $10,000). However, what if we want to know intermediate amounts, like how much we have after one year (12 months).  We could redo the calculation, changing NumMonths to twelve, however, that could get tedious if we need to do it for each month. Notice, however, that during the calculation, Mathematica actually did calculate the total for each month (it was stored in LTtotal).  One way to keep track of that information is to use the Table command, which acts like the Do command as far as repeating an expression, but actually stores the results in a list, which in this case we will name LTList.

LTmonthly  =  10000;
LTtotal  =  0;
LTList  =  
      Table[
LTtotal  +=  
          LTmonthly, 
{i, 1, NumMonths}]

Now if we want to know what the total is after a particular month, we can simply look at a certain part of the list. For example, after 8 months, Lines & Things will have earned $80,000.

LTList[[8]]

We can use ListPlot to see the progression of earnings from Lines & Things

ListPlot[LTList];

Connecting the dots makes it even more evident why this type of growth would be called linear growth,

LTtotgraph  =  
        ListPlot[LTList, PlotJoined -> True, 
          PlotStyle -> {RGBColor[1, 0, 0]}];

Now let's take a look at option #2, "American Explus". We can begin by setting the total and monthly payouts

AEtotal  =  0;
AEmonthly  =  1500;

Notice, however, that each month the total changes, and the amount per month changes! Each month we need to add $1500 to the monthly payout, as well as add the payout to the total.

AEmonthly  +=  1500;
AEtotal  +=  AEmonthly

Wait a minute! After month one we should only have $1500!  What happened?
	Notice that we added $1500 dollars to AEmonthly before adding AEmonthly to AEtotal, which means our first monthly payment was $3000 and not $1500.  There are two ways we can fix this - either switch the order of the additions, or start AEmonthly at zero. Either way we need to reset variables and start over. Here we decide to keep the same order, but start the monthly payments at zero.  This means that after every calculation, AEmonthly will be storing the most recent monthly payment. If we choose the other method, AEmonthly would instead store the next expected payment. Notice that we must be careful and pay attention to what is really stored in each variable! 
	We would still like to set our variables at the beginning, though, so we add a variable AEaddmonthly for the regular monthly increase in payments.

AEaddmonthly  =  1500;
AEmonthly  =  0;
AEtotal  =  0;
AEmonthly  +=  AEaddmonthly;
AEtotal  +=  AEmonthly

We again use the Table function to generate a list of results. Notice that because we must to more than one calculation inside the table, we use parentheses to group them. We are also potentially interested in how the monthly salary changes, i.e. what the value of AEmonthly is, so we store both values as a short list in the table.

AEaddmonthly  =  1500;
AEmonthly  =  0;
AEtotal  =  0;
AEList  =  
      Table[
(AEmonthly  +=  
            AEaddmonthly; 
AEtotal  +=  
            AEmonthly; 
{AEmonthly, 
            AEtotal}), 
{i, 1, NumMonths}]

For any given month, the first number represents the monthly payment for that month, and the second number represents the total payout up to that point.

AEList[[8]]

We can access a monthly value by itself by using an index for the first part .

AEList[[8, 1]]

Similarly, we can access a monthly total with an index for the second part .

AEList[[8, 2]]

It is also a simple matter to show All the monthly totals:

AEList[[All, 2]]

Once again we can use ListPlot to see the progression of earnings.

AEtotgraph = 
        ListPlot[AEList[[All, 2]], PlotJoined -> True, 
          PlotStyle -> {RGBColor[0, 1, 0]}];

We can also plot the list of the monthly earnings as well.

AEmonthgraph = 
        ListPlot[AEList[[All, 1]], PlotJoined -> True];

Notice that the change in monthly payments is linear! Geometric growth is actually characterized by a linear rate of change.
	On to the third option! We should be able to copy the process from option two, with some minor changes...

RSmultmonthly  =  2;
RSmonthly  =  1.0;
RStotal  =  0;
RSList  =  
      Table[
(RSmonthly  *=  
            RSmultmonthly; 
RStotal  +=  
            RSmonthly; 
{RSmonthly, 
            RStotal}), 
{i, 1, NumMonths}]

Are the results correct? Apparently not, as in the first month we made $2, not the expected $1. Again the problem comes up with what exactly is being stored.  Before we start the simulation, we need to set a value for RSmonthly. However, the first thing that happens is that the value is multiplied by RSmultmonthly. In order for the first month to be correct, we need to divide the expected value (1.0) by the multiplier (in this case, 2). Let's look at the revised version:

RSmultmonthly  =  2;
RSmonthly  =  1.0/RSmultmonthly;
RStotal  =  0;
RSList  =  
      Table[
(RSmonthly  *=  
            RSmultmonthly; 
RStotal  +=  
            RSmonthly; 
{RSmonthly, 
            RStotal}), 
{i, 1, NumMonths}]

That looks about right, for the first few months anyway. Now we can look at plots of the monthly totals and the monthly payouts.

RStotgraph  =  
        ListPlot[RSList[[All, 2]], PlotJoined -> True, 
          PlotStyle -> {RGBColor[0, 0, 
                1]}];
RSmonthgraph  =  
        ListPlot[RSList[[All, 1]], 
          PlotJoined -> True];

Both plots look very similar, with the only notable exception being the scale of the vertical axis! As is true of any exponential growth function, the growth itself, as well as its rate of change, will demonstrate a characteristic period of slow increase followed by increasingly rapid increases over time.
	Let us compare the final totals for each option from the total values after month 18.

{LTList[[NumMonths]], AEList[[NumMonths, 2]], 
      RSList[[NumMonths, 2]]}

Apparently the Ratio Shack stock, in spite of the $1 starting payout, is the most valuable after 18 months.  Would this still be true if there were only 12 months of payouts?

{LTList[[12]], AEList[[12, 2]], RSList[[12, 2]]}

Apparently not! In fact, the Ratio Shack total after 12 months is quite a bit lower than the other two, and the regular payments from Lines & Things make the most sense if the limit is twelve months. 
	We've given names to each of our plots, so it is now a simple matter to Show them all simultaneously, which can give us a better picture of the relative worth of the payouts.We use Table to generate lists of locations and specify Ticks on the horizontal axes and Gridlines for in both directions.

Show[LTtotgraph, AEtotgraph, 
        RStotgraph, 
Ticks  ->  {Table[i, {i, 18}], 
            Automatic}, 
        GridLines -> {Table[i, {i, 18}], 
            Table[50000 i, {i, 5}]}];

For what months is the total payout for Lines & Things the most?  When does that change? Which months are best using American Explus? When does that change?

Now for the final challenge...
	With the newfound wealth from your aunt, you have purchased one hundred acres of prime farmland in North Carolina.  You have three options as to how to use the land.
	Option 1: Plant strawberries! You estimate that profits from 'self-pick' sales should be about $1,400 per acre per year.
	Option 2: Raise hogs! After investing your remaining money in hog pens and hog-waste treatment facilities, you should be able to purchase ten breeding sows to start your operation.  You figure that for each sow you can count on an annual brood of five 'prime' piglets to be fattened and sold to market at $1,000 per head. You also plan to increase your number of breeding sows by two each year through minor revenue from fertilizer sales. 
	Option 3: Raise chickens! After covering the land with chicken pens, you should have just enough money for two laying hens, each of which will provide $5 per year profit in egg sales.  Adding a rooster or two as well as a chick nursery, you figure that you should be able to add 50% to your number of laying hens every year as well.

How long do you want to stay in farming? After all the effort of buying the farm, you figure at least five years, but probably less than thirty-five.  Pick a number of years to work on the farm. Which option do you suppose will earn you the most money in that period of time? 
	Modify the functions from the first example to calculate and plot the total profits for each farming option.  For what years is option 1 the best? Option 2? Option 3? 
	How long would you have two work to make $1 million with each type of farm? How long to make $5 million?

Whenever we find ourselves repeating a calculation or a process, it often makes sense to define a reusable program or function in Mathematica or another language to make things easier.  If we notice that all of our growth calculations have some similar elements, we can design a function that includes those similar pieces, and allows us to change the other pieces.
	In the function GrowthFunc, we expect four parameters, i.e. pieces of information that can change depending on how the function is used.  startval is the expected value for the first time period, multip is the amount the regular 'payout' will be multiplied by each time period, addin is the amount added to the regular growth for each time period, and length is the number of time periods, as well as the length of the list. Notice that multiplication occurs before addition, and that in order to get the first month correct, we subtract and then divide the inital value at the very beginning.
	 The := assignment is used to tell Mathematica "don't evaluate this now, evaluate it when it is called for later", and is the correct form for defining a function.

GrowthFunc[startval_, multip_, addin_, 
        length_]  := 
(GFmonthly  =  (startval - 
                addin)/multip; 
GFtotal  =  
          0; 
Table[
(GFmonthly  *=  
              multip; 
GFmonthly  +=  
              addin; 
GFtotal  +=  
              GFmonthly; 
{GFmonthly, 
              GFtotal}), 
{i, 1, length}])

So how do we calculate for each option?  In the first case, the starting value is $1400 for each of 100 acres, so we put in 1400*100 in the first location, which represents the startval.  We don't want the annual value to increase, so we use a multiplier value of 1 and an add-in value of 0, which will leave the value as-is. The final value indicates that we are interested in calculations for a span of 32 years.

Opone  =  GrowthFunc[1400*100, 1, 0, 32];

For the second option, we expect $1000 for each of 5 offspring from each of 10 sows for a startval of  1000*5*10. Annual value will increase linearly, so we still use a multiplier value of 1, but expect a regular increase of $1000 for each of 5 offspring for 2 new sows every year (1000*5*2), again for 32 years.

Optwo  =  GrowthFunc[1000*5*10, 1, 1000*5*2, 32];

The third option starts with a small payout of only $5 for each of two hens (5*2), but has a multiplier value of 1.5,which we might well expect to make up for no linear increase (add-in value of 0), epecially over the course of 32 years.

Opthree  =  GrowthFunc[5*2, 1.5, 0, 32];

Now we can plot the values of the three options and Show them in a single graph for comparison. As might be expected, the results are similar to those for example 1.  What might you change to make the graphs look different?

Show[
        ListPlot[Opone[[All, 2]], PlotJoined  ->  True, 
          PlotStyle -> 
            RGBColor[1, 0, 0]], 
     ListPlot[
          Optwo[[All, 2]], PlotJoined  ->  True, 
          PlotStyle -> 
            RGBColor[0, 1, 0]], 
        ListPlot[
          Opthree[[All, 2]], PlotJoined  ->  True, 
          PlotStyle -> 
            RGBColor[0, 0, 1]] , 
GridLines -> {Table[
              i, {i, 32}], 
            Table[1000000 i, {i, 8}]}];

This notebook created by Dr. Garrett R. Love for the Shodor Education Foundation. Copyright 2001