0% found this document useful (0 votes)
34 views

Matrix Operations In: Matlab

MATLABgui

Uploaded by

Ahmad_Othman3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Matrix Operations In: Matlab

MATLABgui

Uploaded by

Ahmad_Othman3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

Matrix operations in MATLAB

Matrix addition and subtraction


A matrix is to be added and subtracted
from another matrix if the matrices
have the same size (same number of
columns and rows). The same rules
apply to vectors.
Example
1 0 4  7 3 2 
 
Given a  3 1 3 and b  2 3 5
4 0 3 8 2 6
Determine x=a+b and y=a-b
On the command window we write:
>> a=[1 , 0 , 4 ; 3 , 1 , 3 ; 4 , 0 , 3] ;
>> b=[7 , 3 , 2 ; 2 , 3 , 3 ; 8 , 2 , 6] ;
>> x=a+b ;
>> y=a-b ;
>> % type matrix a
>> a
a=
1 0 4
3 1 3
4 0 3
>> % type matrix b
>> b
b=
7 3 2
2 3 5
8 2 6
>> % type matrix x=a+b
>> x
x=
8 3 6
5 4 8
12 2 9
>> % type matrix y=a-b
>> y
y=
6 3 2
1 2 2
4 2 3

Matrix multiplication

When the number of columns in one matrix


and the number of rows of another matrix are
identical, they can be multiplied.
Example
1 4
  1 0 4
Given a  3 3 and b   
3 1 3
4 3 
Determine x=a b and y=b a

On the command window:


>> a=[1 , 4 ; 3 , 3 ; 4 , 3] ;
>> b=[1 , 0 , 4 ; 3 , 1 , 3] ;
>> x=a*b ;
>> y=b*a ;
>> a
a=
1 4
3 3
4 3

>> b
b= 1 0 4
3 1 3
>> x=a*b
x= 13 4 16
12 3 21
13 3 25
>> y
y=
17 16
18 24

Transpose of a matrix
A matrix a may be transposed at in MATLAB
by placing a prime after the matrix.
e=a’
(Matrix e becomes the transpose of a
that is e=at )
Example
>> a=[1 , 4 ; 3 , 3 ; 4 , 3] ;
>> a
a=
1 4
3 3
4 3
>> e=a’ ;
>> e
e=
1 3 4
4 3 3
An identity matrix m x m is generated by:
>> % say for 3 x 3 matrix
>> s=eye(3)
s=
1 0 0
0 1 0
0 0 1
A null matrix m x m is generated by:
>> % say for 3 x 3 matrix
>> s=zeros(3)
s=
0 0 0
0 0 0
0 0 0
A null matrix m x n is generated by:
>> % say for 3 x 2 matrix
>> s=zeros(3 , 2)
s=
0 0
0 0
0 0
An m x n matrix consisting of unity only is:
>> % say for 3 x 2 matrix
>> s=ones(3 , 2)
s=
1 1
1 1
1 1
Inverse matrix
When two square matrices a and b satisfy
a b=I or b a=I where I is the identity matrix,
then a is the inverse of b , and b is the
inverse of a , that is a a-1 =I and a-1a =I

Example

1 2
a  Find the inverse matrix a-1
3 4
>> a=[1, 2 ; 3 , 4] ;
>> b=inv(a) ;
>> a
a=
1 2
3 4

>> b
b=
2 1
1.5  0.5
>> I=a*b
I=
1 0
0 1
>> I=b*a
I= Note
1 0
0 1
The inv command will compute the inverse of
any square matrix except when it is singular.
A singular matrix is such that at least one row
(or column) can be expressed by subtracting
or adding other rows (or columns).
Consider for example, the following matrix:
>> a=[2 , 2 , 2 ; 1 , 1 , 1 ; 1 , 1 ,1] ;
>> a
a=
2 2 2
1 1 1
1 1 1
>> b=inv(a) Matrix is
singular to
warning
working
b= inf inf inf precision
inf inf inf
inf inf inf
Application to solution of linear
algebraic equations
Consider a set of n linear algebraic equations
with n unknowns, which is the most usual
case where a unique solution exists and
under-determined and over-determined
systems are avoided.

The equations may be written as:


