These are some examples of sequences A 1

  • Slides: 28
Download presentation

These are some examples of sequences: A. ) { 1, 2, 3, 4, 5,

These are some examples of sequences: A. ) { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11. . } B. ) { 6, 3, 3, 2, 4, 1 } C. ) { 3, 6, 12, 24, 48, 96, 192, 384, 768. . } A sequence is an ordered list of objects, usually numbers, ordered meaning that {1, 2, 3} is not the same as {3, 1, 2}. An element or term of a sequence is an object in that sequence. A sequence can be infinite like A and C, meaning it goes on forever, or finite like B. Like the 3 in B, it’s fine for the same object to pop up more than once.

A recursive sequence has a rule associated with it that relates an element to

A recursive sequence has a rule associated with it that relates an element to the ones that come before it. For example: A. ) { 3, 6, 12, 24, 48, 96, 192, 384, 768. . } Each element in this sequence is twice the element before it; for example, 192 = 2*96. This is true for all of the elements except the 3 at the beginning, which obviously doesn’t have an element before it to multiply by two. B. ) { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89. . } This is another example, called the Fibonacci sequence. Each element is the sum of the two before it; 5 = 3+2, 21= 13+8. In this case there are two values that this does not work for, the 0 and 1 at the beginning. For any given recursive sequence, some of the values at the beginning don’t have enough before them to look at. These values have to be given and are called initial values. The further back the rule looks, the more initial values there will be. Sequence A looks one element back, and has one initial value (3); Sequence B looks two elements back and has two initial values (0 and 1)

A. ) { 3, 6, 12, 24, 48, 96, 192, 384, 768. . }

A. ) { 3, 6, 12, 24, 48, 96, 192, 384, 768. . } B. ) { 1, 2, 4, 8, 16, 32, 64, 128, 256. . } Both of these sequences work on the rule that each element is twice the one before it. If we call an the nth element, the “twice the one before it” rule can be written: an = 2 * an-1 and an initial value could be, for ex: a 0 = 3 Both A and B follow that rule. The only way they are different is that the initial value is 3 for A, and 1 for B. We would like to be able to point out that they are so similar, so if two sequences follow the same recursion rule, we say that they satisfy the same recurrence relation. In fact we’ll just start calling ‘rules’ recurrence relations. There are many sequences that satisfy a given recurrence relation, like A and B satisfy an = 2* an-1. Each sequence is different from the others only based on its given initial values. Given a recurrence relation and the required initial values, there is only one sequence that works – the solution is unique. So if two sequences have the same initial values and recurrence relation, they are the same sequence.

Programming a recursive sequence is pretty easy. All you have to know is the

Programming a recursive sequence is pretty easy. All you have to know is the recurrence relation and the initial values. Say the relation is an = 2*an-1 + 3*an-2. Since it reaches back to n-2, we’ll need two initial values; call them a 0=0 and a 1=4. Then a program might look like this: public int some. Function ( int n ) { if (n==0) return 0; if (n==1) return 4; return 2*some. Function(n-1)+3*some. Function(n-2); } This version is easy to code, but not necessarily efficient to calculate. Not only will there be duplication of effort (calculating some. Function(4) will compute some. Function(2) twice), but for large values of n you’ll end up with a large stack of recursive calls.

It turns out that the nth value of the relation described by the rule

It turns out that the nth value of the relation described by the rule an = 2*an-1 + 3*an-2 and the initial conditions a 0=0 and a 1=4 can be calculated with the equation an = 3 n – (-1)n. Calculating this value with this equation is much more efficient than using the recursive rule. The rest of this powerpoint is devoted to: • Part 2 : Showing examples of problems involving this type of recurrence relation. • Part 3 : Describing a special subset of recurrence relations for which we can come up with an explicit equation. • Part 4 : Sample problems using this technique.

 • What is a sequence? • What is a recursive sequence? • What

• What is a sequence? • What is a recursive sequence? • What is an initial value? • What is a recurrence relation?

So, you get what recurrence relations are; but what’s the point? Many problems can

So, you get what recurrence relations are; but what’s the point? Many problems can be reduced to patterns that can be described by recurrence relations. Here’s an example: How many bit-strings (ex 1001, 010110100, 10111 etc) of some length n that have no consecutive 0 s are there? How would you write a program to solve this? First, recognize that every valid bit string ends in either a 1 or a 0. Valid strings of length 3: 111 , 101, 010, 110 Given all the strings of length 3, to make the strings of length 4, you can: • Take every string and add a 1 to it (always safe to add a 1) • Take every string that ends with a 1 and add a 0 to it (only safe to add a 0 if it currently ends with a 1) [Continues on next page]

So how many strings does this rule represent? For n bits, • Take every

So how many strings does this rule represent? For n bits, • Take every string and add a 1 to it (always safe to add a 1) A copy of everything we have with a 1 added to it • All the valid strings using n-1 bits • Take every string that ends with a 1 and add a 0 to it (only safe to add a 0 if it currently ends with a 1) Everything that we had two steps ago, with a 1 added and then a 0 added • All the strings that were valid at n-2 bits In other words, the number of strings of length n = number of strings of length n – 1 + number of strings of length n -2 or: an = an-1 + an-2

Confused? If so, it may help to actually write out the valid strings of

Confused? If so, it may help to actually write out the valid strings of n bits: Bits Strings # of Strings 0 ? 1 1, 2 3 4 11, 111, 0 10, 101, 2 01 011, 3 010 1111, 1110, 1101, 1010, 0111, 0110, 0101 5 8 If you have an idea of what you are looking for, the pattern for the number of strings is not too hard to spot – each term is the sum of the previous two. (8 = 5 + 3, 5 = 3 + 2, …) Although it isn't necessarily obvious how many strings of length 0 we should say there are, you can work backwards and decide that the answer must be 1 to make the pattern work. (If you think about it, there is 1 way to pick zero bits…) This is another approach we can use to decide that: an = an-1 + an-2 with a 1 = 2 and a 0 = 1

How many bit-strings (ex 1001, 010110100, 10111 etc) of some length n that have

How many bit-strings (ex 1001, 010110100, 10111 etc) of some length n that have no consecutive 0 s are there? Using our recurrence relation, an = an-1 + an-2 you could code a soloution to the question doing something like this: public int num. Of. BS ( int n ) { if (n==0) return 2; if (n==1) return 3; return num. Of. BS(n-1)+num. Of. BS(n-2); } This is an easier way to think about the problem than giant lists of possible strings – just try listing all the possible strings of length 10 by hand…

 • If you remember, the Fibonacci sequence has recurrence relation an = an-1+an-2

• If you remember, the Fibonacci sequence has recurrence relation an = an-1+an-2 and initial values f(0)=0 and f(1)=1. What might a program for the nth Fibonacci number look like? • Say that an = an-1 + 3*an-3. How many initial values would you need? (The answer isn’t 2!) Pick some random initial values and write a program. • How would you solve the following using a recurrence relation? (You might consider this the simplest type of recurrence relation) A fish whose name is Geoffrey puts $10, 000 into a bank. Every year he gets 5% interest. What is the recurrence relation for the nth year? What is/are the initial value(s)? Click For Hint Each year the money in the bank is multiplied by 1. 05

. . with constant coefficients. A mouthful of a name that describes relations of

. . with constant coefficients. A mouthful of a name that describes relations of the form: an = b 1*an-1 + b 2 *an-2 + … + bk *an-k Examples: an = 3 * an-1 an = 2 * an-1 + an-2 an = 4*an-15, 325 (The b 1. . bk are the constant coefficients: In the first example, b 1=3) This is a pretty broad category, but there are some things it doesn’t fit. For example, an=1+an-1 isn’t one, because that 1 isn’t related to any an’s. Also, factorial, an=n*an-1 doesn’t fit either because n is not a constant. The only terms allowed are an’s multiplied by some constant (which can be 0, as they are in the third example for an-1 through an-15, 324) Some properties of LHRRWCCs: The ‘k’ in an-k is called the ‘degree’. It’s a measure of how far the RR looks back; it is also the number of initial values that are required. Also, LHRRWCCs can be solved with explicit equations…

A fish whose name is Geoffrey puts $10, 000 into a bank. Every year

A fish whose name is Geoffrey puts $10, 000 into a bank. Every year he gets 5% interest. What is the recurrence relation for the nth year? What is/are the initial value(s)? This is an excellent place to start making equations because it is a rather simple one. If you didn’t get it before, the recurrence relation and initial value: an = 1. 05 * an-1 , a 0 = 10, 000 You might already know the answer to this. Every year, you multiply by 1. 05; so, if you want the nth year, you multiply by (1. 05)n. f(0) = 10, 000 f(2) = 1. 05 *10, 000 = (1. 05)2 * 10, 000 … f(1) = 1. 05 * 10, 000 f(3) = 1. 05 * 10, 000 = (1. 05)3 * 10, 000 f(n)= (1. 05)n * 10, 000 People tend to write that equation like this: an = a 0 * rn where r=1. 05 In fact any recurrence relation an = r * an-1 has the equation an = a 0 * rn

