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
Find Minimum and Maximum for each row To find the maximum (or minimum) for each row, the dimension of 2 (that is how a MATLAB refers to rows) can be identified as the third arg

Illustration of Advanced file input and output: For illustration, to refer to the third number in the first element of the cell array: >> subjdata{1}(3) ans =

sprintf function: The sprintf function works precisely like the fprintf function, but rather than printing it generates a string. Here are some illustrations in which the outp

Labels and Prompts: The script loads all the numbers from file into a row vector. It then splits the vector; it stores the initial element that is the experiment number in a v

Standard Deviation The standard deviation is the square root of variance: The built-in function in a MATLAB for the standard deviation is known as std; the standard dev

Example of Logical built-in functions: For equivalent to all the function, we should make sure that the entire elements in the vector are logically true. The one way of doing

fprintf function - File function: The fprintf function really returns the number of bytes which was written to the file, therefore if you do not want to see that number, suppr

Fliplr function: The fliplr function "flips" the matrix from left to right (in another words the left-most column, the first column, become the last column and so on), and the

Writing variables to a File: The save command is used to write variables to a file, or to append the variables to a MAT-file. By the default, save function writes to a MAT-fil

Referring to and Modifying the Matrix Elements: To refer to matrix elements, the row and then the column indices are given in parentheses (always the row index initially and t