MA TLAB: A Short Tutorial on Basic Commands for Linear Algebra, Study notes of Electrical and Electronics Engineering

This tutorial provides an overview of fundamental commands in ma tlab for creating, changing, and operating with vectors and matrices in the context of linear algebra. Topics include creating vectors and matrices, changing their entries, and performing matrix multiplication and inversion.

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-ls6-1
koofers-user-ls6-1 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MATLAB Tutorial
You need a small number of basic commands to start using MATLAB. This short tutorial
describes those fundamental commands. You need to
create
vectors and matrices, to
change
them, and to
operate
with them. Those are all short high-level commands, because MATLAB
constantly works with matrices. I b elievethatyou will likethe power that this software gives,
to do linear algebra by a series of short instructions:
create
E
create
u
change
E
multiply
Eu
E
= eye(3)
u
=
E
(:
1)
E
(3
1) = 5
v
=
E
u
2
4
1 0 0
0 1 0
0 0 1
3
5
2
4
1
0
0
3
5
2
4
1 0 0
0 1 0
5 0 1
3
5
2
4
1
0
5
3
5
The word eye stands for the identity matrix. The submatrix
u
=
E
(:
1) picks out column 1.
The instruction
E
(3
1) = 5 resets the (3
1) entry to 5. The command
E
u
multiplies the
matrices
E
and
u
.All these commands are repeated in our list below. Here is an example
of inverting a matrix and solving a linear system:
create
A
create
b
invert
A
solve
Ax
=
b
A
= ones(3) +eye(3)
b
=
A
(:
3)
C
=inv(
A
)
x
=
A
n
b
or
x
=
C
b
2
4
211
121
112
3
5
2
4
1
1
2
3
5
2
4
:
75
;
:
25
;
:
25
;
:
25
:
75
;
:
25
;
:
25
;
:
25
:
75
3
5
2
4
0
0
1
3
5
The matrix of all ones was added to eye(3), and
b
is its third column. Then inv(
A
) pro duces
the inverse matrix (normally in decimals for fractions use
format rat
). The system
Ax
=
b
is solved by
x
= inv(
A
)
b
,whichis the slow way. The backslash command
x
=
A
n
b
uses
Gaussian elimination if
A
is square and never computes the inverse matrix. When the right
side
b
equals the third column of
A
, the solution
x
mustbe0 0 1]
0
. (
The transpose symbol
0
makes
x
acolumn vector
.) Then
A
x
picks out the third column of
A
,andwehave
Ax
=
b
.
Here are afewcomments. The commentsymbol is %:
% The symbols
a
and
A
are
dierent
: MATLAB is case-sensitive.
% Type
help slash
for a description of how to use the backslash symbol. The word
help
can be followed by a MATLAB symbol or command name or M-le name.
1
pf3
pf4
pf5

Partial preview of the text

Download MA TLAB: A Short Tutorial on Basic Commands for Linear Algebra and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

MATLAB Tutorial

You need a small numb er of basic commands to start using MATLAB This short tutorial describ es those fundamental commands You need to create vectors and matrices to change them and to operate with them Those are all short highlevel commands b ecause MATLAB constantly works with matrices I b elieve that you will like the p ower that this software gives to do linear algebra by a series of short instructions

create E create u change E multiply E u E  eye  u  E   E    v  E u

The word eye stands for the identity matrix The submatrix u  E   picks out column  The instruction E    resets the   entry to The command E u multiplies the matrices E and u All these commands are rep eated in our list b elow Here is an example of inverting a matrix and solving a linear system

create A create b invert A solve Ax  b A  ones eye b  A  C  inv A x  Anb or

^ x^ ^ C^ ^ b

The matrix of all ones was added to eye and b is its third column Then invA pro duces the inverse matrix normally in decimals for fractions use format rat  The system Ax  b is solved by x  inv A b which is the slow way The backslash command x  Anb uses Gaussian elimination if A is square and never computes the inverse matrix When the right side b equals the third column of A the solution x must b e   The transpose symbol  makes x a column vector Then A x picks out the third column of A and we have Ax  b Here are a few comments The comment symb ol is 

 The symb ols a and A are dierent  MATLAB is casesensitive

 Typ e help slash for a description of how to use the backslash symb ol The word help can b e followed by a MATLAB symb ol or command name or Mle name

