08 Recursion - 8 - Recursion Motivation Recursion is one of...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Recursion - 1 8 - Recursion Motivation Recursion is one of the most powerful ways to solve problems with computers. It is also usually the simplest way to write programs. But it can be tricky to understand at first. This lecture will explain recursion and help you to think recursively. You will need to practice thinking and programming this way. It will take some time, but you will find that it is worth the effort. Our mygcd.m program was an example of a recursive program. Mathematical induction again Let’s start with a simple expression: = = N k k N 1 2 ) 1 2 ( This says that you can get the square of a number N by adding up a sequence of consecutive odd numbers up to 2N–1 . In other words: 3 2 =9=1+3+5 6 2 =36=1+3+5+7+9+11 Let’s prove this by induction. Our inductive hypothesis is that 2 1 1 2 ) ( N k N S N k = = = Base case N =1, S ( N ) has just one term, which is one. So S(1)=1 which equals N 2 so we are OK. Inductive step We assume the hypothesis holds for N , that is, we assume that = = + + + + = = N k N N k N S 1 2 ) 1 2 ( ... 5 3 1 ) 1 2 ( ) ( Now we try to prove the main hypothesis, that S(N+1)=(N+1) 2 . Let’s write it down again:
Background image of page 1

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

View Full DocumentRight Arrow Icon
Recursion - 2 + = = + 1 1 ) 1 2 ( ) 1 ( N k k N S The sum is the hard part. But we have a sum that looks a lot like it, namely the sum S ( N ). In fact the only difference is that S ( N+1 ) has one extra term over S(N), which is 2N+1 . In other words ) 1 2 ( ) ( ) 1 ( + + = + N N S N S This brings us closer to a proof because we know what S ( N ) is, or at least we assume we know that it is, ( N –1) 2 by our inductive hypothesis. Let’s write it now S ( N+1 ) = ( N ) 2 + (2 N +1) which can be expanded into S ( N+1 ) = N 2 +2N+1 = (N+1) 2 Thus we find that S ( N+1 ) = ( N+1) 2 , which is exactly what we wanted to prove. Turning proofs into programs Recursion is a programming technique that turns mathematical induction into programs. We can write a program to compute S ( N ) using recursion. During the proof above, there was a step in which we essentially used the fact that: S(N) = S(N-1) + (2N – 1) This line is called a recursive definition of or the recursive relation for S ( N ). Not only do most proofs by induction include a recursive relation, but that line allows you to easily program the expression, if we also have a base case. In this case, the base case is given by: S(1) = 1 We can now write a program for S ( N ). In MATLAB, the program looks like this: %s.m %recursively calculates s(n)=n^2 %using the relationship s(n)=s(n-1)+(2n-1) %E77 reader %07-01 D# function [tot] = s(n) if (n==1) tot = 1; % base case else
Background image of page 2
Recursion - 3 tot = s(n-1) + 2*n -1; % recursive relationship end The program first tests the base case to see if
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/12/2011 for the course E 7 taught by Professor Patzek during the Spring '08 term at University of California, Berkeley.

Page1 / 7

08 Recursion - 8 - Recursion Motivation Recursion is one of...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online