MATLAB - Recursive Functions



Recursive functions are an important concept in programming and can be a valuable tool in MATLAB. A recursive function is a function that calls itself to solve a problem.

What is a Recursive Function?

A recursive function is a function that solves a problem by breaking it down into smaller, more manageable subproblems. Each time the function is called, it works on a smaller portion of the problem until it reaches a base case. The base case is the condition under which the function stops calling itself and returns a result.

How Recursive Functions Work in MATLAB?

In MATLAB, a recursive function follows the same principles as in other programming languages. Here's a step-by-step explanation of how a recursive function works −

Define your base case that handles when the recursion has to stop. Without the base case, the function will call itself infinitely and will cause a stack overflow.

Inside the recursive case, the function calls itself with modified arguments to work on a smaller part of the problem.

As the function returns from each recursive call, the results are combined to solve the overall problem.

The recursion continues until the base case is reached, at which point the function starts returning results back up the call stack.

Examples of Recursive Function

Let us check the example given below, which calculates the factorial of a given number using a recursive function. Let me mark for you in the example the code that belongs to base case and recursive case.

Example 1: To calculate the factorial of a given number

We are going to make use o f the recursion technique to calculate factorial of a given number.

The factorail of a given number is represented as "n!" −

n! = n × (n - 1) × (n - 2) × ... × 3 × 2 × 1

So 10! = 10 × 9 × 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1

Below is the code to do so −

function result = factorial(n)
   % Base case
   if n == 0
      result = 1;
   else
      % Recursive case
      result = n * factorial(n - 1);
   end
end

In this example, the base case is when n is 0, and the recursive case calculates n times the factorial of n-1. The function calls itself until n reaches 0.

Let us first create the function in matlab as shown below −

recursive case

Let us now execute the same in matlab as shown below −

fact = factorial(10)

In execution in matlab command window gives following output −

>> fact = factorial(10)

fact = 3628800

Example 2: Fibonacci Sequence

The Fibonacci sequence is a series of numbers that starts with 0 and 1, where each subsequent number is the sum of the two preceding ones.

The sequence is infinite and it starts as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on.

In the example below let us check how to calculate the fibonacci sequence of 10.

function result = fibonacci(n)
   % Base case
   if n == 0
      result = 0;
   elseif n == 1
      result = 1;
   else
      % Recursive case
      result = fibonacci(n - 1) + fibonacci(n - 2);
   end
end

Let us create the function in matlab as shown below −

function fibonacci

Let us execute the function fibonacci() in matlab command window as shown below −

result = fibonacci(10)

The output is as follows −

>> result = fibonacci(10)

result =

    55

Code Generation for Recursive Functions

Code generation for recursive functions in MATLAB refers to a way to convert the MATLAB code automatically that contains recursive functions into a different programming language, basically one that is more efficient for execution on a target platform or hardware.

The code generator in MATLAB can use either compile-time recursion or run-time recursion to generate code for recursive functions.

Compile-Time Recursion

In Compile-time recursion the code generator will figure out how many times a recursive function needs to be called by itself as the program is getting translated from MATLAB code to machine code. This happens before the program runs. It determines the number of recursive calls in advance and generates code accordingly.

Run-Time Recursion

Run-time recursion, the code generator generates code that can handle recursive calls dynamically while the program is running. This approach doesn't know in advance how deep the recursion will go, so it creates more flexible code that can adapt to different situations at runtime.

Advertisements