12.5. Recursion#
Recursion is a way of solving problems where the solution depends on breaking down the problem into smaller subproblems. This requires functions to include a function call to themselves. For example, consider the calculation of a factorial.
So to calculate \(n!\) we need the value of \((n-1)!\) which in turn needs the value of \((n-2)!\) and so on. So we could calculate \(n!\) be splitting the problem down into subproblems and solving those. Lets write a function called factorial()
and uses recursion. Enter the following code into your program
% -------------------------------------------------------------------------
function y = my_factorial(n)
if n == 0 || n == 1
y = n;
else
y = n * my_factorial(n - 1);
end
end
and make a call to this function by entering the following code before the function declarations.
% Recursion
fprintf("\n6! = %d\n", my_factorial(6))
Run your program and you should see the following added to the console
6! = 720
Lets step through this function for an input of n = 3
.
my_factorial(3)
:n=3
so we need to calculatemy_factorial(2)
my_factorial(2)
:n=2
so we need to calculatemy_factorial(1)
my_factorial(1)
:n=1
so we return 1 and return tomy_factorial(2)
my_factorial(2)
:n=2
so we return2 * 1 = 2
my_factorial(3)
:n=3
so we return3 * 2 = 6
A recursive function requires a base case and recursive case
Base case: the condition under which the function stops calling itself and returns a value. In our
my_factorial()
function this was whenn == 0
orn == 1
so we returned 0 since \(0! = 1! = 1\).Recursive case: the condition under which the function needs to call itself. Each recursive call should move the function closer to the base case so that the recursion eventually terminates. In our
my_factorial()
function this was whenn
is greater than 1. Some recursive functions will have more than one recursive case.
Warning
Recursion offers an elegant way of solving some problems but can require lots of function calls and memory when the levels of recursion gets large resulting in longer run times. Recursion should only really be used where absolutely necessary.
12.5.1. Exercises#
Write a function called fibonacci()
that computes the \(n\)th Fibonacci number using recursion. Use your function to compute:
the 10th Fibonacci number
the 20th Fibonacci number
the 40th Fibonacci number (warning, this may take a few seconds to calculate)
The determinant of an \(n\times n\) square matrix can be calculated using
where \(A_i\) is the matrix formed by removing row 1 and column \(i\) from \(A\) and the determinant of a \(2\times 2\) matrix is
Write a function called my_det()
that computes the determinant of a square matrix using recursion. Use your function to compute the determinants of following matrices:
\(A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}\)
\(B = \begin{pmatrix} -3 & -3 & 4 \\ 6 & -1 & 3 \\ -2 & 6 & 0 \end{pmatrix}\)
\(C = \begin{pmatrix} 5 & 2 & -2 & 6 \\ -1 & -1 & 3 & -1 \\ 5 & -1 & 0 & 1 \\0 & -3 & 5 & 2 \end{pmatrix}\)