Vous êtes sur la page 1sur 11

MTS-362

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.

Matlab program and script files always have filenames ending with ".m"; the
programming language is exceptionally straightforward since almost every data object is
assumed to be an array. Graphical output is available to supplement numerical results.

1.0
1.1

Entering the matrices and simple matrix manipulation


Matrices

To enter the matrix


12
34
and store it in a variable a, do this:
>> 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.
Control Engineering Lab

1/11

To perform element-wise multiplication rather than matrix multiplication, use the .*


operator:
p = a.*a
The matrix operators for multiplication, division, and power each have a corresponding
array operator that operates element-wise. For example, raise each element of a to the
third power:
a.^3
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 1; 1 1 ]
>> inv(c);
Control Engineering Lab

2/11

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


>> det(a)
>> det(c)
A transpose of a matrix
>> a
Concatenation
Concatenation is the process of joining arrays to make larger ones. In fact, you made your
first array by concatenating its individual elements. The pair of square brackets [] is the
concatenation operator.
A = [a,a]
A=
1 2 3 1 2 3
4 5 6 4 5 6
7 8 10 7 8 10
1.2

Building Matrices

Matlab has many types of matrices which are built into the system. A 7 by 7 matrix with
random entries is produced by typing
rand(7)
You can generate random matrices of other sizes and get help on the rand command within
matlab:
rand(2,5)
help rand
Another special matrix, called a Hilbert matrix, is a standard example in numerical linear
algebra.
hilb(5)
help hilb
A 5 by 5 magic square is given by the next command:
magic(5)
help magic
A magic square is a square matrix which has equal sums along all its rows and columns.
We'll use matrix multiplication to check this property a bit later.
Control Engineering Lab

3/11

Some of the standard matrices from linear algebra are easily produced:
eye(6)
zeros(4,7)
ones(5)
You can also build matrices of your own with any entries that you may want

1.3

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
A
a
x

3x3
1x1
3x1

Bytes Class
72 double array
8 double array
24 double array

To clear the screen:


Clc
To clear the memory:
Clear
Clear (variables)
Colon Notation:
Matlab offers some powerful methods for creating arrays and for taking them apart.
x=-2:1
length(x)
x= -2:.5:1
Control Engineering Lab

4/11

x= -2:.2:1
a=magic(5)
a(2,3)

Now we will use the colon notation to select a column of a.


a(2,:)
a(:,3)
a
a(2:4,:)
a(:,3:5)
a(2:4,3:5)
a(1:2:5,:)
You can put a vector into a row or column position within a.
a(:,[1 2 5])
a([2 5],[2 4 5])
1.4

Complex Numbers and Matrices:

Z=3 +4i

or z=3 +4j

There are at least two convenient ways to enter complex matrices.


A=[1 2;3 4] + i*[5 6;7 8]
And
A=[1+5*i 2+6*i;3+7*i 4+8*i]
1.5

Miscellaneous commands:

Control Engineering Lab

5/11

The MATLAB display only shows 5 digits in the default mode. The fact is that MATLAB
always keeps and computes in a double precision 16 decimal places and rounds the display to
4 digits. The command
format long
will switch to display all 16 digits and
format short
will return to the shorter display. It is also possible to toggle back and forth in the scientific
notation display with the commands
format short e
and
format long e
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, A;. When MATLAB is
ready to proceed, the prompt >> will appear. Try this on a matrix right now.
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

1.6

SOLVING SIMULTANEOUS LINEAR EQUATIONS

One of the most common applications of matrix algebra occursin 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
.
.
.
. .
.
.
.
. .
Control Engineering Lab

6/11

.
.
.
. .
.
.
.
. .
an1x1 + an2x2 + an3x3 + ............... annxn = bn
The matrix format for these equations is
[a][x] = [b]
where
a11 a12 a13 .... a1n

x1

b1

a21 a22 a23 .....a2n

x2

b2

[a] = a31 a32 a33 .... a3n


. . .
.
.
. . .
.
.
. . .
.
.
. . .
.
.
an1 an2 an3 .... ann

[x] = x3
.
.
.
.
xn

[b] = b3

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. Pre-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)
Control Engineering Lab

7/11

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.
We leave to a study of linear algebra, the method and steps required to calculate the
inverse of a matrix. Suffice it to say here that for systems of equations numbering in the
hundreds or thousands, which often occur in complex engineering problems, 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.
x1 - 4x2 + 3x3 = -7
3x1 + x2 - 2x3 = 14
2x1 + x2 + x3 = 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 ]
1.7

LEFT and RIGHT Matrex 'DIVISION' In MATLAB

In contrast to matrix inversion, the preferred methods of solution for large-scale systems
of simultaneous equations are methods of back substitution whereby the equations can be
rearranged so that first xn is solved, then xn-1 and so on until finally x1 is obtained in order.
Thus in back substitution we never solve the equations simultaneously, rather we solve them
sequentially. For example, the above set of equations can be rearranged through appropriate
combination into the below equations. (You should carry out this algebra to ensure you
understand the steps matlab takes in solving with back substitution. In the general case, the
steps or algorithm is much more complex than that required for 3 equations.)
x1 - 4x2 + 3x3 = -7
Control Engineering Lab

8/11

13x2 - 11x3 = 35
(34/13)x3 = (68/13)
In this format, we see from the third equation the solution for x3= 2 and knowing this, the
second equation yields x2 = 1, and knowing both x3 and x2, the first equation gives x1 = 1.
This is an application of back substitution, whereby each unknown is obtained by simple
sequential solution to a single equation. Methods of back substitution are employed by
'matlab' when we invoke the 'right division' and 'left division' commands.
\
/

left division
right division

Understand that matrices cannot be divided. The operation is not defined. The syntax of 'right
division' and 'left division' simply invoke back substitution methods for obtaining a solution
vector from a system of simultaneous equations. Whether 'right division' (/) or 'left division'
(\) is appropriate depend on how the matrix equation is posed.
Left Division. If the matrix equation is
[a][x] = [b]
then we have a nxn matrix [a] pre-multiplying a nx1 matrix [x], resulting in a nx1 matrix [b].
The solution for x is obtained by using the left division operation.
[x] = [a]\[b]
If the matrix equation is cast in this format, the use of right division to obtain the solution
vector x will result in an error message.
Returning to 'matlab', recall we have defined the matrices [a] and [b] in the format of the
matrix equation appropriate for left division.
a = [ 1 -4 3; 3 1 -2; 2 1 1];
b = [ -7; 14; 5];
Now enter the command
x1 = a\b
and obtain the result
x1 = [ 3
1
-2 ]
which is the same result found earlier for x when solving by matrix inversion.
Control Engineering Lab

9/11

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]
Note that in this form [x] and [B] are row vectors rather than column vectors as above. This
form represent a 1xn matrix (x) pre- multiplying a nxn matrix (A), resulting an a 1xn matrix
(B). Again, recalling the set of equations,

x1 - 4x2 + 3x3 = -7
3x1 + x2 - 2x3 = 14
2x1 + x2 + x3 = 5
These equations can be written in matrix format
[x][A] =[B]
if
x = [x1 x2 x3]

B = [ -7 14 5]

and
1 3 2
[A] = -4 1 1
3 -2 1
Note that [A] is equal to the transpose of [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 ]
Control Engineering Lab

10/11

1.8

Exercise:

1. 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.
2. 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.

Control Engineering Lab

11/11

Vous aimerez peut-être aussi