You are on page 1of 36

Introduction to MATLAB

Yunkai Liu Assistant Professor Computer Science Department University of South Dakota

What is MATLAB?
MATLAB (MATrix LABoratory)
– –

MATLAB is developed by The MathWorks, Inc. MATLAB is a high-level technical computing language and interactive environment for algorithm development, data visualization, data analysis, and numeric computation. MATLAB can be install on Unix, Windows and Mac.

History of MATLAB

Ancestral software to MATLAB
– –

Fortran subroutines for solving linear (LINPACK) and eigenvalue (EISPACK) problems Developed primarily by Cleve Moler in the 1970’s

History of MATLAB  In 1970. Cleve Moler. the chairman of the computer science department at University of New Mexico. designed MATLAB to give his students to access LINPACK and EISPACK without requiring knowledge of Fortran .

In1984. con’t: 2   It soon spread to other universities and found a strong audience within the applied mathematics community. .History of MATLAB. Cleve Moler and Steve Bangert rewrote MATLAB in C with more functionality (such as plotting routines). Jack Little.

MathWorks Product Overview .

Distributed Computing @ matlab .

and Fortran. C++.MATLAB MATLAB is a high-level language and interactive environment that enables you to perform computationally intensive tasks faster than with traditional programming languages such as C. .

Simulink. . and can be extended for specialized applications.Simulation and Model-Based Design Simulink is a platform for multidomain simulation and Model-Based Design for dynamic systems. It provides an interactive graphical environment and a customizable set of block libraries.

Statistics Toolbox 5.1 Apply statistical algorithms and probability models Key Features  Calculation and fitting of probability distributions  Linear and nonlinear modeling  Multivariate statistics  Descriptive statistics  Analysis of variance (ANOVA)  Hypothesis testing  Industrial statistics (Statistical Process Control. Design of Experiments)  Statistical plotting and data visualization .

proteomic.Bioinformatics Toolbox 2.2 Read. and visualize genomic. analyze. and microarray data Key Features  Genomic. proteomic. and gene expression file formats  Internet database access  Functions for pairwise and multiple sequence alignment  Sequence analysis tools  Phylogenetic tree analysis tools  Capabilities for microarray data analysis and visualization  Support for mass spectrometry preprocessing and analysis  Statistical learning functionality .

and units  Project files that store models with simulation settings and userdefined plot types . and nonstiff deterministic solvers  Model components. algebraic rules. design. including species. and simulate biochemical pathways Key Features  Access to all functions via the command line and a graphical user interface  Stochastic. reactions. stiff deterministic.SimBiology 1 Model. parameters. kinetic laws.

Although primarily procedural. . MATLAB may behave like a calculator or as a programming language. MATLAB does have some object-oriented elements. errors are easier to fix.Strengths of MATLAB      MATLAB is relatively easy to learn. MATLAB is interpreted. MATLAB code is optimized to be relatively quick when performing matrix operations.

and Microsoft Excel . Fortran. Java. such as C. C++.Other Features    2-D and 3-D graphics functions for visualizing data Tools for building custom graphical user interfaces Functions for integrating MATLAB based algorithms with external applications and languages. COM.

.Weaknesses of MATLAB    MATLAB is NOT a general purpose programming language. MATLAB is designed for scientific computation and is not suitable for some things (such as parsing text). MATLAB is an interpreted language (making it for the most part slower than a compiled language such as C++).

provides updates to MATLAB.The Latest Update   The latest version of MATLAB is 7. Release 14 with Service Pack 3 (R14SP3). . 2005.1. and 75 other products. launched on September 1. Simulink.

Licenses  MATLAB @ home – – Standard edition  Available for roughly two thousand dollars Student edition   Available for roughly one hundred dollars. Some limitations. such as the allowable size of a matrix or some toolboxes.  MATLAB @USD Art & Science Building 16 B  MATLAB @ SDSU Solberg 1st floor .

. Prentice Hall. Bruce Mastering MATLAB 7. Amos Gilat. 2004.html http://www.ccs.neu. Wiley. MATLAB: An Introduction with Applications. Duane C.References     ndex. 2003. Hanselman. Littlefield.

