Talk by Daniel Zwillinger
Raytheon
13 June 2001


This file contains: (1) a list of slide titles (hyperlinked), and (2) the contents of each slide.
A pdf version of the talk is available here

Contents

  1. Matlab Overview
  2. Abstract
  3. Outline
  4. Classes of computer languages
  5. Matlab: MATrix LABoratory
  6. Matlab: MATrix LABoratory
  7. Matlab: A linear algebra language
  8. Programming I
  9. Programming II
  10. Many tools for matrix manipulation I
  11. Many tools for matrix manipulation II
  12. Many tools for matrix manipulation III
  13. Functions extend naturally to higher dimensional objects
  14. Function examples
  15. Easy high level manipulations
  16. Linear algebra operations
  17. Solving systems of equations (Ax=b)
  18. Various matrix extensions
  19. Vectorized operations are fast
  20. Special variables
  21. Graphics I (two-dimensional graphics)
  22. Graphics II (three-dimensional graphics)
  23. Graphics III (matrix visualization)
  24. Graphics IV
  25. Functions
  26. Simulink
  27. Toolboxes
  28. Local facilities
  29. Octave (free Matlab look-alike)
  30. Conclusion




Matlab Overview


Daniel Zwillinger, PhD

13 June 2001

Sudbury 1-1-623    telephone 978-440-1660
Daniel_I_Zwillinger@raytheon.com

http://www.mathtable.com/zwillinger/talks/20010613/




Abstract

This course will expose users to the Matlab software language. The language will be described at a high level. Matlab's capabilities (data types, programming constructs, functions, toolboxes, and graphics) and how users tend to use them will be discussed. A discussion of when to use Matlab will be given. A demonstration will be given.




Outline




Classes of computer languages




Matlab: MATrix LABoratory




Matlab: MATrix LABoratory




Matlab: A linear algebra language




Programming I

1.
Use interactively or as programming language
(interpreted or compiled)
2.
Can link to other languages (e.g., compiled C code)
3.
Large number of included examples
4.
Conditionals, looping, functions, globals, etc
5.
Sophisticated debugger, profiler
6.
GUI development tools




Programming II

1.
Object oriented capabilities
2.
Variable number of input and output arguments
3.
Case sensitive variables
4.
Operator precedence
(a)
arithmetic (+, -, *, /, etc.)
(b)
relational (==, <, >, etc)
(c)
logical (AND, OR, NOT, etc)
5.
Memory partitioned into ``workspaces''
6.
``Toolboxes'' contain collections of functions
7.
(Optional) Space allocation for data structures




Many tools for matrix manipulation I

>> A=zeros(2,2)
A =
     0     0
     0     0
>> B=ones(3,2)
B =
     1     1
     1     1
     1     1
>> C=eye(3)
C =
     1     0     0
     0     1     0
     0     0     1
>> D=[1 2; 3 4]
D =
     1     2
     3     4




Many tools for matrix manipulation II

>> s= 1:4
s =
    1    2    3    4
>> s2= 1:2:7
s2 =
    1    3    5    7
>> s3= 1:.5:2
s3 =
  1.0000  1.5000  2.0000

>> r=rand(2,3)
r =
   0.47   0.85   0.20
   0.42   0.53   0.67
>> r(:,1)
ans =
   0.47
   0.42
>> r(:,1)'
ans =
   0.47   0.42




Many tools for matrix manipulation III

>> A=zeros(2,2);
>> B=ones(3,2);
>> C=[ [A;B], [B+5;A-7] ]
C =
     0     0     6     6
     0     0     6     6
     1     1     6     6
     1     1    -7    -7
     1     1    -7    -7

>> C(:,[1 4])
ans =
     0     6
     0     6
     1     6
     1    -7
     1    -7




Functions extend naturally to higher dimensional objects

>> log( 1 )
ans =
     0
>> log( [1 2] )
ans =
         0    0.6931
>> log( [1 2; 0 NaN] )
Warning: Log of zero.
ans =
         0    0.6931
      -Inf       NaN




Function examples

Say u=[1 2 3], then


Input   Output
u<3      [1 1 0]
all(u<3)      0
any(u<3)      1
find(u<3)      [1 2]




Easy high level manipulations

>> a = eye(4) + 0.01*rand(4,4) 
a =
    1.0095    0.0089    0.0082    0.0092
    0.0023    1.0076    0.0044    0.0074
    0.0061    0.0046    1.0062    0.0018
    0.0049    0.0002    0.0079    1.0041
