Built in recursive function in matlab, MATLAB in Mathematics

Assignment Help:

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.


Related Discussions:- Built in recursive function in matlab

Illustration of minimum and maximum value, Illustration of Minimum and Maxi...

Illustration of Minimum and Maximum Value Both of these functions also return the index of the minimum or maximum value; when there is more than one occurrence, it returns the

Counting in a while loop, Counting in a While Loop: Whenever it is not...

Counting in a While Loop: Whenever it is not known ahead of the time how many values will be entered into the script, it is often essential to count the number of values which

Function isreal - complex numbers, Function isreal - complex numbers: ...

Function isreal - complex numbers: The function isreal returns 1 for logical true when there is no imaginary part of the argument, or 0 for false when the argument does have a

Illustration of spreadsheet files, Illustration of Spreadsheet Files: ...

Illustration of Spreadsheet Files: This reads the numbers in a double vector variable nums and the text in a cell array txt (the xlsread function forever returns the numbers f

Types of user-defined functions, Types of User-defined Functions: We k...

Types of User-defined Functions: We know how to write a user-defined function, stored in an M-file, which computes and returns one value. This is merely one type of the functi

Writing variables to a file, Writing variables to a File: The save com...

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

Matlab program - error-checking for integers, Error-checking for integers: ...

Error-checking for integers: illustrations of running this script as shown below: >> readoneint Enter an integer: 9.5 Invalid! Enter an integer: 3.6 Invalid

Example of recursive functions, Example of Recursive functions: This d...

Example of Recursive functions: This definition is recursive as a factorial is defined in terms of the other factorial. There are two parts to any recursive definition: the co

Use of logical vector, Use of logical vector: Determine how many eleme...

Use of logical vector: Determine how many elements in the vector vec were greater than 5, the sum function can be used on the resulting vector isg: >> sum(isg) ans =

Find minimum and maximum for each row, Find Minimum and Maximum for each ro...

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

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd