
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
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 −

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 −

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.