Components of MATLAB Interface     Workspace Current Directory Command History Command Window .

Interactive Calculations    Matlab is interactive. b=1. no need to declare variables >> 2+3*4/2 >> a=5e-2. a+b Most elementary functions and constants are already defined >> cos(pi) >> abs(1+i) >> sin(pi)     .

Variable and Memory Management Matlab uses double precision >> format long (15 fixed point format) >> format compact (5 fixed point format) All variables are shown with >> who lists the variables currently in the workspace. >> whos whos is similar to who. but also gives size and storage information Variables >> can be stored on file save filename >> clear >> load filename .

The Help System  Search for appropriate function >> lookfor keyword Rapid help with syntax and function definition >> help function An advanced hyperlinked help system is launched by >> helpdesk Complete manuals    .mathworks.

-6.4.Vectors and Matrices    Vectors (arrays) are defined as >> v = [1. 2. 4. 5]   Matrices (2D arrays) defined similarly >> A = [1.3.-5. 2.6. 4.5. 5] >> w = [1.7] .2.

Matrix Operators        All common operators are overloaded >> v + 2 Common operators are available >> B = A’ >> A*B >> A+B Note: Matlab is case-sensitive A and a are two different variables .

2).1).A(2. A(2.A(3.3) Index submatrices using vectors of row and column indices >> A([2 3].2).[2 1]) .1)] >> B=A([3 2].[1 2]) Ordering of indices is important! >> B=[A(3.Indexing Matrices Indexing using parentheses >> A(2.

:) Can also add limit index range >> A(1:2.:) General notation for colon operator >> v=1:5 >> w=1:2:5 .Indexing Matrices Index complete row or column using the colon operator >> A(1.:) >> A([1 2].

*A Note: in general.<operator>” is elementwise operation     . ”. >> I=eye(3)     Elementary functions are often overloaded >> help elmat >> sin(A) Specialized matrix functions and operators >> As=sqrtm(A) >> As^2 >> A.Matrix Functions    Many elementary matrices predefined >> help elmat.

 .2.Numerical Linear Algebra Basic numerical linear algebra  >> z=[1.D]=eig(A) [V. x=inv(A)*z  >> x=A\z Many standard functions predefined  >> det(A) -.determinant  >> rank(A) – matrix rank  >> eig(A)--Eigenvalues and eigenvectors The number of input/output arguments can often be varied  >> [V.3].D] = EIG(X) produces a diagonal matrix D of eigenvalues and a full matrix V whose columns are the corresponding eigenvectors so that X*V = V*D.

solid   Can change plot properties in Figure menu. ylabel(’y=sin(x)’).y).1:pi. y=sin(x).Graphics      Visualization of vector data is available >> x=-pi:0. or via ”handle” >> h=plot(x.’s-’) – s means squre and – means >> xlabel(’x’).y. set(h. >> plot(x. pie(v)   . Many other plot functions available >> v=1:4.y) >> plot(x. 4). ’LineWidth’.

the result will have the same orientation.Graphics Three-dimensional graphics >> A = zeros(32). FFT2 Two-dimensional discrete Fourier Transform. >> A(14:16. >> F=abs(fft2(A)).If X is a vector. >> rotate3d on Several other plot functions available >> surfl(F) Can change lightning and material properties >> cameramenu >> material metal . >> mesh(F) MESH 3-D mesh surface.14:16) = ones(3). FFT2(X) returns the two-dimensional Fourier transform of matrix X.

Graphics     Bitmap images can also be visualized >> load mandrill >> image(X). colormap(map) >> axis image off .

Scripts end with the extension . When you type the name of the script file at the MATLAB prompt the commands in the script file are executed as if you had typed them in from the keyboard.MATLAB Scripts   Scripts are MATLAB commands stored in text files.m Referred to as M-Files .

.For Loops  Common to other programming languages for variable = expression statement .. statement end .

end .While Loops. (taken from help while) while (1+eps) > 1 eps = eps/2. con’t: 2  Ex.

..........case_expr3.statement ..statement case … {case_expr1.} statement.statement end . otherwise statement.....Case statements  Syntax – switch switch_expr case case_expr statement..case_expr2..

Another satisfied MATLAB user! End .