Beruflich Dokumente
Kultur Dokumente
Matlab Tutorial
What is MATLAB?
both a computing environment and a language initially developed as an easy interface to LAPACK: Linear Algebra Package
(FORTRAN libraries) MATrix LABoratory Written in C. For matrix computations, it calls C/Fortran libraries == Fast Matlab + "Toolboxes"
Symbolic Math Toolbox: mathematical manipulation of symbols Partial Differential Equation Toolbox: tools for solving PDEs in 2-D Statistics Toolbox: statistical data analysis Image processing toolbox: visualization and image analysis Bioinformatics toolbox: computational molecular biology Compiler: application development many many more. http://www.mathworks.com
Why Matlab?
The Good:
Fast development times no compiling, easy debugging accessible syntax and language constructs in-house graphics capabilities tons of basic "libraries" or functions available many more complicated "toolboxes" can be added
The Bad:
small coding mistakes can result in slow code loops are extremely computationally intensive language is limited: no templates, classes etc.
The Ugly:
proprietary (but the language format is open) expensive the open source substitue, Octave, is not fully compatible
3
How to Matlab?
Lukes 4 big ideas: Let Matlab help you Everything is a matrix tri-development: interactive, scripting, functions (next time) visualize
Everything is a Matrix
>> a = [1 2 3 4 5] >> b = some string >> whos >> a(3) >> b(3) >> length(a) >> length(b)
Exercise #1
Exercise #1
do not use loops consider n = 100, 1000, 10000 can be done in 4 lines:
Exercise #1
Exercise #2
Ruffed Grouse: Primary upland game bird of central/northern Wisconsin
First Column: year Second Column: Average number of "drums" per stop (overlap starting at year
1994. Line 34) Plot the data. Are there any trends?
10
Exercise #2
11
12
13
Functions
save in functionname.m can have 0, n, or variable number of inputs can have 0, n, or variable number of outputs use return; to leave a function early use inline(...) to define without another file variables not "passed in" are local: only available within
the function scope use global varname; to declare a variable that is visible through all functions
15
More on functions:
use eval to call a function from a string varnargin: variable number of argument inputs nargin: number of argument inputs varnargout: variable number of argument outputs nargout: number of argument outputs
16
17
Vectorize!
18
19
20
Visualizing Data
21
Advice on programming
use disp pause spy surf whos profile and semicolons to
help debug and visualize your code. Matlabs in-house debugger often not enough vectorize when you can avoid loops when you can modularize your code...this will take practice
22
23
An excercise
24
Any Question??
25