Вы находитесь на странице: 1из 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 1970s

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

History of MATLAB, cont: 2

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

MathWorks Product Overview

Distributed Computing @ matlab

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, C++, and Fortran.

Simulink- 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, and can be extended for specialized applications.

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

Bioinformatics Toolbox 2.2


Read, analyze, and visualize genomic, proteomic, 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

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

Strengths of MATLAB

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

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, such as C, C++, Fortran, Java, COM, and Microsoft Excel

Weaknesses of MATLAB

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

The Latest Update


The latest version of MATLAB is 7.1. Release 14 with Service Pack 3 (R14SP3), launched on September 1, 2005, provides updates to MATLAB, Simulink, and 75 other products.

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

References

http://www.mathworks.com/products/matlab/i ndex.html http://www.ccs.neu.edu/home/wmason/ Mastering MATLAB 7. Duane C. Hanselman, Bruce L. Littlefield. Prentice Hall, 2004. MATLAB: An Introduction with Applications. Amos Gilat. Wiley, 2003.

Components of MATLAB Interface


Workspace Current Directory Command History Command Window

Interactive Calculations

Matlab is interactive, no need to declare variables >> 2+3*4/2 >> a=5e-2; b=1; 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 http://www.mathworks.com/access/helpdesk/help/techdoc/

Vectors and Matrices


Vectors (arrays) are defined as >> v = [1, 2, 4, 5] >> w = [1; 2; 4; 5]

Matrices (2D arrays) defined similarly >> A = [1,2,3;4,-5,6;5,-6,7]

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

Indexing Matrices
Indexing

using parentheses >> A(2,3)


Index

submatrices using vectors of row and column indices >> A([2 3],[1 2])
Ordering

of indices is important! >> B=[A(3,2),A(3,1);A(2,2), A(2,1)] >> B=A([3 2],[2 1])

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

Matrix Functions

Many elementary matrices predefined >> help elmat; >> I=eye(3)

Elementary functions are often overloaded >> help elmat >> sin(A)
Specialized matrix functions and operators >> As=sqrtm(A) >> As^2 >> A.*A Note: in general, .<operator> is elementwise operation

Numerical Linear Algebra


Basic numerical linear algebra >> z=[1;2;3]; 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,D]=eig(A) [V,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.

Graphics

Visualization of vector data is available >> x=-pi:0.1:pi; y=sin(x); >> plot(x,y) >> plot(x,y,s-) s means squre and means >> xlabel(x); ylabel(y=sin(x));

solid

Can change plot properties in Figure menu, or via handle >> h=plot(x,y); set(h, LineWidth, 4); Many other plot functions available >> v=1:4; pie(v)

Graphics
Three-dimensional

graphics >> A = zeros(32); >> A(14:16,14:16) = ones(3); >> F=abs(fft2(A));


FFT2 Two-dimensional discrete Fourier Transform. FFT2(X) returns the two-dimensional Fourier transform of matrix X.If X is a vector, the result will have the same orientation.

>>

mesh(F) MESH 3-D mesh surface. >> rotate3d on


Several

other plot functions available >> surfl(F)


Can

change lightning and material properties >> cameramenu >> material metal

Graphics

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

MATLAB Scripts

Scripts are MATLAB commands stored in text files. 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. Scripts end with the extension .m Referred to as M-Files

For Loops

Common to other programming languages

for variable = expression statement ... statement end

While Loops, cont: 2

Ex. (taken from help while)

while (1+eps) > 1 eps = eps/2; end

Case statements

Syntax

switch switch_expr case case_expr statement,...,statement case {case_expr1,case_expr2,case_expr3,...} statement,...,statement ... otherwise statement,...,statement end

Another satisfied MATLAB user!

End

Вам также может понравиться