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

Strings as matrix, Strings as matrix: The matrix can be generated, tha...

Strings as matrix: The matrix can be generated, that consists of strings in each row. Therefore, essentially it is created as a column vector of strings, but the final result

Example of symbolic expression, Example of Symbolic Expression When the...

Example of Symbolic Expression When there is more than one variable, the MATLAB selects which to solve for. In the illustration below, the equation ax 2 + bx = 0 is solved. Th

Three-dimensional pie and bar, Three-dimensional pie and bar: For the ...

Three-dimensional pie and bar: For the bar3 function, x & y vectors are passed and the function displays three-dimensional bars as shown in figure: Figure: Three-dimen

Common form of the switch statement, Common form of the switch statement: ...

Common form of the switch statement: The common form of the switch statement is as shown below:   switch switch_expression   case caseexp1    action1   case cas

Looping, How do I write a product series running from i to n of x''s using ...

How do I write a product series running from i to n of x''s using a for loop?

Print an imaginary number, Print an imaginary number: To print an imag...

Print an imaginary number: To print an imaginary number, the function disp will show both parts automatically: >> disp(z1)    4.0000 + 2.0000i The function fprint

Optimal foraging theory, This project requires you to use the ideas of Chap...

This project requires you to use the ideas of Chapter 25 regarding maximization of a function. Here we assume that evolution has acted to generate highly efficient foragers. By hig

Empty vectors, Empty Vectors: An empty vector or in another words, a v...

Empty Vectors: An empty vector or in another words, a vector which stores no values, can be generated using the empty square brackets: >> evec = [] evec = [] >> lengt

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

Illustration of advanced file input and output, Illustration of Advanced fi...

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 =

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