>> eig(a)
ans =
   1.0232          
   1.0009 + 0.0046i
   1.0009 - 0.0046i
   1.0023




Linear algebra operations




Solving systems of equations (Ax=b)

>> A=rand(3,2)
A =
    0.7095    0.1897
    0.4289    0.1934
    0.3046    0.6822

>> b=rand(3,1)
b =
    0.3028
    0.5417
    0.1509
>> soln=A\b
soln =
    0.6387
   -0.0121




Various matrix extensions

$\bullet$ Sparse matrices

>> A=speye(100000,100000);
>> A2=2*A;
>> A2(4,5)=5;
>> nnz(A2)
ans =
       100001

$\bullet$Multidimensional arrays

>> r=rand(2,2,3)
r(:,:,1) =
    0.1389    0.1987
    0.2028    0.6038
r(:,:,2) =
    0.2722    0.0153
    0.1988    0.7468
r(:,:,3) =
    0.4451    0.4660
    0.9318    0.4186




Vectorized operations are fast




Special variables

ans most recent result
eps machine epsilon
flops total floating point ops during session
i,j $\sqrt{-1}$
inf $\infty$
NaN not-a-number
pi $\pi$
realmax largest positive floating point number
realmin smallest positive floating point number




Graphics I (two-dimensional graphics)

>> x=0:.1:10;
>> y=sin(x);
>> plot(x,y)
\resizebox {1in}{!}{\includegraphics{fig.matlab.ex1.eps}}

>> data=[2 6 4];
>> text={'a','b','c'};
>> pie(data,text)
\resizebox {1in}{!}{\includegraphics{fig.matlab.ex2.eps}}




Graphics II (three-dimensional graphics)

>> [X,Y]=... 
    meshgrid(-2:.2:2,-2:.2:3);

>> Z = X.*exp(-X.^2-Y.^2);
>> [C,h] = contour(X,Y,Z);
>> clabel(C,h)
\resizebox {1in}{!}{\includegraphics{fig.matlab.ex4.eps}}

>> t=0:.1:10;
>> x=sin(t);
>> y=cos(t);
>> z=x'*y;
>> meshc(x,y,z);
\resizebox {1in}{!}{\includegraphics{fig.matlab.ex3.eps}}




Graphics III (matrix visualization)

>> format +
>> A=random(5,15)-1/2
A =
++--+++-+++++-+
+-+---+-++-+++-
-++--+-+--+-+--
+-+--+---+++---
-+-++-----+--+-

>> B=zeros(10,10)
>> for i=1:9
>>    B(i,i+1)=2;
>> end
>> spy(B)
\resizebox {1in}{!}{\includegraphics{fig.matlab.ex5.eps}}




Graphics IV




Functions

$\bullet$ Usually, each function in its own file:



file run.m
\fbox {\begin{minipage}
{0.88in}
\texttt{function a=run(b)}\\ $\vdots$\\ \texttt{d=2*b}\\ \texttt{c=foo(d)}\\ $\vdots$\\ \texttt{a=log(c)}
\end{minipage}}




file foo.m
\fbox {\begin{minipage}
{0.98in}
\texttt{function e=foo(f,g)}\\ $\vdots$\\ \texttt{[h,i]=bar(f)}
\\ $\vdots$\\ \texttt{e=sqrt(h)}
\end{minipage}}




file bar.m
\fbox {\begin{minipage}
{1.19in}
\texttt{function [j,k,l]=bar(m)}\\ $\vdots$\space \\ \texttt{j=m} \\ \texttt{k=2*m}\\ \texttt{l=m*m}
\end{minipage}}


$\bullet$ Have variables nargin and nargout

$\bullet$ Frequently have many short files




Simulink




Toolboxes




Local facilities




Octave (free Matlab look-alike)

http://www.octave.org      (UNIX and Microsoft)

GNU Octave is a high-level language, primarily intended for numerical computations. It provides a convenient command line interface for solving linear and nonlinear problems numerically, and for performing other numerical experiments using a language that is mostly compatible with Matlab. It may also be used as a batch-oriented language.

Octave has extensive tools for solving common numerical linear algebra problems ...It is easily extensible and customizable via user-defined functions written in Octave's own language, or using dynamically loaded modules written in C++, C, Fortran, or other languages.

GNU Octave is also freely redistributable software. You may redistribute it and/or modify it under the terms of the GNU General Public License (GPL) as published by the Free Software Foundation.




Conclusion