Script M-file

Constructing Function M-files

Function M-files allow you to define, construct and store your own functions.

Why would I want to do that? You may want to create a function that plots a graph with certain fixed parameters, or create a complex equation that you will call often, or even, you may want to create a modified version of an existing function, such as a random number generator that creates numbers with values between 100 and 200 instead of 0 and 1.

First, let’s recall some of the in-built MATLAB functions you have already used. If you type command help <name of the function> , such as help abs, in the Command Window, MATLAB will print description and correct syntax of this function. These functions have a few things in common and a few differences.

wink Coding Tip: If the function you use does not work, try to type help <name of the function> to check for correct syntax. This may also help you immensely during the MATLAB test.

Function: y = abs(x)

Description: Assigns the value x to y if x is non-negative, or –x if x is negative.

Input: 1 number: x

Output: 1 number: either x or –x

 

Function: y=rem(a,b)

Description: Assigns the remainder of a/b to y

Input: 2 numbers: a is the numerator, b is the denominator

Output: 1 number: remainder of a/b

 

Function: plot(xArray,yArray)

Description: Plots a graph, given an array of x-coordinates, xArray, and an array of y-coordinates, yArray.

Input: 2 arrays of equal length: xArray, yArray.

Note: there can be many additional optional inputs.

Output: A graph.

 

Exercise: Determining the Inputs & Outputs

> For the following function (which you may recall from the first practical), use MATLAB to help you write the description, inputs and outputs.

Function: round(a)

Description: _________________________________________

Inputs: _________________________________________

Outputs: _________________________________________

> Exercise: Creating a Customised Random Number Generator

> Create a function M-file called myRand.m that outputs a random number between the inputted values of minRand and maxRand by adding code in the starred lines.

The MATLAB rand function returns a random value between 0 and 1. You will need to use this function as well as calculating the scale and offset values.

Example: If you want to find a number between 3 and 10, your scale is 7 and your offset is 3.

% <insert your name and the date here>

% myRand.m

% inputs: minRand, maxRand

% outputs: y, a random number with value between

% minRand & maxRand

 

function y = myRand(minRand, maxRand)

% **calculate the scale**

% **calculate the offset**

% **calculate y, using your scale, offset and %MATLAB’s rand function**

Note: Save your function in the Current Directory, otherwise you will need to switch to the directory you saved your function in or type in a full path.

    > Type in the following lines to call this function from your Command Window and verify that it works.

    myRand(1,10)

    myRand(100,100+1)

    myRand(3,pi)

    myRand(20)

    myRand(20,1)

    > How do you think your function should handle the last two entries? You may need to edit your function to accommodate inputs of this form or generate message about the error.