How would you go about making an equation for, say, the Fibonacci sequence? The

How would you go about making an equation for, say, the Fibonacci sequence? The RR is an= an-1+an-2. If you recall, for degree one (interest problem) the equation is an=a 0*rn. If a 0=1, that’s just an=rn. We can actually find a similar answer for the Fibonacci sequence. First, its easiest (although not necessary) to rewrite an= an-1+an-2 as an+2= an+1+an; they’re the same thing. So then since an=rn, which also means an+1= r *an: r 2 *an= r *an + an or which we can rewrite as an * ( r 2 – r – 1 ) = 0 which means either r 2 *an – r *an – an = 0, or r 2 -r-1 = 0 Now, an = 0 obviously works, since 0 = 0+0. Since it works for any LHRRWCC as long as all the initial values are 0 also, it’s called a ‘trivial solution’ and we just tend to ignore it. r 2 – r – 1 = 0 is called the characteristic polynomial of this particular LHRRWCC, and can be solved using the famous Quadratic Formula, or by factoring, or whatever, but you can solve it. The important thing, though, is that it doesn’t have just one answer; in this case there are two. To see this, if we use the quadratic formula or factor or whatever, we find out r 2 – r – 1 = (r – [1+√ 5]/2) * (r – [1 -√ 5]/2) (factored form) In fact any polynomial P of degree k (degree being largest exponent; 2 in this case) will have k roots, or solutions to P = 0. And, the degree of the characteristic polynomial is the same as the degree of the LHRRWCC, and is the same as the number of initial values necessary for that LHRRWCC. On the next slide, how the roots of the characteristic polynomial are useful is discussed.

Just a little practice with them. If you work out the characteristic polynomial for

Just a little practice with them. If you work out the characteristic polynomial for an= an-1 - 2*an-2+ 4*an-3 you get r 3 - r 2+ 2 r- 4=0. For an = 2. 5*an-1 + 13*an-4 you get r 4 -2. 5 r 3 -13=0. Do you see a pattern? Basically you just make the coefficients on the right negative. Then, as far as factoring them goes, if you factor r 2+6 r+8=0 into (r+4)(r+2)=0 , the roots are actually -4 and -2. For (r-4)(r-2)=0 , they’re 4 and 2. You make the numbers negative. If you don’t factor but use the quadratic formula instead, the roots for ar 2+br+c=0 are [-b±√(b 2 -4 ac)]/2 a, and you don’t have to make them negative. It’s also possible to have complex roots, like for r 2+r+1=0 whose roots are -1/2 + i*(√ 3)/2 and -1/2 - i*(√ 3)/2. The recurrence relation is an=-an-1 -an-2 and for initial values 0 and 1, this corresponds to the sequence { 0, 1, -1, 0, 1, … } which although it looks different than most since it repeats, is still made up of real numbers. So if you get complex roots, don’t panic. For polynomials of degree > 2, it becomes a lot harder to factor and you may want instead to try out some root-finding calculator on teh interwebs, of which there are many.

So, what is useful about the roots [1+√ 5]/2 and [1 -√ 5]/2? Well,

