Writing and Using Functions in MATLAB

Một phần của tài liệu Handbook of computational finance (Trang 785 - 788)

MATLAB has many built-in functions that execute commonly used tasks in linear algebra, data analysis, and engineering. Some of these standard functions include trigonometric functions (sin, cos, tan, and so on), log, exp, specialized functions (Bessel, gamma, beta, and so on), and many more. In this section, we provide an introduction on writing your own functions and programs in MATLAB.

28.3.1 Script Files and Functions

MATLAB programs are saved in M-files. These are text files that contain MATLAB commands and expressions, and they are saved with the .mextension. Any text editor can be used to create them, but the one that comes with MATLAB is recommended. It has special color coding and other helpful features to write MATLAB programs that execute correctly.

When script M-files are executed, the commands are implemented just as if they were typed at the prompt. The commands have access to the workspace and any variables created by the script file are in the workspace when the script finishes executing. To execute a script file, simply type the name of the file at the command line or prompt.

Script files and functions have the same.mfile extension. However, a function has a special syntax for the first line. In the general case, this syntax is

function [out1,..., outM] = func name(in1, ..., inN) A function does not have to be written with input or output arguments. Also, a function can be called with fewer input and/or output arguments, but not more.

The function corresponding to the above declaration would be saved in a file called func name.m, and it is invoked usingfunc name.

It is important to understand the scope of MATLAB workspaces. Each function has its own workspace, which is separate from the main MATLAB workspace.

Communicating information about variables and their values is accomplished by way of the input and output variables. This concept is very important when writing and debugging functions.

It is always a good idea to put several comment lines at the beginning of a func- tion. This is the information that is returned by the commandhelp func name.

At a minimum, this should include a description about what the function does and what types of variables are used for inputs and outputs.

Most computer languages provide features that allow one to control the flow of execution that depends on conditions. MATLAB has similar constructs, and these are listed here:

• For loops

• While loops

• If-else statements

• Switch statement

These should be used sparingly to make the code run fast. In most cases, it is more efficient in MATLAB to operate on an entire array rather than looping through it. It is important to note that most of the functions in MATLAB operate on entire arrays, alleviating the need to loop through the arrays. A brief description of these programming constructs is given below.

The basic syntax for a for loop is for i = array

commands end

The looping variable is given by i, and the loop runs one time for each element in the variable array. The variable i takes on the next value in array and the commands between thefor andendare executed each time the loop executes. The colon notation is often used to generate a sequence of numbers for the variable array. For example, usingfor i = 1:10means that i would take on the values 1 through 10. Severalforloops can be nested, with each one terminated by anendstatement.

Unlike aforloop, awhileloop executes an indefinite number of times. The general syntax is

whileexpression commands end

The commands between thewhileand theendare executed as along as expression is true. (Note that in MATLAB a scalar that is nonzero evaluates to true.) Usually, a scalar entry is used in expression, but an array can be used also. In the case of arrays, all elements of the resulting array must be true for the commands to execute.

Sometimes commands must be executed based on a relational test. The if-else statement is suitable in these situations. The basic syntax is

if expression commands elseif expression

commands else

commands end

Only oneendis required at the end of the sequence ofif,elseifandelse statements. Commands are executed only if the corresponding expression is true.

Note that in the simplest case, one might only need to use the following if expression

commands end

In other words, you do not necessarily need to have theelseconstructs.

Theswitchstatement is useful if one has a lot ofif,elseifstatements in the program. This construct is very similar to that in the C language. The basic syntax is

switch expression case value1

commands % executes if expression = value1 case value2

commands % executes if expression = value2 ...

otherwise

commands % executes if nothing else does end

The expression must be either a scalar or a character string.

Một phần của tài liệu Handbook of computational finance (Trang 785 - 788)

Tải bản đầy đủ (PDF)

(817 trang)