Note The command name is upp er case in the description given by help but must b e lower case in actual use And the backslash Anb is dierent when A is not square

 To display all  digits typ e format long The normal format short gives  digits after the decimal

 A semicolon after a command avoids display of the result A  ones will not display the    identity matrix

 Use the uparrow cursor to return to previous commands

How to input a row or column vector

u     has one row with three comp onents a    matrix

v      has three rows separated by semicolons a    matrix

v     or v  u transposes u to pro duce the same v

w   generates the row vector w      with unit steps

u    takes steps of to give u    

How to input a matrix a row at a time

A      has two rows always a semicolon b etween rows

A    also pro duces the matrix A but is harder to typ e  

B      is the transpose of A Thus AT^ is A in MATLAB

How to create sp ecial matrices

diagv  pro duces the diagonal matrix with vector v on its diagonal

to eplitzv  gives the symmetric constantdiagonal matrix with v as rst row and rst col umn

to eplitzw v  gives the constantdiagonal matrix with w as rst column and v as rst row

onesn gives an n  n matrix of ones

Numb ers and matrices asso ciated with A

detA is the determinant if A is a square matrix

rankA is the rank numb er of pivots  dimension of row space and of column space

sizeA is the pair of numb ers m n

traceA is the trace  sum of diagonal entries  sum of eigenvalues

nullA is a matrix whose n  r columns are an orthogonal basis for the nullspace of A

orthA is a matrix whose r columns are an orthogonal basis for the column space of A

Examples

E  eye  E     creates a    elementary elimination matrix E A subtracts  times row  of A from row B  A b creates the augmented matrix with b as extra column E  eye  P  E     creates a p ermutation matrix Note that triuA trilA  diagdiagA equals A

Builtin Mles for matrix factorizations all imp ortant

L U P   luA gives three matrices with P A  LU

e  eig A is a vector containing the eigenvalues of A

S E   eig A gives a diagonal eigenvalue matrix E and eigenvector matrix S with AS  S E If A is not diagonalizable to o few eigenvectors then S is not invertible

Q R   qrA gives an m  m orthogonal matrix Q and m  n triangular R with A  QR

Creating Mles

Mles are text les ending with m which MATLAB uses for functions and scripts A script is a sequence of commands which may b e executed often and can b e placed in an mle so the commands do not have to b e retyp ed MATLABs demos are examples of these scripts An example is the demo called house Most of MATLABs functions are actually mles and can b e viewed by writing type xxx where xxx is the name of the function

To write your own scripts or functions you have to create a new text le with any name you like provided it ends with m so MATLAB will recognize it Text les can b e created edited and saved with any text editor like emacs EZ or vi A script le is simply a list of MATLAB commands When the le name is typ ed at the MATLAB prompt the contents of the le will b e executed For an mle to b e a function it must start with the word function followed by the output variables in brackets the function name and the input variables

Examples

function CmultA rrankA C  A A

Save the ab ove commands into a text le named multm Then this funtion will take a matrix A and return only the matrix pro duct C The variable r is not returned b ecause it was not included as an output variable The commands are followed by  so that they will not b e printed to the MATLAB window every time they are executed It is useful when dealing with large matrices Here is another example

function VDrpropertiesA This function nds the rank eigenvalues and eigenvectors of A mnsizeA if mn VDeigA rrankA else disp Error The matrix must be square  end

Here the function takes the matrix A as input and only returns two matrices and the rank as output The is used as a comment The function checks to see if the input ma trix is square and then nds the rank eigenvalues and eigenvectors of a matrix A Typing propertiesA only returns the rst output V the matrix of eigenvectors You must typ e VDrpropertiesA to get all three outputs

axis a b c d will scale the graph to lie in the rectangle a  x  b c  y  d To title the graph or lab el the xaxis or the y axis put the desired lab el in quotes as in these examples

title height of satellite xlabel time in seconds y label height in meters

The command hold keeps the current graph as you plot a new graph Rep eating hold will clear the screen To print or save the graphics window in a le see help print or use print Pprintername print d lename