a11 x1+a12 x2+a13 x3+………………+a1n xn = b1
a21 x1+a22 x2+a23 x3+………………+a2n xn = b2
……………………...……………………….….
………………………………………………….
an1 x1+an2 x2+an3 x3+………………+ann xn = bn

The equations above may be expressed in


matrix form as:

ax=b
 a 11 a 12 .. ... a 1n 
a a 22 
... ... a 2 n 
where a  21
 ... ... ... ... ... 
 
a n1 a n2 ... ... a nn 

 x1   b1 
x  b 
x  2
b  2
 ...   ... 
   
x n  b n 
The solution to the equations may be written as,
x=a-1 b xt=bt (at)-1
To find the solution by MATLAB we write,
x=inv(a) * b
More efficient computationally
or x = a\b
than the former command
z=b’ * inv(a’) where z=x’

or z=b’ / a’
Example
Find the solution of the equations:
x1+2x2=1
3x1+4x2=2

Solution
On the command window we write,
>> a=[1 ,2 ; 3 , 4] ; b=[1 , 2] ’ ;
>> a
>> a=
1 2
3 4
>> b
b=
1
2
>> x=inv(a)*b
x=
0
0.5
>> x=a\b
x=
0
0.5
>> z=b’ * inv(a’)
z=
0 0.5
>> z=b’ / a’
Which is the same answer in a
z=
row vector form.The answer may
0 0.5
be converted to a column vector
form as shown below
>> x=z’
x=
0
0.5
The solution will be x1=0 and x2=0.5
determinants
The determinant of a square matrix may be
expressed concisely by det(a)
or |a| which is equivalent to:
|a|= a11 a12 …………. a1n
a21 a22 …………. a2n
… … …………. …
an1 an2 …………. ann
Where |a| is the determinant of the n x n
Matrix a
To compute the determinant in MATLAB
we use det(a) where a is a square matrix
Example
The determinant of a square matrix may be
expressed concisely by det(a) 1 2 3
Compute the determinant |a|= 4 5 6
Solution 7 8 2
>> a=[1 , 2 , 3 ; 4 , 5 , 6 ; 7 , 8 , 2]
a=
1 2 3
4 5 6
7 8 2
>> d=det(a)
d=
21
MATLAB commands for polynomials
A polynomial y of order n and coefficients
cis may be expressed as:
y=c1 xn+c2 xn-1+c3 xn-2+……….+cn x+cn+1
The polynomial y=0 has n roots , some of
which may be multiple or complex.
In MATLAB, a polynomial of order n is
represented by a row vector containing the
coefficients of powers in descending order.
p=[ c1 c2 c3 …………… cn cn+1 ]
The roots of a polynomial are found by the
roots command and are given in a column
form. r = roots(p)
Example
Find the roots of the polynomial:
y=2x3+x2+4x+5=0
Solution
>> p=[2 1 4 5] ;
>> p
p=
2 1 4 5
>> r=roots(p)
r=
0.2500+1.5612 i
0.2500 -1.5612 I
-1.0000
The product of two polynomials p1 and p2 may
be found by the command conv(p1,p2).
The derivative of a polynomial p may be
obtained by the command polyder(p).
Example
Find the roots of the polynomial p resulting
from the product of the two polynomials
p1=x4-26x2+25 and p2=x3+2x2-9x-18
>> p1=[1 0 -26 0 25]
p1=
1 0 -26 0 25
>> p2=[1 2 -9 -18]
p1=
1 2 -9 -18
>> p=conv(p1,p2)
p=
1 2 -35 -70 259 518 -225 -450
The resulting polynomial is:
p=x7+2x6-35x5-70x4+259x3+518x2-225x-450
>> roots(p)
ans=
-5.0000
5.0000
-3.0000
3.0000
-2.0000
-1.0000
1.0000
Example
Consider the polynomial y=3x4-7x3+2x2+x+1
a) Obtain the first derivative yd of the
polynomial
b) Obtain the value of y and yd when x=1
>> y=[3 -7 2 1 1]
y=
3 -7 2 1 1
>> yd=polyder(y)
yd=
12 -21 4 1
>> yi=polyval(y,1)
yi=
0
>> ydi=polyval(yd,1)
ydi=
-4

You might also like