elementsofmatlab_2.pdf

26
Essential Elements of Matlab ( EX_Matlab.m) Fernando Monar & Ken Nyholm Lima, February 2010

Upload: luis-ricardo-vigo-verastegui

Post on 17-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ElementsofMatlab_2.pdf

Essential Elements of Matlab( EX_Matlab.m)

Fernando Monar & Ken Nyholm

Lima, February 2010

Page 2: ElementsofMatlab_2.pdf

2

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

Page 3: ElementsofMatlab_2.pdf

3

Getting Matlab up and running

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

(c) Monar & Nyholm

Page 4: ElementsofMatlab_2.pdf

4

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

Page 5: ElementsofMatlab_2.pdf

5

Getting Matlab up and running

(c) Monar & Nyholm

Page 6: ElementsofMatlab_2.pdf

6

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 re-

occuring tasks

(c) Monar & Nyholm

Page 7: ElementsofMatlab_2.pdf

7

Getting Matlab up and running

• Examples of command line entry:

(c) Monar & Nyholm

Page 8: ElementsofMatlab_2.pdf

8

Getting Matlab up and running

• The Matlab workspace

(c) Monar & Nyholm

Page 9: ElementsofMatlab_2.pdf

9

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 alland the screen is cleared by the command clc

(c) Monar & Nyholm

Page 10: ElementsofMatlab_2.pdf

10

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

Page 11: ElementsofMatlab_2.pdf

11

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

Page 12: ElementsofMatlab_2.pdf

12

Getting Matlab up and running

(c) Monar & Nyholm

Page 13: ElementsofMatlab_2.pdf

13

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

Page 14: ElementsofMatlab_2.pdf

14

Functionalities

b) From the Matlab built-in help function:

(c) Monar & Nyholm

Page 15: ElementsofMatlab_2.pdf

15

Functionalities

• Branching and Looping, i.e. conditional execution of

code, requires relation operators:

(c) Monar & Nyholm

Page 16: ElementsofMatlab_2.pdf

16

Functionalities

• Branching:

(c) Monar & Nyholm

Page 17: ElementsofMatlab_2.pdf

17

Functionalities

• The if and case statements:

(c) Monar & Nyholm

Page 18: ElementsofMatlab_2.pdf

18

Functionalities

• Looping:

(c) Monar & Nyholm

Page 19: ElementsofMatlab_2.pdf

19

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

Page 20: ElementsofMatlab_2.pdf

20

Functionalities

• An example of a function:

(c) Monar & Nyholm

Page 21: ElementsofMatlab_2.pdf

21

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

Page 22: ElementsofMatlab_2.pdf

22

Functionalities

(c) Monar & Nyholm

Page 23: ElementsofMatlab_2.pdf

23

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

Page 24: ElementsofMatlab_2.pdf

24

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 command-

line mode by:

[my_output] = function_name(my_input)

(c) Monar & Nyholm

Page 25: ElementsofMatlab_2.pdf

25

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

Page 26: ElementsofMatlab_2.pdf

26

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