This notebook contains the Mathematica commands from Chapter 6 of Introduction to Scientific Programming by Joseph L. Zachary.

(6.1)  Let's calculate the effect of 6% simple interest over a period of three years.  In the beginning we have a balance of $1000.

year0 = 1000.

(6.2)  After a year passes we have 6% more than the previous year.

year1 = year0 + year0 * .06

(6.3)  After another year passes we have 6% more than the previous year.

year2 = year1 + year1 * .06

(6.4)  After a third year passes we have 6% more than the previous year.

year3 = year2 + year2 * .06

(6.5)  Let's repeat (6.1) through (6.4) using symbols instead of numbers.  In the beginning we have a balance of $p.

year0 = p

(6.6)  After a year passes we have a factor of r more than the previous year.

year1 = year0 + year0 * r

(6.7)  After another year passes we have a factor of r more than the previous year.

year2 = year1 + year1 * r

(6.8)  After a third year passes we have a factor of r more than the previous year.

year3 = year2 + year2 * r

(6.9)  Let's do some algebraic manipulations on the exprssion from (6.8) to try and discover a pattern.  We can "expand" it, which distributes products over sums.

Expand[year3]

(6.10)  We can also "factor" it.  This looks promising.

Factor[year3]

(6.11)  Let's follow this up by factoring "year2".  This appears to confirm the pattern.

Factor[year2]

(6.12)  Let's factor the balance that would exist after four years.

Factor[year3 + year3 * r]

(6.14)  Based on the pattern that we uncovered in (6.10) through (6.12), let's create a programmer-defined function "simple".  It takes an initial balance "p", an interest rate "r", and a number of years "n", and returns the balance after "n" years.

simple[p_, r_, n_] := p * (r+1)^n

(6.15)  We can use "simple" to calculate the balance that will result by investing $1000 at 6% interest for 500 years ...

simple[1000, .06, 500]

(6.16)  ... and the balance that will result by investing $B at 10% interest for 8 years.

simple[B, .10, 8]

We record the population of the United States on January 1, 1994.

jan94 = 259167000.

And on January 1, 1995.

jan95 = 261638000.

(6.17)  If the population of the United States were growing according to a simple interest model, "simpleGrowth" would be the annual interest rate at which it grew in 1994.

simpleGrowth = (jan95 - jan94) / jan94

(6.18)  By applying the simple interest model to the population growth of the United States, we can project the population on January 1, 2000.

simple[jan94, simpleGrowth, 6]

(6.19)  We can use the simple interest model to calculate compound interest.  Here we calculate the result of investing $1000 at 6% annual interest, compounded daily, for a period of one year.

simple[1000., .06/365, 365]

(6.20)  Here we repeat (6.19) symbolically.  We calculate the result of investing $p at r% annual interest, compounded daily, for a period of one year.

simple[p, r/365, 365]

(6.21)  We continue to generalize.  Here, we calculate the result of investing $p at r% annual interest, compounded daily, for a period of "n" years.

simple[p, r/365, 365*n]

(6.22)  Finally, we calculate the result of investing $p at r% annual interest, compounded "m" times per year, for a period of "n" years.  This gives us a general expression for compound interest.

simple[p, r/m, m*n]

(6.23)  We convert the expression from (6.22) into a "compound" function.  It takes as parameters an initial balance "p", an interest rate "r", a period of years "n", and the number of compounding intervals per year "m".  It returns the result of investing $p at r% annual interest, compounded "m" times per year, for "n" years.

compound[p_,r_,n_,m_] := p * (1 + r/m)^(m*n)

(6.24)  We use "compound" to calculate the result of investing $1000 at 6% annual interest, compounded daily, for 500 years.

compound[1000, .06, 500, 365]

(6.26)  We use "Solve" to find find the simple interest rate "r" that corresponds to a 6% investment compounded daily.

Solve[simple[p, r, 1] == compound[p, .06, 1, 365], r]

(6.27)  We try to use "Solve" to find an interest rate "r" that we can use to model the growth of the population of the United States in 1994, under the assumption that the growth occurs daily.  Unfortunately, "Solve" is unable to solve the 365th-degree polynomial equation.

Solve[jan95 == compound[jan94, r, 1, 365], r]

(6.30)  We can rearrange the equation from (6.27) into a form with one root root that "Solve" can find.

Solve[1 + r/365 == (jan95/jan94)^(1/365), r]

(6.32)  We find the limit of a compound interest investment as the number of compounding intervals tends to infinity.  This is the continuous interest formula.

Limit[compound[p, r, n, m], m->Infinity]

(6.33)  We use the expression from (6.30) to create a function called "continuous".  It takes as parameters an initial balance "p", an interest rate "r", and a number of years "n", and returns the balance that will result from investing $p at r% continuous interest for "n" years.

continuous[p_,r_,n_] := p*Exp[n*r]

(6.34)  We use "solve" to find the continuous interest rate "r" that will allow us to model the growth of the population of the United States during 1994 as a problem in continuous interest.

Solve[jan95 == continuous[jan94,r,1], r]

(6.35)  We save the value for "r" found in (6.34) in the variable "continuousGrowth".

continuousGrowth = 0.00948923

(6.36)  We use the result of (6.34) to create a function "dailyPop" that takes "days" as its parameter.  It returns the population of the United States "days" days after January 1, 1994, assuming that the population grows according to the continuous interest model.  Notice that this function is defined in terms of another programmer-defined function, "continuous".

dailyPop[days_] := continuous[jan94, continuousGrowth, days/365.]

(6.37)  We use "dailyPop" to project the population of the United States at midnight on February 1, 1994.

dailyPop[31]

(6.38)  We use "dailyPop" to project the population of the United States at 6:00 am on February 1, 1994.

dailyPop[31.25]

(6.39)  We use "InputForm" to look at all of the digits of the solution we originally round in (6.34).

InputForm[Solve[jan95 == continuous[jan94,r,1], r]]

(6.40)  We use "dailyPop" to project the population of the United States at midnight on January 3, 1994.

dailyPop[2]

(6.41)  This is how we would have to do the calculation from (6.36) if we hadn't defined the "dailyPop" function.

259167000 * Exp[2/365 * .00948923]

(6.42)  Once a variable has been given a value via assignment, it cannot be used as a symbolic constant.

Solve[simpleGrowth^2 == 5, simpleGrowth]

(6.43)  We can "unassign" a variable by using "Clear".

Clear[simpleGrowth]

(6.44)  Once a variable has been "unassigned", it can be used as a symbolic constant once again.

Solve[simpleGrowth^2 == 5, simpleGrowth]

(6.46)  An example of the use of the "Sum" function.

Sum[n^2, {n,1,100}]

(6.47)  An example of using the "Sum" function to compute a sum over an infinite number of terms.

Sum[1/2^n, {n,1,Infinity}]