{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

10 Linear Recurrences

# 10 Linear Recurrences - 10 Recursion and Linear Recurrences...

This preview shows pages 1–3. Sign up to view the full content.

Linear Recurrences - 1 10 - Recursion and Linear Recurrences Motivation Now that you’ve seen recursion in action, it is time to move on to some more interesting examples of it. These examples typically have more than one recursive call in the function. To understand such programs, we often use recursion trees to visualize what’s going on. A More Complicated Recurrence The Fibonacci numbers are an important sequence of numbers that have a recursive definition with two terms: F(n) = F(n–1) + F(n–2) and base case F(1) = F(2) = 1 The sequence is: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 … You can see that it increases rather sharply. In fact, as we will see later, it increases at an exponential rate. For now, let’s write a MATLAB program to compute Fibonacci numbers. The process is very similar to the recursive code we wrote before, %fibonacci.m %calculates a fibonnacci sequence of length n %E77 reader %07-01 D# function sum = fibonacci(n) if (n < 3) sum = 1; else sum = fibonacci(n-2) + fibonacci(n-1); end If you write this program and test it, you’ll find that it does indeed do the right thing. As for understanding the program, it’s a lot more complicated than a recursive program with one call. We couldn’t draw the kind of figure we did in the first recursion lecture to explain what’s going on. Instead, we use recursion trees to explain it. Each node in a recursion tree represents one call to the function. The root of the tree is the first call. The children of the root are the recursive calls that the root makes. Their children represent

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Linear Recurrences - 2 the recursive calls that they make, etc.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern