Lecture 1 Basic Matlab
-
Upload
vinoth-kumar -
Category
Documents
-
view
237 -
download
0
Transcript of Lecture 1 Basic Matlab
-
8/8/2019 Lecture 1 Basic Matlab
1/17
MATH 3795
Lecture 1. Basics of MATLAB
Dmitriy Leykekhman
Fall 2008
Topics Sources.
Entering Matrices. Basic Operations with Matrices. Build in Matrices. Build in Scalar and Matrix Functions. if, while, for m-files Graphics.
Sparse Matrices.D. Leykekhman - MATH 3795 Computational Mathematics Introduction to Matlab 1
-
8/8/2019 Lecture 1 Basic Matlab
2/17
Resources.
There are many good books on Matlab besides the textbook. RecentCleve Molers book Experiments with MATLAB has many fun problemsand is also available online.
Good introduction is Matlab primer
The MATHWORKS has a lot of useful information
D. Leykekhman - MATH 3795 Computational Mathematics Introduction to Matlab 2
http://www.mathworks.com/moler/exm/index.htmlhttp://www.math.toronto.edu/mpugh/primer.pdfhttp://www.mathworks.com/http://www.mathworks.com/http://www.math.toronto.edu/mpugh/primer.pdfhttp://www.mathworks.com/moler/exm/index.html -
8/8/2019 Lecture 1 Basic Matlab
3/17
Resources.
There are many good books on Matlab besides the textbook. RecentCleve Molers book Experiments with MATLAB has many fun problemsand is also available online.
Good introduction is Matlab primer
The MATHWORKS has a lot of useful information
In addition, books by Andrew Knight, BASICS OF MATLAB and Beyondand
by D.J. Nigham and N.J. Nigham MATLAB Guide
are pretty useful.
D. Leykekhman - MATH 3795 Computational Mathematics Introduction to Matlab 2
http://www.mathworks.com/moler/exm/index.htmlhttp://www.math.toronto.edu/mpugh/primer.pdfhttp://www.mathworks.com/http://www.mathworks.com/http://www.math.toronto.edu/mpugh/primer.pdfhttp://www.mathworks.com/moler/exm/index.html -
8/8/2019 Lecture 1 Basic Matlab
4/17
Entering Matrices.
Matlab is standing for matrix laboratory and was basically designed foroperations with matrices. We will essentially only work with matrices. Ascalar is 1-by-1 matrix.
D. Leykekhman - MATH 3795 Computational Mathematics Introduction to Matlab 3
-
8/8/2019 Lecture 1 Basic Matlab
5/17
Entering Matrices.
Matlab is standing for matrix laboratory and was basically designed foroperations with matrices. We will essentially only work with matrices. Ascalar is 1-by-1 matrix.
Matrices can be introduced into MATLAB in several different ways:
Entered by an explicit list of elements
Generated by built-in statements and functions
Loaded from external data files or applications
D. Leykekhman - MATH 3795 Computational Mathematics Introduction to Matlab 3
-
8/8/2019 Lecture 1 Basic Matlab
6/17
Basic Matrix Operations.
The following are the basic matrix operations in MATLAB:
+ addition
subtraction
multiplication power
conjugate transpose
/ left division
\ right division
D. Leykekhman - MATH 3795 Computational Mathematics Introduction to Matlab 4
-
8/8/2019 Lecture 1 Basic Matlab
7/17
Matrix Building Functions.
Matrices can also be build from the functions:
eye identity matrix
zeros matrix of zeros
ones matrix of ones
diag create or extract diagonals triu upper triangular part of a matrix
tril lower triangular part of a matrix
rand randomly generated matrix
hilb Hilbert matrix magic magic square
D. Leykekhman - MATH 3795 Computational Mathematics Introduction to Matlab 5
-
8/8/2019 Lecture 1 Basic Matlab
8/17
for
General syntax for for loop
for variable = expression,statement,
...,statement
end
Examples.
D. Leykekhman - MATH 3795 Computational Mathematics Introduction to Matlab 6
-
8/8/2019 Lecture 1 Basic Matlab
9/17
for
General syntax for for loop
for variable = expression,statement,
...,statement
end
Examples.
As a general rule try to avoid such loops.
D. Leykekhman - MATH 3795 Computational Mathematics Introduction to Matlab 6
-
8/8/2019 Lecture 1 Basic Matlab
10/17
while
General syntax for while loop
while expression
statement
...statement
end
Examples.
D. Leykekhman - MATH 3795 Computational Mathematics Introduction to Matlab 7
-
8/8/2019 Lecture 1 Basic Matlab
11/17
if
General syntax for if statement
if expression,statements,
elseif expressionstatement
else expression
statement
end
Examples.
D. Leykekhman - MATH 3795 Computational Mathematics Introduction to Matlab 8
-
8/8/2019 Lecture 1 Basic Matlab
12/17
Relations
< less than
> greater than
= greater than or equal
== equal
= not equal.
& and
| or
not
D. Leykekhman - MATH 3795 Computational Mathematics Introduction to Matlab 9
-
8/8/2019 Lecture 1 Basic Matlab
13/17
Scalar functions.Certain MATLAB functions operate essentially on scalars, but operateelement-wise when applied to a matrix.
sin asin exp
abs round
cos acos log (natural log)
sqrt
floor
tan
atan rem (remainder)
sign
ceilD. Leykekhman - MATH 3795 Computational Mathematics Introduction to Matlab 10
-
8/8/2019 Lecture 1 Basic Matlab
14/17
Vector functions.
Certain MATLAB functions operate essentially on scalars, but operateelement-wise when applied to a matrix.Other MATLAB functions operate
essentially on a vector (row or column), but act on an m-by-n matrix(m 2) in a column-by-column fashion to produce a row vectorcontaining the results of their application to each column.
max
min
sum
prod
median mean
all
any
D. Leykekhman - MATH 3795 Computational Mathematics Introduction to Matlab 11
-
8/8/2019 Lecture 1 Basic Matlab
15/17
Matrix functions.Much of MATLABs power comes from its matrix functions.
eig eigenvalues and eigenvectors
chol cholesky factorization svd singular value decomposition inv inverse lu LU factorization qr QR factorization
hess hessenberg form schur schur decomposition rref reduced row echelon form expm matrix exponential sqrtm matrix square root
poly characteristic polynomial det determinant size size norm 1-norm, 2-norm, F-norm, 1-norm cond condition number in the 2-norm
rank rankD. Leykekhman - MATH 3795 Computational Mathematics Introduction to Matlab 12
-
8/8/2019 Lecture 1 Basic Matlab
16/17
Graphics functions.
MATLAB can produce planar plots of curves, 3-D plots of curves, 3-Dmesh surface plots, and 3-D faceted surface plots. The primarycommands for these facilities are:
plot
polar plot3
mesh
surf
There are others.
D. Leykekhman - MATH 3795 Computational Mathematics Introduction to Matlab 13
-
8/8/2019 Lecture 1 Basic Matlab
17/17
Sparse Matrices.In many applications, like solving systems of PDEs, most elements of theresulting matrices are zeros (sparse). MATLAB has ability to store andmanipulate sparse matrices. For almost any matrix function there is acorresponding sparse matrix function. Most useful are:
speye sparse identity matrix
sparse create sparse matrix; convert full matrix to sparse
spones replace nonzero entries with ones
spdiags sparse matrix formed from diagonals sprandn sparse random matrix
full convert sparse matrix to full matrix
spy
nnz number of nonzero entries nzmax amount of storage allocated for nonzero entries
issparse true if matrix is sparse
spalloc allocate memory for nonzero entries
There are other commands (cf. Primer.).D. Leykekhman - MATH 3795 Computational Mathematics Introduction to Matlab 14