# Form of Functions

MATLAB functions have the form:

[output]=function(input)

Where we use [ ] to enclose the output variables, function is the function name and the function input variables are enclosed with ( )

Multiple output variables are separated using , likewise for multiple input variables:

[output1,output2]=function(input1,iInput2)

For a single output we do not need to use [ ]

output=function(input)

# sqrt Function

Let us use the example of the inbuilt function sqrt which calculates the square root of the input variable:

output=function(input)

First we are going to type the function name with an open bracket and no outputs:

output=sqrt(

If MATLAB recognises the function name. a drop down box will show, which highlights the form of the functions inputs (in this case just 1 input):

We can select More Help to get more information:

Okay now let’s input a simple input and close the bracket

output=sqrt(4)

$\displaystyle \text{Output}=2$

This type of function can be used with an Array and works on an element by element basis.

M=[4,9;16,25]

Output=sqrt(M)

$\displaystyle \text{Output}=\left[ {\begin{array}{*{20}{c}} 2 & 3 \\ 4 & 5 \end{array}} \right]$

# nthroot Function

Let us use the example of the inbuilt function nth which calculates the nth root of the first input variable with respect to the second input variable:

Output=function(Input1,Input2)

First we are going to type the function name with an open bracket and no outputs:

output=nthroot(

We can see this time that there are two inputs. Once again if we select More Help we can get more information:

Here we find a description of what the function does and what each of the inputs are:

Setting the second input of three gives the cubed route of the first input value 27:

Output=nthroot(27,3)

$\displaystyle \text{Output}=3$

Let’s now make both inputs matrices:

M=[4,27;256,3125]

N=[2,3;4,5]

Output=nthroot(M,N)

$\displaystyle \text{Output}=\left[ {\begin{array}{*{20}{c}} {\sqrt[2]{4}} & {\sqrt[3]{3}} \\ {\sqrt[4]{{256}}} & {\sqrt[5]{{3125}}} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} 2 & 3 \\ 4 & 5 \end{array}} \right]$

# exp Function

For a process with 100 % return during a single growth period the rateofgrowth will equal to:

$\displaystyle \text{rateofgrowth=}{{\left( {1+\frac{{100\%}}{1}} \right)}^{1}}={{\left( {1+\frac{1}{1}} \right)}^{1}}=2$

In other words after 1 growth period with 100 % growth you get your starting investment value of 1 plus an additional value of 1 return.

After two growth periods:

$\displaystyle \text{rateofgrowth}={{\left( {1+\frac{{100\%}}{2}} \right)}^{2}}={{\left( {1+\frac{1}{2}} \right)}^{2}}=2.25$

If instead you get an investment of 100 % growth over two growth periods, in the first of the two growth periods you will have your initial investment of 1, plus an additional value of 0.5 return. This means you start the second growth period with 1.5 and then after the second growth period you gain an additional value of 0.75 return. Thus you end up with your starting investment of 1 plus an additional value of 1.25.

After three growth periods:

$\displaystyle \text{rateofgrowth}={{\left( {1+\frac{{100\%}}{3}} \right)}^{3}}={{\left( {1+\frac{1}{3}} \right)}^{3}}=2.3704$

If instead you get an investment of 100 % growth over three growth periods, in the first of the three growth periods you will have your initial investment of 1, plus an addition value of 0.3333 return. In the second growth period you thus start with 1.3333 and get 0.4444 return and this means in the third growth period, you start with 1.7777 and get 0.5925 in return. Thus you end up with your starting investment of 1 plus an additional value of 1.3702 (1.3704 if the calculation is carried out to more significant figures).

And after $\displaystyle n$ growth periods:

$\displaystyle \text{rateofgrowth}={{\left( {1+\frac{{100\%}}{n}} \right)}^{n}}={{\left( {1+\frac{1}{n}} \right)}^{n}}$

For continuous growth $\displaystyle n\to \infty$

$\displaystyle \begin{array}{*{20}{c}} n & {{{{\left( {1+\frac{1}{n}} \right)}}^{n}}} \\ 1 & {2.0000} \\ 2 & {2.2500} \\ 3 & {2.3704} \\ 4 & {2.4414} \\ 5 & {2.4883} \\ 6 & {2.5216} \\ 7 & {2.5465} \\ 8 & {2.5658} \\ 9 & {2.5812} \\ {10} & {2.5937} \\ {100} & {2.7048} \\ {1000} & {2.7169} \\ {10000} & {2.7181} \\ {100000} & {2.7183} \\ {1000000} & {2.7183} \\ {10000000} & {2.7183} \\ {100000000} & {2.7183} \\ {1000000000} & {2.7183} \\ {10000000000} & {2.7183} \end{array}$

As you can see from above the numbers begin to converge at high $\displaystyle n$ and they converge to a constant known as $\displaystyle e$. $\displaystyle e$ is thus defined as the maximum rate of a continuous process.

$\displaystyle e={{\lim }_{{_{{_{{n\to \infty }}}}}}}{{\left( {1+\frac{1}{n}} \right)}^{n}}$

output=function(Input)

We can look at MATLABs help about the Exponential by typing the following in and selecting More Help:

output=exp(

output=exp(1)

output=2.7183

M=[1,2;3,4]

N=exp(M)

$\displaystyle \text{N}=\left[ {\begin{array}{*{20}{c}} {\exp \left( 1 \right)} & {\exp \left( 2 \right)} \\ {\exp \left( 3 \right)} & {\exp \left( 4 \right)} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {2.7183} & {7.3891} \\ {20.0855} & {54.5982} \end{array}} \right]$

# log Function

The natural logarithm can be used to calculate the amount of product one has after $\displaystyle x$ units of time at continuous growth. We can also calculate the logarithm using the function. In this case we will actually use another function as an input argument and because these two functions cancel each other out we should just return with the input argument:

Output=function(Input)

Output=log(exp(2))

$\displaystyle \text{Output}=2$

Output=log(N)

$\displaystyle \text{Output}=\left[ {\begin{array}{*{20}{c}} {\log \left( {2.7183} \right)} & {\log \left( {7.3891} \right)} \\ {\log \left( {20.0855} \right)} & {\log \left( {54.5982} \right)} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} 1 & 2 \\ 3 & 4 \end{array}} \right]$

This of course is equal to M as the two functions cancel each other out: