Sie sind auf Seite 1von 25

Chapter 2

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

Let Matlab Help You


>> help >> doc >> lookfor Actually read the error messages

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

Wisconsin DNR data: grouse.dat


1964 1.14 1965 1.02 1966 0.94 . .

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

Today: Programming Basics


Three things to keep in mind about Matlab when programming: everything is linear script versus function vectorize vectorize vectorize

12

Interpreter vs. Scripts vs. Function

13

Interpreter vs. Scripts vs. Function


Interpreter: quick access good for developing ideas good for accessing data in the workspace poor performance and editing/debugging capabilities Script good test bed: all info in the workspace fast, linear poor profiling/optimization Functions can be optimized and cached doesnt fill up the workspace with memory that you have to manage, but... pass by value (ouch!) reuseable code
14

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

break versus return

17

Vectorize!

18

More vectorization: Memory


Preallocate memory: set size of the matrix beforehand: A=zeros(n) this allocates a chunk of memory at ones instead of multiple times on the fly

19

Hany Vectorized Functions


all(X): true if all elements are nonzero any(X): true if any elements are nonzero is*: checks for *: isnan, isfinite, isinf, isempty, isnumeric find: finds subset satisfying conditions

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

Advice on "good" code write-ups

23

An excercise

24

Any Question??

25

Das könnte Ihnen auch gefallen