Friday, November 13, 2015

Making a factorial function in matlab

Creating a function in matlab is easy and is just like making a function in any other programming language. First we write the 'function' keyword which means we are starting our function then we write our output parameter(variable in which we want to store our output value) in square brackets and if there is none then we leave it blank. After that we write an equal ('=') sign and then we write our function name and the input parameters in round brackets ('()'), remember our function name and input parameters as they are needed to run the function. Write down the function code and remember to write the keyword 'end' when the function is finished. Save the file and it should have the same name as your function and this is a must.

Below is an example of a factorial function:



function [ y ] = factorial(x)

 if(x==0)
     y=1;
 else 

    y=x
     for i=1:x-1
    y=y*i;
 end

end

Output


Widgets

 

Copyright @ 2014 CPP Fuzz.