Getting started Mihaela Duta mihaela.duta@maths.ox.ac.uk 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 Need more help? mihaela.duta@maths.ox.ac.uk www.mathworks.com http://www.mathtools.net/matlab/index.html 1
Today s topics Programming basics MATLAB background Development environment Scripts and functions Data types Matrices Arithmetical operators Interfacing with the OS 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. Semantics/syntax Semantics reflects the meaning of programs Syntax: the set of allowed reserved words and possible order of instructions in a program 2
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 MATLAB background Very powerful tool for technical computing Integrates Computation Visualisation Programming Interactive and easy to use 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 3
Overview Application Program Interface (API) Development Environment Command window Editor/Debugger Browsers (help etc) Graphics The MATLAB system The MATLAB language Function library 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 The Workspace Accumulates variables handled in a session Can be manipulated from Workspace browser Command line 4
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 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 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; 5
Data types Basic: Numerical 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 MATRIX The fundamental two-dimensional data storage unit Created with matrix creator operator [ ] >> a = [1 2 3 4]; % 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) = []; 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. 6
( * vs.* ), (/ vs./ ), (^ vs.^ ) ( * vs.* ), (/ vs./ ) 10 20 1 2 70 100 * 30 40 3 4 = 150 220 matrix multiplication vs 10 20 1 2 10 40.* 30 40 = 3 4 90 160 element-wise (^ vs.^ ) 10 20 10 20 10 20 700 1000 ^ 2 * 30 40 = 30 40 = 30 40 1500 2200 matrix power vs 10 20 100 400. ^ 2 = 30 40 900 1600 element-wise Strings Arrays of alpha-numeric elements Elements accessed like array elements Can be concatenated Useful functions strcmp/strcmpi strfind/findstr sprintf/sscanf num2str/str2num 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 7
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 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 8