I will ask students to gather into their problem groups. Each group will work on their problem at one computer. I will walk around and try to facilitate this effort. As students solve their problems, they may also try to help other groups.
If there is a great deal of time left, one or two groups may present their solutions. Some other groups will present their solutions next time. We might go on to ...
We've been looking at sequences which are created by taking the "old value" and adding on a some number depending on n. So, for example, we could have looked at the sum of the squares:
There is another way to think about this sequence. It is a sequence
which has a starting value, v(1), which equals 1, and whose growth is
defined by the equation v(n)=v(n-1)+n^2. So we could tell Maple that
the sequence is defined by these instructions:
if n=1 then the number is 1 else the number at n is the "old" number at n-1 plus n^2.This method of defining the sequence is called recursive and uses "old" or earlier values of the function to define its current value. In many applications, recursive definitions are more natural and actually easier to understand than other definitions. For example, in biological and chemical applications, thinking about how things change during a recent time interval may give a very useful point of view rather than attempting to precribe the total change over a long time.
In this definition, the words if
and then and else are logical directions to Maple. They work like this:
if some logical test
then do this if the test reports "true"
else do this if the test reports "false".
This is a more complicated form of instruction than the formula we had
involving add. The standard way to tell
Maple this sort of instruction is a
procedure statement. Here is an example
of such a statement.
> frog:=proc(n) if (n=1) then 1 else frog(n-1)+n^2 end if end proc;
The words end if tell Maple that the whole if .. then .. else statement is done, and the words end proc tell Maple that the procedure definition is done.
The Maple response to this instruction
(if I type it correctly!) is
frog := proc(n) if n = 1 then 1 else frog(n - 1) + n^2 end if end proc
That is, Maple just repeats the definition back, and stores it for later use. How can we use it? Look:
> frog(2); 5The result is the same as 12+22. What Maple does is begin computing frog(2) with an if/then/else. Since 2>1, it decides it must compute frog(1) and then add 22 to that result, so we get 1+22=5 as the result.
I should warn you about something, though. There is a penalty for using this sort of logical construction, and that penalty is in higher running time and more storage space. For example, the computations of frog(1000) and add(j^2,j=1..1000) both return the same result (333833500, surely!) but when I just tried both in different Maple sessions, the recursive definition took twice as much time and space on my home PC as the direct computation. This may not matter in such toy examples, but in real computations in complicated situations, such resource demands may be important.
Let me now define a different sort of sequence, one where a recursive definition seems to be the simplest and other ways of looking at it may be more difficult. Here is a Maple procedure defining toad:
> toad:=proc(n) if (n=1) then 1 else 3*toad(n-1)+1 end if end proc;
What will the result of seq(toad(n),n=1..3); be? I hope that students will think this through but here is the idea of the computation.
> seq(toad(n),n=1..3); 1, 4, 13This is just as predicted.
This toad is a more complicated sequence. I'd like to try to get a formula for it and verify the formula. Please notice that if the "+1" were omitted in the definition, the way we'd get from one term to the next would be just to multiply by 3. So a formula for the sequence whose first term was 1 and which had the recursive rule s(n)=3*s(n-1) would be 3n-1. I bet that the formula for our toad sequence is related to 3n. The variable is now in the exponent, not in the base. How can we guess the formula? Once it is guessed, I will show you how we can verify it in a way quite similar to what we've done.
There are ways to guess the formula, but let me show you
How to cheat with sequences
There is a wonderful web resource created by Neil Sloane called the Encyclopedia of Integer Sequences. This contains over one hundred thousand entries -- wonderful integer sequences and a great deal of weird and wonderful information about them. Neil Sloane has told me that many strange things have occurred as people have "given" him sequences for his Encyclopedia. But let's use his data base.
For example, look at this Maple command and its response:
> seq(toad(n),n=1..10); 1, 4, 13, 40, 121, 364, 1093, 3280, 9841, 29524We can take the response and put it into the search bar, and the result is Sequence A003462. The initial information given is (3^n-1)/2, a formula for the sequence!
Here is a complete verification that the formula is correct.
> zebra:=n->(3^n-1)/2; n zebra := n -> 1/2 3 - 1/2 > zebra(1); 1 > zebra(n)-(3*zebra(n-1)+1); n (n - 1) 3 3 3 ---- - ---------- 2 2 > simplify(%); 0The first instruction defines a function, zebra, using the formula we got from the Encyclopedia. The second instruction verifies the value of zebra(1), the then part of the definition of toad. Now we need to check that zebra grows in the same way that toad grows -- that is, that the else part of the definition is satisfied. So the third instruction about compares zebra(n) with 3*zebra(n-1)+1 using subtraction (I am careful with parentheses!). The result is a bit complicated, but it should be 0. And with a "call" to simplify we indeed get 0.
Confession or boast? Many other people and I use the Encyclopedia a great deal. The term paper project I mentioned at the first meeting will be connected with the Encyclopedia.
Next time ...
Some groups will present their solutions, and I will show you some very old and very new recursive sequences.
Maintained by firstname.lastname@example.org and last modified 9/21/2008.