Essential Elements of Matlab

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Essential Elements of Matlab

( EX_Matlab.m)
Lima, February 2010
Fernando Monar & Ken Nyholm

Outline
Getting Matlab up and running

Setting the path


Excel link
Command line
The Matlab work space
Writing and executing a script

Functionalities

Help
Graphing
Variables
Branching and Looping
Functions
Fmincon

(c) Monar & Nyholm

Getting Matlab up and running


The path tool tells matlab where your programs and
scripts are stored

(c) Monar & Nyholm

Getting Matlab up and running


The Excel link:
Makes it easier to tranfer data between Matlab and
Excel
Allows for execution of Matlab scripts from Excel,
either directly or from VBA

To install:
In Excel go to: Tools, add-ins, browse
And find the folder: Matlab\toolbox\excel link
see next slide

(c) Monar & Nyholm

Getting Matlab up and running

(c) Monar & Nyholm

Getting Matlab up and running


Basically two types of input formats can be
used with Matlab:
Command line entry for smaller tasks or when
executing a program
Scripts/programs and functions for larger and reoccuring tasks

(c) Monar & Nyholm

Getting Matlab up and running


Examples of command line entry:

(c) Monar & Nyholm

Getting Matlab up and running


The Matlab workspace

(c) Monar & Nyholm

Getting Matlab up and running


The workspace can be saved so that all variables are
preserved for later use and/or shared with colleagues

The workspace can be cleared by using the command clear all


and the screen is cleared by the command clc
(c) Monar & Nyholm

Getting Matlab up and running


The Matlab editor is used to write scripts and functions
It can be started from the command line by writing edit
The following editor window then appears:

(c) Monar & Nyholm

10

Getting Matlab up and running


The script should be given a name and saved in the
appropriate folder
An example is given in the lecture notes page 10 (see
next slide for the output produced)
Notice the significance of the symbols:
Semicolon ;
Percentage %

(c) Monar & Nyholm

11

Getting Matlab up and running

(c) Monar & Nyholm

12

Functionalities
There are different ways to obtain help in Matlab
a) Help on a particular function:

From command line: >> help function name


For example:

(c) Monar & Nyholm

13

Functionalities
b) From the Matlab built-in help function:

(c) Monar & Nyholm

14

Functionalities
Branching and Looping, i.e. conditional execution of
code, requires relation operators:

(c) Monar & Nyholm

15

Functionalities
Branching:

(c) Monar & Nyholm

16

Functionalities
The if and case statements:

(c) Monar & Nyholm

17

Functionalities
Looping:

(c) Monar & Nyholm

18

Functionalities
A function in Matlab typically has the following
components:
Header that specifies the name, input and output variables,
and the keyword function that tells Matlab that the
following should be interpreted as a function
An introduction section that tell the user how to use the
function
The text that performs the calculations of the function

Functions are a useful structure for tasks that are


repeated many time and that can be formulated in
somewhat general terms
(c) Monar & Nyholm

19

Functionalities
An example of a function:

(c) Monar & Nyholm

20

Functionalities
One of the most useful function for our purposes is
the Matlab built-in function fmincon
It can be use to find the arguments that minimise a
particular function
We will use this function to:

Solve likelihood optimisation


Find optimal regression coefficients
Find optimal portfolio weights
Find efficient frontier portfolios
Find the yield (internal rate of return) on a stream of bond
payments

(c) Monar & Nyholm

21

Functionalities

(c) Monar & Nyholm

22

Functions in Matlab
User defined functions were presented in the
slide-set called An Introduction to Matlab

Here we go a bit more in detail


Such functions are mainly used to:
Automise tasks that are to be performed several
times
Implement theoretical models so we can calculate
the function value for given and varying inputs and
the arguments the maximise/minimise the function

It is the latter application that we focus on here


(c) Monar & Nyholm

23

Functions in Matlab
Matlab uses a function handle when
functions are defined or used
If a function is defined in a file the follwing
structure has to be use:
function [output] = function_name(input)

A function defined in this way can be called


from another file/program or from commandline mode by:
[my_output] = function_name(my_input)

(c) Monar & Nyholm

24

Functions in Matlab
And it can be used as an input to a built-in
Matlab function, for example fmincon, usin the
handles @ or function_name, in the
following ways:
[Est,fval,flag,outp,lambd,grad,hess] = ...
fmincon(@var_p_lik, pStart,[],[],[],[],LB,UB,[],options_);

[param, fval, exitflag, outpt, la, g, H] = ...


fmincon('regime_likeli',S_param,A,A_u,[],[],lb,ub,[],options);

(c) Monar & Nyholm

25

In-line functions
In-line functions refer to the concept of
defining the object of interest in just one line
within the program that condusts the
optimisation or function value calculation
This can be achieved by:

(c) Monar & Nyholm

26

You might also like