MATLAB Part 1 Oct 13 2010 1
Rough schedule First week, MATLAB as calculator : basics, MATLABspecific syntax, MATLAB command line (the MATLAB shell ) Second week, programming in MATLAB : more detail about arrays, functions, vectorized calculations, basic profiler Third week: more advanced plotting, debugger, profiler 2
What is MATLAB? MATrix LABoratory: www.mathworks.com MATLAB is an interpreted language This means you can write programs but you can also use it interactively to analyze and display data, simulations, etc. (In a sense, it is like a much more powerful Excel) The interactive nature makes it extremely useful for initial analysis of data or development of algorithms Creating a program requires a plan of what to do ahead of time; interactive use lets you develop on the fly. MATLAB is good at moving between these two regimes. 3
MATLAB strengths There are really no truly standard programming languages in atmospheric science, but MATLAB comes close On the fly type conversion & variable creation Lots of built-in functions & toolboxes to leverage Lots of built in graphics Many ways to solve numerical problems 4
MATLAB weaknesses The huge library of built in functions can be overwhelming there are probably too many different ways to do the same thing. Some functionality is split into toolboxes, which each can introduce their own peculiarities Cost each tool box is an added charge but, student license is reasonable, $100 from DoIT open source alternative (Octave) http://www.gnu.org/software/octave Octave is almost completely equivalent for most computations, but the graphics are somewhat different 5
Different ways to run MATLAB From Windows: run from start menu or desktop icon (if there is one) From UNIX shell: The GUI user> matlab & Lightweight options this will run it inside the terminal window (good if you are running remotely) user> matlab -nodesktop user> matlab -nojvm 6
The GUI (lots of stuff) 7
Editor, Figure Variable List, Command History, Command Window 8
Command Line Similar to a (graphing) calculators simple_example_script.m... 9
Basic Scripts Can easily collect basic commands into a script file (or sometimes called a batch script e.g., run a batch of commands) Go back & forth with mouse cursor context menu, cells in the script file Examples... 10
MATLAB syntax Some MATLAB specifics comment character % Relational operators: == ~= < > <= >= Operators: +.*./.^ (for arrays) * / ^ (for matrices, scalars more on this later) Arrays are indexed as A(n, m). First index is 1 LOTS of ways to create arrays (more on that later) End each line with ; (otherwise the result is echoed to the command line) Variable names are case sensitive (Search Naming Variables for more info) 11
MATLAB variable classes Standard things: Numeric types, Logical (True or False), Characters, Structures Arrays (note that all variables in MATLAB are really arrays scalars are actually 1 element arrays) Not so standard things: Empty array (zero elements) Cell arrays (more on these later) Function handles Use whos or class(x) to find a variable's type. 12
MATLAB numeric types Any numeric array created in MATLAB automatically defaults to double precision Helpful because you rarely need to worry about floating point roundoff error with double precision Not helpful because data is rarely stored as double precision large data arrays will use up twice as much memory Remember that you might need to round the data to get integers. In general, if x is some floating point variable, the expression: may or may not be equal to 1! 100 x 99 x x 13
The empty array Lots of ways to make one (but rarely do you want to). Example below. Some built-in functions return these, so mind them... >> not_empty_array = 1; empty_array = []; >> size(not_empty_array) 1 1 >> size(empty_array) 0 0 >> isempty(not_empty_array) 0 >> isempty(empty_array) 1 14
Function Handle Can create a variable that is a reference to a function Useful for certain built-in numeric computations (like quad: numeric integration on any function specified by the input function handle) Think of this as a representation of a mathematical function (rather than a function as a computer programming term.) Syntax: function_handle = @function_name; function_handle = @(x) f(x); 15
Function Handle Simple example cos(x), integrate from 0 pi/4. Analytic result is / 4 0 cos x dx= [sin x ] 0 / 4 = 1 2 >> foo = @(t) cos(t); >> quad(foo,0,pi/4) 0.7071 >> sqrt(1/2) 0.7071 16
Function Handle Simple example x 3, and then integrate from 0 1.5. 3 /2 Analytic result is 0 >> xcubed = @(x) x.^3; >> quad(xcubed, 0, 1.5) 1.2656 x 3 dx=[ 1 4 x4]0 3 /2= 1 4 3 2 4 = 81 64 >> 81/64 1.2656 >> quad(xcubed, 0, 1.5) - 81/64-2.2204e-16 17
Let's make something useful Blackbody curve (emittance) as a function of wavenumber: E,T =1.191 10 8 [W / m 2 cm 1 ] 3 exp 1.439[K cm] T 1 Create a MATLAB function handle use quad to integrate & compare to Stefan-Boltzmann E T =5.67 10 8 [W m 2 ]T 4 18
Let's make something useful E,T =1.191 10 8 [W / m 2 cm 1 ] 3 exp 1.439[K cm] T 1 bbemit = @(T,nu) 1.191e-8.*pi.*nu.^3./... (exp(1.439*nu./t)-1); E T =5.67 10 8 [W m 2 ]T 4 stefbolt = @(T) 5.67e-8 * T.^4; 19
Let's make something useful >> T = 255; >> stefbolt(t) 239.7418 >> quad(bbemit,1,8000)??? Input argument "nu" is undefined. Remember that bbemit requires 2 inputs T and nu. Need to create another function handle that sets the value of T: >> bbemit_t = @(nu) bbemit(t,nu); >> quad(bbemit_t, 1, 8000) 239.6003 20