Built in recursive function in matlab, MATLAB in Mathematics

Built in recursive function in MATLAB:

We have seen that the built-in function in MATLAB to compute factorials, termed as the factorial and we know how to implement the iterative definition by using a running product. Now we will rather than write a recursive function known as the fact. The function will receive an integer n, which for ease will suppose is a positive integer, and will compute n! just by using the recursive definition given:

 

1287_Built in recursive function in matlab.png

The function computes one value, by using an if-else statement to select between the base and general cases. When the value passed to the function is 1, the function returns 1 as1! become equal to 1. Or else, the common case applies. According to definition, the factorial of n, that is what this function is computing, is defined as n multiplied by the factorial of (n-1). Therefore, the function assigns   n * fact(n-1) to the output argument.

How does this work? Precisely the way the illustration was sketched for 3!. Let's trace what would take place if the integer 3 is passed to the function:

fact(3) tries to assign 3 * fact(2)

fact(2) tries to assign 2 * fact(1)

fact(1) assigns 1

fact(2) assigns 2

fact(3) assigns 6

Whenever the function is initially called, 3 is not equivalent to 1, therefore the statement

facn = n * fact(n-1);

is executed. This will try to assign the value of 3 * fact(2) to  facn, but this expression cannot be computed yet and hence value cannot be assigned yet as first the value of fact(2) should be found. Therefore, the assignment statement has been interrupted by a recursive call to the fact function. The call to the function fact (2) answers in a try to assign 2 * fact(1), but again this expression cannot so far be computed. Later, the call to the function fact(1) answers in a complete execution of an assignment statement as it just assigns 1. The base case has been reached once, and then the assignment statements which were interrupted can be computed, in the reverse order.

Posted Date: 10/22/2012 1:31:37 AM | Location : United States







Related Discussions:- Built in recursive function in matlab, Assignment Help, Ask Question on Built in recursive function in matlab, Get Answer, Expert's Help, Built in recursive function in matlab Discussions

Write discussion on Built in recursive function in matlab
Your posts are moderated
Related Questions
Complex numbers: A complex number is commonly written in the form z = a + bi here a is known as the real part of the number z, b be the imaginary part of z, and i is √-1

Indexed empty matrix: The Individual elements cannot be eliminated from matrices, as matrices always have the similar number of elements in every row. >> mat = [7 9 8; 4 6

Plot Functions: Faraway, we have plotted to generate two-dimensional plots and bar to generate bar charts. We have seen how to clear the Figure Window by using clf, and how to

Blanks function: The blanks function will generate a string consisting of n blank characters-that are kind of hard to see here! Though, in a MATLAB if the mouse is moved to hi

Illustration of Writing to files: Here is the other illustration in which a matrix is written to a file. At First, an arbitrary 2 × 4 matrix is generated, and then it is writt

Function fopen - file function: The permission string in the call to the fopen function identifies that the file is opened for writing to it. Just as when reading from a file,

If I have a vector representing the packed storage form of a symmetric matrix, how do I perform a cholesky factorisation on that?

Functions to create special matrices: The MATLAB also has various functions which create special matrices. For illustration, the zeros function generates a matrix of all zeros

Animation: In this part we will observe a couple of ways to animate a plot. These are visuals, therefore the outcomes can't really be shown here; it is essential to type these

User input in the while loop: Here is an illustration of running this script: >> readonenum Enter a positive number: -5 Invalid! Enter a positive number: -2.2