# This worksheet contains the Maple 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, and
# January 1, 1995.
#
> jan94 := 259167e3;
> jan95 := 261638e3;
#
# (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 * (R/m + 1)^(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(simple(P, simpleGrowth, 1) = compound(P, R, 1, 365), R);
#
# (6.30) 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.31) 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.32) 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.
#
> continuousGrowth := solve(simple(P, simpleGrowth, 1) = continuous(P,
> R, 1), R);
#
# (6.33) We use the result of (6.32) 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.34) We use dailyPop to project the population of the United States
# at midnight on February 1, 1994.
#
> dailyPop(31);
#
# (6.35) We use dailyPop to project the population of the United States
# at 6:00 am on February 1, 1994.
#
> dailyPop(31.25);
#
# (6.36) We use dailyPop to project the population of the United States
# at midnight on January 3, 1994.
#
> dailyPop(2);
#
# (6.37) 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 * .009489227532);
#
# (6.38) Once a variable has been given a value via assignment, it
# cannot be used as a symbolic constant.
#
> solve(simpleGrowth^2 = 5, simpleGrowth);
#
# (6.39) We can "unassign" a variable by assigning it to a quoted
# version of itself.
#
> simpleGrowth := 'simpleGrowth';
#
# (6.40) Once a variable has been "unassigned", it can be used as a
# symbolic constant once again.
#
> solve(simpleGrowth^2 = 5, simpleGrowth);
#
# (6.42) An example of the use of the sum function.
#
> sum(n^2, n=1..100);
#
# (6.43) An example of using the sum function to compute a sum over an
# infinite number of terms.
#
> sum(1/2^n, n=1..infinity);
>