Networks I

Course No. 0909-201-01

Fall 1997, 2nd Quarter

Introduction to Matlab

 What is Matlab?


A Matlab Tutorial

Getting Started

Double click on the Matlab icon to start the program (duh....). A MATLAB Command Window should pop-up with the following text:

  To get started, type one of these commands: helpwin, helpdesk, or demo.

The ">>" is a prompt, requiring you to type in commands. One of the first commands you should type in is to find out what your working directory is. The working directory is where you will save the results of your calculations. So, type in
>> pwd
pwd stands for "print working directory". You probably would get an output like:

ans =


Matlab always stores the result of its last calculation in a variable called ans (short for answer). Now this indicates the working directory. You can change the working directory using the cd (short for change directory) command. Do
>> cd C:\temp
>> pwd

ans =


Now, the results of your computations will be saved in the C:\temp directory, when you issue the correct commands (more about that later).

Online help can be accessed for all Matlab commands by issuing the help command.
>> help <type name of command here>

To get started, you can simply type
>> help

(Back to top)

Vectors and Matrices

 Variables in Matlab are just like variables in any other programming language (C, C++ etc.); only difference is that you do not have to define them by indicating the type etc. Also, variable names (case sensitive) can be used to refer to a single number (a scalar), a set of numbers (a vector) or an array of numbers (a matrix).
Vectors are nothing but matrices having a single row (a row vector), or a single column (a column vector).
To create a row vector in Matlab, do:
>> r = [1 2 3 4]

r =
        1     2     3     4

A column vector can be created by
>> c = [1; 2; 3; 4]
c =

 On the other hand, you can use the ' operator (transpose)
>> c = r'
c =


Vectors can also be created by incrementing a starting value with a constant quantity. For example,
>> r = [0:2:10]
r =

     0     2     4     6     8    10
creates a row vector, with the first element = 0; each element inceremented by 2; until the final value of 10.

You can index specific parts of a vector. For example, to get the third element in the vector r, you can do
>> r(3)

ans =


Matrices are 2 dimensional quantities and are created similar to vectors. We can do
 >> a = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
a =

     1     2     3
     4     5     6
     7     8     9
    10    11    12
which is a 4x3 matrix (4 rows and 3 columns). We can also use the incremenation principle to do
>> b = [0:2:10; 1:2:11]
b =

     0     2     4     6     8    10
     1     3     5     7     9    11
which is a 2x6 matrix. Again, individual elements of the matrix, for instance the element in the 2nd row, 5th column can be accessed using the notation:
>> b(2, 5)
ans =


(Back to top)

Vector and Matrix Operations

The basic arithmetic operations +, -, *, / can be used for vectors and matrices. These would generate corresponding output vectors or matrices. For example, to add two vectors:
>> a = [1 2 3 4];
>> b = [5 6 7 8];
>> c = a+b
c =
        6 8 19 12

The semicolons (;) in the first two commands direct Matlab not to echo the values of the variables a and b on to the screen immediately after you type them. Obviously, only vectors that have the same number of elements can be added or subtracted. Similarly, two matrices with identical number of rows and columns can be subtracted as follows:
>> a = [1:3:20; 21:3:40];
>> b = [2:3:20; 22:3:40];
>> c = a-b
c =

    -1    -1    -1    -1    -1    -1    -1
    -1    -1    -1    -1    -1    -1    -1

Matrix multiplication using the * symbol is possible only if the number of columns in the first matrix equals the number of rows in the second:
>> a=[1 2 3; 4 5 6]
a =

     1     2     3
     4     5     6

>> b = a'
b =

     1     4
     2     5
     3     6

>> c=a*b

c =

    14    32
    32    77

However, if you want to multiply corresponding elements of two matrices, then you do an array multiply using the .* symbol.
>> a = [1 2 3 4; 5 6 7 8]; b=[2 2 2 2; 3 3 3 3];
>> c=a .* b
c =

     2     4     6     8
    15    18    21    24

Discussion on matrix "division" using the / symbol for matrices is postponed till later.

Solving systems of linear equations in Matlab

To solve the set of equations
a1 x + b1 y + c1 z = d1
a2 x + b2 y + c2 z = d2
a3 x + b3 y + c3 z = d3

we set this up as a matrix equation of the form
P U = Q
P = [a1 b1 c1; a2 b2 c2; a3 b3 c3]
U = [x; y; z]
Q = [d1; d2; d3]

The solution of this system of equations is U = P-1 Q; this is accomplished in Matlab using
>> U = inv(P)*Q

or by using the backslash \ operator
>> U = P\Q


  1. Set up Mesh Current Equations in the form R I = V and solve for the mesh currents, I.
  2. Set up Node Voltage Equations in the form G V = I and solve for node voltages, V.
(Back to top)


 The for loop (very similar to C expression) is a simple command for setting up a loop. Example:

>> for i = 1:10;
>> a(i) = i*i;
>> end
>> a
a =

     1     4     9    16    25    36    49    64    81   100

All statements between the for and the end statements will be executed as per the command specifications. Example of a for loop where the increment is not 1 would be >> for i = 1:3:20; ..... etc.
Type in
>> help for
for more details.

(Back to top)


 The plot command is used for generating 1-D (functions of one variable) plots. Do >> help plot for complete details. Let's make a graph of y = sin(x), for x on the interval x=0, to x = 10.
>> x = [0:0.1:10]; (here .1 is the increment)
>> y = sin(x); (notice how the sin function operates on each element of the entire row vector x, to generate a nother row vector y)
>> plot (x, y)

Surface or 2-D (functions of two variables) plots are generated using the surf command. More on that later.

To clear a plot, type in >> clg (clear graph)

To generate another plot window, do >> figure

(Back to top)


 You can print a plot using the Print Option under the File Menu in the Figure Window. The text in the Command Window is printed similarly.

(Back to top) 

File Input/Output

The save and load commands are used for saving data to disk or loading data from disk, respectively.
To save values in a  matrix or vector, called, for instance, y, do:
>> y = [1 2 3 4; 5 6 7 8];
>> save y.txt y -ascii
The -ascii option ensures that the data is saved in ASCII form, so that it can be read by other programs - Word, Excel Notepad, Wordpad, etc. Examine the file y.txt using one of these programs. The file is generated in the working directory (remember this? If not click here).

An ASCII data file dat.txt can be loaded using
>> load dat.txt

This provides a variable called dat in the Matlab workspace. All manner of vector/matrix operations can be performed on dat, just like any other variable.

Do >> help save and >>help load for learning all the load/save options.

After you read the next section, click here for a fun Matlab project - generating music with Matlab!

(Back to top)

Executable files (m-files in Matlab)

Executable files in Matlab are generated by storing a list of Matlab commands  in a file given the extension .m
These files are called M-files. To create an M-file, use the New...M-file Option under the File Menu in the Command Window. Type in the following commands in the M-File Editor Window:
x = [0:0.1:10];
y = cos(x);
title('A plot of Cosine(x)')

Save the file using the Save Option under the File Menu in the M-File Editor Window. Call it, say, cosineplot.m. Now, to run this program in Matlab, move over to the Matlab Command Window and just type in >> cosineplot

(Back to top)

Remember, MATLAB IS EASY!!!!!

Back to Course Homepage