Matlab Tutorial
by Paul Pawola
Matlab is a vector- and matrix-based mathematical software package that is used
in ECE 386. This tutorial is intended to introduce you to the basic concepts and
commands that you will be using throughout the semester.
In this tutorial, important terminology will be labeled in bold. Lines
beginning with ">" indicate that this is a line you need to enter into the Matlab
command line.
- Start Matlab by clicking on the Matlab icon on the desktop.
- Begin by creating a vector x1.
> x1 = [1, 2, 3, 4, 5]
- Notice that the result is displayed on the following line in the
command window. The output can be suppressed by ending the line with
a semicolon (;). Additionally, note that the commas used to seperate
the vector elements can be replaced by a space.
> x2 = [1 2 3 4 5];
- To check that x2 contains the correct elements, simply type x2 at the
command line.
> x2
- Matlab can do basic vector addition and subtraction (provided the vectors
are the same dimension), and also scalar multiplication. Try to following:
> q = x1 + x2
> w = x1 - x2
> e = x1*9
- Notice that x1, x2, q, w, and e are all the same dimension: 1 row, 5
columns. A vector transpose can be taken by using ('):
> q'
- Notice that the result is 5 rows, 1 column. A column vector can be
created by seperating the elements by a semicolon:
> r = [9; 7; 5; 3; 1]
- A matrix is essentially a two-dimensional vector. Think of a
matrix being composed of either: 1) a row of column vectors, or 2) a
column of row vectors. As such, a matrix can be made using vectors that
have already been created:
> A = [x1; x2; q; w; e]
- The matrix A has been created from row vectors. To create a matrix
using column vectors:
> B = [x1' x2' q' w' e']
- Like vectors, matrices and be added and subtracted if they have the same dimensions,
and also can be multiplied by scalars:
> Q = A + B
> W = A - B
> E = A*9
- Additionally, matrices can be made by entering all the value row by row, with each row
seperated by a semicolon.
> M = [1 2 3;4 5 6;7 8 9]
- Check the dimensions of a matrix or vector by using the size command:
> size(Q)
- The first number listed is the number of rows, the second number is the
number of columns. Try again for the vector e:
> size(e)
- Long vectors, such as vector containing a sequence of numbers, can be created by using the colon (:) in the following way:
starting_value:increment:ending_value. Try the following:
> t = 0:.1:10
- Notice that the result is a row vector. Certain functions can use vector
inputs to create vector results. For example:
> s = sin(t)
- Plot the result:
> plot(t,s)
- Note that plot takes the x-axis vector first, followed by the y-axis
vector. Additional arguments can be added that can change the line color
or the line style.
> c = cos(t);
> hold on
> plot(t,c, 'r')
- Firstly, the command hold on was used to hold the plot on the graph. If the hold
was not on, the new plot would overwrite the old plot. The 'r' added to the plot command
tells Matlab to plot the line in red. To see what other options are available for plot,
use the Matlab help option:
> help plot
- Here, all of the details and options for the plot command are used. Additionally,
Matlab lists suggestions under "See Also" for other commands that are related to plot. Typing
"help" at the command line brings up a list of toolboxes. These toolboxes contain all the functions
used in Matlab. Once you decide what toolbox you need, type help toolbox to see the functions
available in that toolbox.
> help
> help polyfun
> help spline
- For more user-friendly Windows-style context-searchable help, use the command helpwin.
- Going back to the plot, give the plot a title using the title command:
> title('Plot 1')
- Notice that Matlab uses single quotes for strings. Label the axis by using the xlabel and
ylabel commands:
> xlabel('Time')
> ylabel('Result')
- Additionally, special characters can be used. Greek symbols can be added by using a backslash (\) followed
by the symbol name, a superscript can be added by using a carrot (^), and a subscript can be added using an
underscore (_). Give the plot a new name:
> title('\Sigma^2 + \alpha_3')
- To further enhance the plot, a legend can be added using the legend command:
> legend('sin','cos')
- To change the scale of the plot, use to axis command. Use the Matlab help to learn how to
use the axis command, then change the axis so that the x-min is 0, x-max is 2*pi (Matlab understands "pi"), and the y-min
is -1, and the y-max is 1:
> help axis
- The command who gives a list of all the variables in the workspace.
> who
- The command whos gives, in addition to the list of variables, the dimension, the size (in bytes), and the type of each variable. It also tells how many total elemens
are being stored, and how many total bytes are used.
> whos
- The clear command can be used to delete variables from the workspace. Delete the variable s:
> clear s
> who
- Notice that s no longer exists. Now delete all elements:
> clear
- Create new vectors x and y, and matrices X and Y.
> x = [1 3 5 7 9]
> y = [2 4 6 8 0]
> X = [1 1;2 3]
> Y = [1 9;2 8; 3 7]
- Vectors and matrics can be used in polynomials, but special care needs
to be taken. Observe the following sequence:
> z1 = 3*x + y
> z2 = x^2
- Matlab has no problem with z1; however, z2 gives an error. The reason for this is that
x^2, in Matlab, is equal to x*x. From linear algebra, you know that unless x is a square matrix,
this doesn't work. Now, to square each individual element of x, use (.^) instead:
> z2 = x.^2
- This works, and each element of z2 is the square of the corresponding element of x. This can be used for division and multipation of vectors and matrices as well.
> z3 = y .* x
> z4 = y ./ x
- For matrices, the same rules apply. To extract a specific row or column for a matrix, use the colon (:). For example, to extract the first column of Y:
> Y(:,1)
- Essentially, the colon means "all". So, the above lines is saying matrix Y, all rows, first column. Similarly:
> Y(1,:)
- This says matrix Y, first row, all columns. This can also be used with vectors, since Matlab treats vectors as matrices with either 1 row or 1 column.
To extract numbers in a range, use the following: start_point:end_point. For example:
> v = 0:1:100
> r = v(1:30)
> g = v(45:70)
- The vector r contains the first 30 elements of v, and g contains the middle portion of v.
After completing this tutorial, you should be able to do the basic Matlab commands given, and should be able to find any other Matlab commands or Matlab info that
you will need.