You are on page 1of 7

Control Engineering Lab-1

Introduction to MATLAB its functions and applications:


Matlab is a tool for doing numerical computations with matrices and vectors. It can
also display information graphically. The best way to learn what Matlab can do is to
work through some examples at the computer.
Entering the matrices and simple matrix manipulation
Matrices
To enter the matrix and store it in a variable a,
1 2
3 4
>> a = [1 2; 3 4]
To redisplay the matrix, just type its name:
>> a
Once you know how to enter and display matrices, it is easy to compute with them.
First we will square the matrix a :
>> a * a _______________
_______________

Wasn't that easy? Now we'll try something a little harder. First we define a matrix
b:
>> b = [ 1 2; 0 1 ]
Then we compute the product ab:
>> a*b _______________
_______________

Finally, we compute the product in the other order:


>> b*a _______________
_______________

Notice that the two products are different: matrix multiplication is non
commutative.
Of course, we can also add matrices:
>> a + b _______________
_______________

Now let's store the result of this addition so that we can use it later:
>> s = a + b
Matrices can sometimes be inverted:
>> inv(s) _______________
_______________

To check that this is correct, we compute the product of s and its inverse:
>> s * inv(s) _______________
_______________

The result is the unit, or identity matrix. We can also write the computation as
>> s/s _______________
_______________
We can also write
>> s\s _______________
_______________

which is the same as


>> inv(s) * s
To see that these operations, left and right division, are really different, we do the
following:
>> a/b _______________
_______________

>> a\b _______________


_______________

Not all matrices can be inverted, or used as the denominator in matrix division:
>> c = [1 2; 1 2]
>> inv(c) _______________
_______________

A matrix can be inverted if and only if its determinant is nonzero:


>> det(a) _______________
_______________

>> det(c) _______________


_______________

A transpose of a matrix
>> a’ _______________
_______________

Building Matrices
Matlab has many types of matrices which are built into the system. A 2 by 2 matrix
with random entries is produced by typing
rand(2) _______________
_______________

You can generate random matrices of other sizes and get help on the rand
command within matlab:
rand(2,5) _______________
_______________

A 5 by 5 magic square is given by the next command:


magic(5) _______________
_______________
_______________
_______________
_______________
A magic square is a square matrix which has equal sums along all its rows and
columns.
It is not always necessary for MATLAB to display the results of a command to the
screen. If you do not want the matrix A displayed, put a semicolon (;) after it

Some of the standard matrices from linear algebra are easily produced:
eye(6) is an identity matrix of 6 rows and 6 columns.
zeros(4, 7) will return a matrix having all entries equals to zero.
ones(5) will return a matrix having all entries equals to one.

You can also build matrices of your own with any entries that you may want
You may have discovered by now that MATLAB is case sensitive, that is "a" is not
the same as "A." If this proves to be an annoyance, the command
casesen
will toggle the case sensitivity off and on.

Sometimes you will have spent much time creating matrices in the course of your
MATLAB session and you would like to use these same matrices in your next
session. You can save these values in a file by typing
save filename
This creates a file
filename.mat

Getting Workspace information


At any time you want to know the active variables you can use who:
>> who
To get the complete information we will write
whos
>> whos
Name Size Bytes Class
A 3x3 72 double array
a 1x1 8 double array
x 3x1 24 double array

what
MATLAB replies with the current directory and MATLAB files in the directory

To clear the screen:


>> clc
To clear the memory:
>>clear

Colon Notation:
Now we will use the colon notation to select a column of a.
To select rows or columns of a matrix, use
a= 12 5
-2 13
>> d=a(1,:); d=[12 5];
>>e=a(:,2); e=[5;13];
>>a(2,:)=[3 6]; replaces second row of a
>>a(:,[1 2])
>>a([2 5],[2 4])

Complex Numbers and Matrices:


