# Form of Functions

MATLAB functions have the form:

<span style="color: #ff0000;">[</span>output<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">function(</span>input<span style="color: #0000ff;">)</span> Where we use

<span style="color: #ff0000;">[</span> <span style="color: #ff0000;">]</span>

to enclose the output variables,

<span style="color: #0000ff;">function</span>

is the function name and the function input variables are enclosed with

<span style="color: #0000ff;">(</span> <span style="color: #0000ff;">)</span>

Multiple output variables are separated using

<span style="color: #808080;">,</span>

likewise for multiple input variables:

<span style="color: #ff0000;">[</span>output1<span style="color: #808080;">,</span>output2<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">function(</span>input1<span style="color: #808080;">,i</span>Input2<span style="color: #0000ff;">)</span> For a single output we do not need to use

<span style="color: #ff0000;">[</span><span style="color: #ff0000;"> </span><span style="color: #ff0000;">]</span>
output=<span style="color: #0000ff;">function(</span>input<span style="color: #0000ff;">)</span> # sqrt Function

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

output=<span style="color: #0000ff;">function(</span>input<span style="color: #0000ff;">)</span>

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

output=<span style="color: #0000ff;">sqrt(</span> 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):  Okay now let's input a simple input and close the bracket

output=<span style="color: #0000ff;">sqrt(</span>4<span style="color: #0000ff;">)</span> $\displaystyle \text{Output}=2$ This type of function can be used with an Array and works on an element by element basis.

M=<span style="color: #ff0000;">[</span>4<span style="color: #808080;">,</span>9<span style="color: #ff00ff;">;</span>16<span style="color: #808080;">,</span>25<span style="color: #ff0000;">]</span>
Output=<span style="color: #0000ff;">sqrt(</span>M<span style="color: #0000ff;">)</span> $\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=<span style="color: #0000ff;">function(</span>Input1,Input2<span style="color: #0000ff;">)</span>

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

output=<span style="color: #0000ff;">nthroot(</span> 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=<span style="color: #0000ff;"><strong>n</strong>throot(</span>27,<strong>3</strong><span style="color: #0000ff;">)</span> $\displaystyle \text{Output}=3$ Let's now make both inputs matrices:

M=<span style="color: #ff0000;">[</span>4<span style="color: #808080;">,</span>27<span style="color: #ff00ff;">;</span>256<span style="color: #808080;">,</span>3125<span style="color: #ff0000;">]</span>
N=<span style="color: #ff0000;">[</span>2<span style="color: #808080;">,</span>3<span style="color: #ff00ff;">;</span>4<span style="color: #808080;">,</span>5<span style="color: #ff0000;">]</span> Output=<span style="color: #0000ff;">nthroot(</span>M,N<span style="color: #0000ff;">)</span> $\displaystyle \text{Output}=\left[ {\begin{array}{*{20}{c}} {\sqrt{4}} & {\sqrt{3}} \\ {\sqrt{{256}}} & {\sqrt{{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=<span style="color: #0000ff;">function(</span>Input<span style="color: #0000ff;">)</span>

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

output=<span style="color: #0000ff;">exp(</span> output=<span style="color: #0000ff;">exp(</span>1<span style="color: #0000ff;">)</span>
output=2.7183 M=<span style="color: #ff0000;">[</span>1<span style="color: #808080;">,</span>2<span style="color: #ff00ff;">;</span>3<span style="color: #808080;">,</span>4<span style="color: #ff0000;">]</span>
N=<span style="color: #0000ff;">exp(</span>M<span style="color: #0000ff;">)</span> $\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=<span style="color: #0000ff;">function(</span>Input<span style="color: #0000ff;">)</span>
Output=<span style="color: #0000ff;">log(</span><span style="color: #00ccff;">exp(</span>2<span style="color: #00ccff;">)</span><span style="color: #0000ff;">)</span> $\displaystyle \text{Output}=2$ Output=<span style="color: #0000ff;">log(</span><span style="color: #0000ff;"><span style="color: #00ccff;">N</span></span><span style="color: #0000ff;">)</span> $\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: This site uses Akismet to reduce spam. Learn how your comment data is processed.