Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to MATLAB

Similar presentations


Presentation on theme: "Introduction to MATLAB"— Presentation transcript:

1 Introduction to MATLAB
Session 1 Getting started Mihaela Duta MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

2 Recommended bibliography
“Matlab Guide” – D. Higham and N. Higham, “Matlab primer” – A. Davis “A Guide to MATLAB: For Beginners and Experienced Users”, B. Hunt, R. Lipsman and J. Rosenberg “Essential MATLAB for Scientists and Engineers” – B.Hahn “Numerical Methods in Finance: A MATLAB-based Introduction ”, P. Brandimarte MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

3 Need more help? mihaela.duta@maths.ox.ac.uk www.mathworks.com
MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

4 Today’s topics Programming basics MATLAB background
Development environment Scripts and functions Data types Matrices Arithmetical operators Interfacing with the OS MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

5 Programming language An artificial language used to control the behaviour of a computer, in order to manipulate information and express algorithms. Structure and meaning is determined with syntactic rules semantic rules respectively. MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

6 Semantics/syntax Semantics reflects the meaning of programs Syntax:
the set of allowed reserved words and possible order of instructions in a program MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

7 Algorithm A procedure defined as a finite set of well-defined instructions for accomplishing some task – recipe Implemented as functions They often have iterations - repetitions of a process: loops conditionals - execution choices based on a given condition: conditional statements MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

8 MATLAB background Very powerful tool for technical computing
Integrates Computation Visualisation Programming Interactive and easy to use MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

9 MATLAB short history Cleve Moler invented MATLAB in late ’70s
Very popular within the applied maths community Joined by Jack Little and Steve Bangart to rewrite MATLAB in C found Mathworks in 1984 MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

10 Application Program Interface (API)
Overview Development Environment Command window Editor/Debugger Browsers (help etc) Application Program Interface (API) The MATLAB system Graphics Talk through the slide The MATLAB language Function library MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

11 The development environment
Collection of graphical windows Command line window Editor/Debugger Browsers for help, workspace, commands history, directory Apart from command line window, all the other are optional – everything can be done in the command window Let’s have a look now at what has MATLAB environment to offer. Open MATLAB Discuss initial desktop configuration Name and describe briefly each visible window Show how you can type commands Show how one can customize the desktop by adding/removing windows Open the browser and point out that the studentsare supposed to make full use of help when solving the exercises MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

12 The Workspace Accumulates variables handled in a session
Can be manipulated from Workspace browser Command line MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

13 Scripts Collection of MATLAB statements Stored in a .m file
They runs in the same memory area with the main MATLAB session: Use variables from the workspace Changes made to the workspace remain after script finishes Give operators example. Show workspace contents before and after MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

14 Functions Collections of MATLAB statements Syntax for declaration
function [output args] = fname(input args) Run in a separate memory space than the main MATLAB session: does not have access to the callers workspace changes made to variables are local variables passed via input/output arguments Same example with operators MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

15 Code comments Any text starting following %
Recommend – document all code while implementing it Example: function umax = exmax(u1,u2) % function exmax(u1,u2) % analytic extension of real max function sw = real(u1) > real(u2); umax = sw.*u1 + (1-sw).*u2; MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

16 Data types Basic: Numerical Logical Strings Advanced: Cell arrays
stored floating-point double-precision ( “double ) – a format that occupies two storage locations in computer memory e.g. >> a = 3.14; format: controls the way numerical variables are echoed in the command window Logical >> a = true; Strings >> a = 'Some text' ; Advanced: Cell arrays >> a = {'Text' , [1 2 3], [true false]}; Structures >> a.location = 'Oxford'; a.date = '12-Oct-05'; Objects No need to declare the variables a = 1; whos a = ‘text’ whos MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

17 MATRIX The fundamental two-dimensional data storage unit
Created with matrix creator operator [ ] >> a = [ ]; % one-dimensional array (vector, array) >> b = [1 2; 3 4]; % two-dimensional matrix Element separator: space, column separator: ; Access elements with numerical indices >> c = a(1); >> i = 2; d = a(i); Concatenate >> a = [1 2]; b= [3 4]; >> c = [a b]; Selectively display rows/columns >> a = [1 2; 3 4; 5 6]; >> b = a(2, :); Empty matrix >> c = []; Delete rows/columns >> a(2) = []; Bring back the previous examples a = 1; whos Show formats MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

18 Arithmetical operators
+, -, *, / >> a=[1 2]; b = [3 4]; c = a+b; their element-wise counterparts .* , ./ >> a=[10 20;30 40]; b=[1 2; 3 4]; c = a./b; d = a/b; ^ (raise to the power of); also .^ >> a = [10 20; 30 40]; b = a.^2; c = a^2; ' (transpose) >> a = [1 2 3]; b = a'; \ (left division) – A\B: If A is a square matrix, A\B is roughly the same as inv(A)*B, except it is computed in a different way. Give extensive examples MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

19 ( * vs .* ) , (/ vs ./ ), (^ vs .^ ) ( * vs .* ) , (/ vs ./ ) vs
matrix multiplication element-wise (^ vs .^ ) matrix power element-wise MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

20 Strings Arrays of alpha-numeric elements
Elements accessed like array elements Can be concatenated Useful functions strcmp/strcmpi strfind/findstr sprintf/sscanf num2str/str2num MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

21 Structures A data unit made up of containers called fields
The fields can be any data type Compact way of storing data <struct_name>.<fieldX> Build a structure by assigning values to its fields: >> a.location = 'Oxford'; struct function: >> a = struct(‘location’, ‘Oxford’); rmfield/isfield Can be used to build matrices MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

22 Cell arrays A way of storing a miscellaneous collection of data in matrix form {} – cell arrays constructor operator >> a = {'Text' , [1 2 3], [true false]}; Indices for elements given in {} >> a = {2} can be concatenated sort/size apply if these operations make sense for the elements of the cell array MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1

23 Interface with the OS Run OS commands from MATLAB prompt with preceding them with the character ! e.g. >> !dir Manipulate files and directories using MATLAB functions: path pwd cd copyfile/movefile/delete mkdir/rmdir MSc in Mathematical and Computational Finance Introduction to MATLAB Session 1


Download ppt "Introduction to MATLAB"

Similar presentations


Ads by Google