Here are MATLAB commands to treat complex variables:
Z = complex ( a , b)
creates a complex output, Z, from the two real inputs
ZC = conj (Z)
returns the complex conjugate of the elements of Z
X = real (Z)
returns the real part of the elements of the complex array Z
Y=imag (Z)
returns the imaginary part of the elements of array Z
R= abs (Z)
returns the complex modulus (magnitude), which is the same as
R= s q r t ( real ( Z ) .A2 + imag(Z) .A2)
theta = angle(Z)
returns the phase angles, in radians, for each element of complex array Z
The angles lie between the "real axis" in the s-plane and the magnitude R
Z = R . * e x p ( i * t h e t a)
converts back to the original complex Z
>> Z = complex(3,2)
Z=
3.0000 + 2.00001
>> ZC = conj (Z)
ZC =
3.0000 - 2.0000i
>> R = abs(Z)
R=
3.6056
>> theta= angle(Z)
theta =
0.5880
>> ZRT = R.*exp(i*theta)
ZRT =
3.0000 + 2.00001
SOLVING SIMULTANEOUS LINEAR EQUATIONS
One of the most common applications of matrix algebra occurs in the solution of
linear simultaneous equations.
Consider a set of n equations for which the unknowns are x1, x2, ....... xn.
a11x1 + a12x2 + a13x3 + ............... a1nxn = b1
a21x1 + a22x2 + a23x3 + ............... a2nxn = b2
a31x1 + a32x2 + a33x3 + ............... a3nxn = b3
.....
.....
.....
.....
an1x1 + an2x2 + an3x3 + ............... ann xn = bn
The matrix format for these equations is
[a][x] = [b]
Where
[a] = a11 a12 a13 ....a1n [x] = x1 and [b] = b1
a21 a22 a23 ....a2n x2 b2
a31 a32 a33 .... a3n x3 b3
...... . .
...... . .
...... . .
...... . .
an1 an2 an3.... ann xn bn
Note that only when the equations are linear in the unknown xi's is the matrix
formulation possible. The classical matrix solution to this equation is based on the
definition of the inverse of a matrix. The inverse of a matrix is that matrix which
when multiplied by the original matrix, results in the identity matrix. Then
a-1 *a = I
where a-1 denotes the 'inverse of matrix a' and I denotes the identity matrix of the
same order as matrix 'a'. If 'a' is a known coefficient matrix and if 'b' is a column
vector of known terms, the problem is one of finding the n vales of x1, x2, .... xn
that satisfy these simultaneous equations.
multiplying both sides of the equation by the inverse of 'a' gives
[a-1][a][x] = [a-1][b] or
[x] = [a-1][b]
Thus if we find the inverse of the coefficient matrix, the product of the inverse
times the matrix of non homogeneous terms, b, yields the unknown matrix, x. To
observe the simple property of the inverse, define the 4x4 matrix, C
C = [1 -4 3 2; 3 1 -2 1; 2 1 1 -1; 2 -1 3 1]
calculate the inverse using the 'matlab' command, inv().
C_inverse = inv(C) and note that
C_inverse * C = [1 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1]
Where the right hand side is the 4x4 identity matrix.
The calculation of the matrix inverse is a time consuming operation even for
modern computers. As an example of solving a system of equations using the
matrix inverse, consider the following system of three equations.
1x1 - 4x2 + 3x3 = -7
3x1 + x2 - 2x3 = 14
2x1 + x2 +1x3 = 5
Using 'matlab', define the two matrices for [a] and [b].
a = [ 1 -4 3; 3 1 -2; 2 1 1];
b = [ -7; 14; 5];
Find the solution vector, x, by
x = inv(a)*b giving
x = [ 3 1 -2 ]

Left Division:
if an equation is of the form a*x=b with a as a square matrix, x=a\b is the same as
x=inv(a)*b
i-e [x] = [a]\[b] is same as [x] = [a-1][b]
For example
a = [ 1 -4 3; 3 1 -2; 2 1 1];
b = [ -7; 14; 5];
Now enter the command
x = a\b
result: x1 = [ 3 1 -2 ]
Which i s the same result found earlier for x when solving by matrix inversion.
Right Division:
The matrix format in which right division is employed is less common but no less
successful in solving the problem of simultaneous equations. Right Division is
invoked when the equations are written in the form
[x][a] = [b]
x1 - 4x2 + 3x3 = -7
3x1 + x2 - 2x3 = 14
2x1 + x2 +1x3 = 5
These equations can be written in matrix format
[x][a] =[b]
if
x = [x1 x2 x3] B = [ -7 14 5]
And
132
[a] = -4 1 1
3 -2 1
Note that [a] is equal to the transpose of previously used [a].
A = a'
Having so defined A and B, the solution for x can be obtained by right division.
x = b/a
results in
x = [ 3 1 -2 ]

LAB#01 EXCERCISE
TASK 01. Find the solution to
r+s+t+w=4
2r - s + w = 2
3r + s - t - w = 2
r - 2s - 3t + w = -3
Using the matrix inverse and left and right division. write down the matlab code
here:
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
______________

TASK 02. Find the solution to


2x1 + x2 - 4x3 + 6x4 + 3x5 - 2x6 = 16
-x1 + 2x2 + 3x3 + 5x4 - 2x5 = -7
x1 - 2x2 - 5x3 + 3x4 + 2x5 + x6 = 1
4x1 + 3x2 - 2x3 + 2x4 + x6 = -1
3x1 + x2 - x3 + 4x4 + 3x5 + 6x6 = -11
5x1 + 2x2 - 2x3 + 3x4 + x5 + x6 = 5
Using the matrix inverse and left and right division.
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
_____________

You might also like