## Writing functions in MATLAB by Chandradeepa Kurera

Last month we have learnt how to deal with Matrices in MATLAB, in this lesson I have decided to discuss about writing functions in MATLAB. At the moment we know some operators and functions in MATLAB. We have used them and solved problems by entering command by command in the command window. Using functions we are able to bundle those commands and use them directly. In this lesson we will try to understand how to write functions and use them in MATLAB.

You should be familiar with functions unless you are new to computer programming, however we will start from the beginning here. In MATLAB you need a file called M-File to write those functions. You can write a function in an M File and then run them in the command window. We will discuss how to get an M File, Write a function and then run the function.

First of all you have to get in to the editor, you can get the editor by following Start > Desktop Tools > Editor. You may find the Start button on the left-bottom corner of the MATLAB environment. Then you will be able to see the Editor in the Environment. Now we are ready to type the function. First of all we will try to understand the structure of a function. The following diagram shows the structure. First the keyword function should be added then a variable is assigned. Function name and its arguments are then added. This line is known as the Function Definition Line. You may add no arguments, one argument or more than one argument. It depends on your need. Then the body of the function and last the keyword end is added.

CODE

``` function variable = function_name(arg_list) function body end ```

As an example we now try to write a trivial function that takes two inputs from the user and then output the addition of two numbers. First we have to write the Function Definition line, therefore we will include the keyword function and then a variable is assigned to hold the output. Then we have to provide a name for the function and then the argument list. We will name it as addTwoNum, something meaningful and short. According to the need we have to get two user inputs so we should have two variables to take the inputs. They will be num1 and num2. Bare mind to separate variables using a comma (,) when you use more than one variable. Now we have all data, we can write the function definition line.

CODE

``` function r = addTwoNum(num1, num2) ```

Now we have to complete the rest. You can include MATLAB command in the body as they solve the problem. We have to add commands to add two numbers in the body section. Then keyword end should be appended. The whole code should be in the following manner.

CODE

``` function r = addTwoNum(num1, num2) r = num1 + num2; end ```

Then the M File should be saved. Use the Save button and the save it from the same name of the function. Then the last phase, to test the function in the command window. Select the saved M File directory from Current Directory, you may find it on the MATLAB toolbar. Now the work is done, you can check the function on the command window.

CODE

``` > > addTwoNum(10, 20) ``` As an exercise try to develop a function to finds roots of a quadric equation. Three coefficients should get as an input. Two roots should be output as an array. But there is a problem then, how can we output an array from a function. It is very easy. Try to understand it from the solution. The solution is given below

CODE

``` function r = findRoots (a, b, c)   x1=((-1*b)+((b^2)-(4*a*c))^0.5)/2;   x2=((-1*b)-((b^2)-(4*a*c))^0.5)/2;   r = [x1, x2]; end ```

You are able to call the function as follows.

CODE

``` >>findRoots (1, 2, 1) ```

It is very easy to form a matrix as the output, what you have to do is form a matrix and assign it to the variable which output the value in the function. Suppose you need to get the roots as a 2x2 matrix and they should lie on the diagonal of the matrix. Then you can achieve it by changing the fourth line as follow.

CODE

``` r = [x1,0 ; 0, x2]; ```

Try to be familiar with matrices as much as possible. In this series of lessons I am trying to teach you how to use MATLAB to make our calculations easy. Therefore you have to have the mathematical knowledge. If anything remains unclear please let me know. Visit http://bckurera.info/learning/ for more details. See you in the next issue with another interesting topic in MATLAB.

Previous Article 