So, what is useful about the roots [1+√ 5]/2 and [1 -√ 5]/2? Well, for one thing they are solutions. For example, [1+√ 5]/2 ≈ 1. 618: a 0= 1. 6180 =1 a 1 = 1. 618 a 2=1. 6182=2. 618 a 3=1. 6183=4. 236 a 4=1. 6184=6. 854 a 5=1. 6185=11. 09 (these numbers are rounded) You can see that this satisfies the RR an= an-1+an-2: 2. 618=1+1. 618, 6. 854 = 4. 236+2. 618, etc. So both of the roots work in the equation an=rn. However, that isn’t all that works. Say you have two sequences that work; the one above and the Fibonacci sequence, for example. If we call this one A and the Fib F, An=An-1+An-2 and Fn=Fn-1+Fn-2. What if you added these two together? Call that one, I don’t know, H. So Hn= An+ Fn = (An-1+An-2) + (Fn-1+Fn-2) = (An-1 + Fn-1) + (An-2 + Fn-2) = Hn-1 + Hn-2. So, if you add them, you get another one that works. This is because they are LHRRWCCs; specifically because they are Linear. Given some whatevers, a linear combination is what you get by multiplying each one by a coefficient and then adding them together. For example, if you’ve done vectors, you know that you can turn any 2 d vector into its x and y components, or x*i + y*j. That’s a linear combination. In other words, any point on an x-y graph is a linear combination of some distance along the x axis, i, and some distance along the y axis, j. i and j are called the ‘bases’, bases just basically being whatever you’re linear-combination-ing. In the same way, the roots of the characteristic polynomial, in this case [1+√ 5]/2 and [1 -√ 5]/2, are the bases for every possible sequence that satisfies the recurrence relation. Formally, if you call the first one r 1 and the second one r 2, and if you call their coefficients b 1 and b 2, then the equation for some sequence a looks like: an = b 1*r 1 + b 2*r 2

So, say we have a LHRRWCC of a sequence a, and we have turned

So, say we have a LHRRWCC of a sequence a, and we have turned it into its characteristic polynomial P of degree k. Also, we’ve done a lot of math that we’re quite proud of and we’ve got the roots of P, specifically r 1, r 2. . rk. Then an=b 1 r 1 n +b 2 r 2 n+. . +bkrkn, assuming we’ve figured out the b’s which we’ll get to later. This is all well and good, but only if all of the r’s are different! Here’s an example of a characteristic polynomial that this doesn’t work for: r 2 -2 r+1=0 which factors to (r-1)=0. Horrors! We have two roots, but, they’re the same! It turns out there’s an easy fix. First, an= b 1 rn still works. All we have to do is add in a second thing that also happens to work, namely an=b 2*n*rn. If we had three roots that were the same, we’d have to add in an=b 3 n 2 rn also. If our characteristic polynomial was (r-1)(r-1)(r-5)(r+11)=0 The general equation would look like an = b 1(1)n + b 2 n(1)n + b 3 n 2(1)n + b 4(5)n + b 5 n(5)n + b 6(-11)n A couple side notes. First, note that if you factor a polynomial, the roots are negative of what’s in the factor. That is, if (r+6) is a factor, -6 is the root, and if its (r-6), 6 is the root. Second, the number of times a root pops up is called its multiplicity. That is, in the above example, the root 1 had multiplicity 3, 5 had multiplicity 2, and -11 had multiplicity 1. And also, it does continue like it looks like it should; with multiplicity 4, you’ll have a bn 3 rn, 5 is bn 4 rn, etc.

The last step is to figure out the coefficients. Now, since every sequence that

The last step is to figure out the coefficients. Now, since every sequence that satisfies the same recurrence relation has the same characteristic polynomial, and therefore the same roots. So the only thing different in the equation are the coefficients, while the only thing that’s different in the actual sequences we already know are the initial values. And also, there are just as many coefficients as there are roots, and there are just as many roots as initial values. So it makes sense that we work out the coefficients by using the initial values. Let’s find the coefficients for the Fibonacci sequence. We already know the roots, so: an = b 1 [ (1+√ 5)/2]n + b 2 [(1 - √ 5)/2]n If we plug in 0 and 1, which are the ns for the two initial values, a 0 = b 1 + b 2 = 0, a 1 = b 1 [ (1+√ 5)/2] + b 2 [(1 - √ 5)/2] = 1 This is a system of equations. You can mess about with substitution and elimination, or you can use matrices and have a calculator do it for you. Eh, I dunno, your choice. With matrices it looks something (decimals aren’t exact) like: A More General Form To solve this, if you put the 2 x 2 into your calculator as [A] and the [0, 1] as [B] then [A]-1[B] will give you the answer.

 • What’s the equation for an=5*an-1 if a 0=13? • What is the

• What’s the equation for an=5*an-1 if a 0=13? • What is the characteristic equation for an= an-1 + 12 an-2? What are its roots? • What’s a general equation given the characteristic polynomial (r-1)(r+5)=0? • Given the RR an=3 an-1 -2 an-2, and the initial values a 0=15, a 1=25, find a happy magic equation.

0 2 Start with an equilateral triangle. Every step, remove the middle third of

0 2 Start with an equilateral triangle. Every step, remove the middle third of every line and add in two of the sides of an equilateral triangle. (Call the first triangle the 0 th iteration) 1 Q 1: What is the perimeter of the n-th iteration if the starting side length is L (so the starting perimeter is 3 L) ? lots Area added 0 th time Area added 1 st time Q 2: If the A/3 is the area ADDED the 0 th time, that is the area of the three little triangles, what is the amount of area ADDED the n-th time? Note that in this case, the n=0 corresponds to the difference between the area of iteration n=1 and iteration n=0.

Q 1: Notice that every iteration, each line becomes 4/3 as long as 1

Q 1: Notice that every iteration, each line becomes 4/3 as long as 1 2 3 4 it was before: 1 2 3 So, the nth perimeter pn = 4/3 * pn-1. You should recognize that the equation is then (4/3)n *p 0, and if the side length is L, p 0=3 L pn = (4/3)n * 3 L Q 2: Because every iteration, each line is broken from one line into 4 lines, every iteration will have 4 times as many triangles added to it as the last one. And, each triangle will have a side length of 1/3 what it had last time, which means 1/9 the area. That means that between each iteration, the area added is 4/9 what was added the time before. So, an = 4/9*an-1. You’re given that a 0=A/3, (the /3 so that A is the area of the original triangle, but that doesn’t matter so much) so the equation is an = (4/9)n * A/3 Incidentally, if you add up the area added each time plus the starting area in order to get the total area, you end up with what’s called a geometric series. Those are included in the “Helpful Extras”.

You only have to find the recurrence relations and initial values for all of

You only have to find the recurrence relations and initial values for all of them; just find an equation for one of them (not Q 1!) Q 1. Say you have two letters, A and B, to use. If you can’t have the same letter twice in a row, how many strings of length n can you make? Q 2. What if you have three letters? (A, B, C) With three letters: A, B, C, AB, AC, BA, BC, CA, CB, ABA, ABC, ACA … Q 3. What if you have two letters and can have the same letter up to twice in a row? Q 4. With three letters? Q 5. With up to three in a row, and four letters? A, B, C, AA, AB, AC, … AAB, AAC, ABA, ABB, ABC … Bonus: Have you found a pattern yet? Try to find a general sort of pattern for L letters and R allowed in a row. (It’s called ‘coloring’ because if the letters are called colors, it’s how many ways to color a line with only so many of the same color in a row. Coloring is a whole subject in mathematics, because math is so serious a subject that it is easy to get away with being childish. )

Q 1. So, there’s only one possible letter to add at any time. That

Q 1. So, there’s only one possible letter to add at any time. That is to say, an = an-1. Initially, you have 2 options: A or B. Q 2. Now there are two possible letters to add. an = 2*an-1. Q 3. Those of length n can be divided into those with a mismatch at the end (AB, BA) and those with double at the end (AA, BB). The mismatches come from adding the other letter to the end of an n-1, (A = > AB) the doubles come from adding two of the other letter to the end of an n-2 (A => ABB). an = an-1 + an-2. Initial is A/B, AA/AB/BA/BB: a 1 = 2, a 2 = 4. Q 4. Same as above, but there’re two options each time (A => AB, AC). an = 2*an-1+2*an-2. Also, there are different initial values: a 1 = 3, a 2 = 9. Q 5. There are those that end in one of the same, BA, those that end in two of the same, and those that end in three of the same. A => AB, AC, AD ; A => ABB, ACC, ADD ; A => ABBB, ACCC, ADDD. Each time there’re three options for colors (the ones that aren’t the previous color), and each can be made from any working string of the correct length (Singles from n-1, doubles from n-2, etc). an = 3*an-1 + 3*an-2 + 3*an-3. Initial values are a 1=4, a 2=16, a 3=64. Q 6. In general, the RR is an = (C-1)*an-1 +. . + (C-1)*an-R, with initial values a 1=C, a 2=C^2. . a. R=C^R.

Many useful recurrence relations are non-homogeneous. Something like: an = an-1 + 13 n

Many useful recurrence relations are non-homogeneous. Something like: an = an-1 + 13 n 2 – 5 In other words, it’s an ordinary LHRRWCC, except that added to the end of it are a few terms that might depend on n but not on previous values of an. For information on solving these, see: http: //www. hlt. utdallas. edu/~vh/Courses/Fall 09/Discrete. Math/Lectures/ Non-homogeneous%20 recurrence%20 relations. ppt#360, 9, Example