Introduction to MATLAB
The purpose of this module1 is to review MATLAB for those that have used it before, and to provide a brief introduction to MATLAB for those that have not used it before. This is a "handson" tutorial introduction. After using this tutorial, you should be able to: • • • 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10 1.11 1.12 1.13 1.14 1.15 enter matrices make plotswrite simple m-files Background Using this tutorial Entering matrices The MATLAB workspace Complex variables Matrix multiplication Plotting More matrix stuff FOR loops and IF-THEN statements m-files Diary Toolboxes Limitations to Student MATLAB Contacting MATHWORKS Summary of Commands • • perform matrix operations use MATLAB functions
1 A good reference is “Introduction to MATLAB For Engineersand Scientists” by D.M. Etter (Prentice Hall, 1996).
© 22 August 1996 - B.W. Bequette
Module 1. MATLAB
MATLAB is an interactive program for numerical computation and data visualization. It was originally developed in FORTRAN as a MATrix LABoratory for solving numerical linear algebra problems. The original application may seem boring (except to linear algebraenthusiasts), but MATLAB has advanced to solve nonlinear problems and provide detailed graphics. It is easy to use, yet very powerful. A few short commands can accomplish the same results that required a major programming effort only a few years ago.
Using This Tutorial
This tutorial provides a brief overview of essential MATLAB commands. You will learn this material more quickly if youuse MATLAB interactively as you are reviewing this tutorial. The MATLAB commands will be shown in the following font style:
the prompt for a user input is shown by the double arrow
MATLAB has an extensive on-line help facility. For example, type
help pi at the prompt
» help pi P I PI = 4*atan(1) = 3.1415926535897....
so we see that MATLAB has the number π "built-in".As another example
» help exp EXP EXP(X) is the exponential of the elements of X, e to the X.
sometimes you do not know the exact command to perform a particular operation. In this case, one can simply type
and MATLAB will provide a list of commands (and m-files, to be discussed later) that are available. If you do not know the exact command for the function that you are after,another useful command is lookfor. This command works somewhat like an index. If you did not know the command for the exponential function was exp, you could type
» lookfor exponential EXP EXPM Exponential. Matrix exponential.
1.2 Using This Tutorial
EXPM1 EXPM2 EXPM3 EXPME
Matrix exponential via Pade' approximation. Matrix exponential via Taylor series approximation. Matrixexponential via eigenvalues and eigenvectors. Used by LINSIM to calculate matrix exponentials.
The basic entity in MATLAB is a rectangular matrix; the entries can be real or complex. Commas or spaces are used to delineate the separate values in a matrix. Consider the following vector, x (recall that a vector is simply a matrix with only one row or column)
» x =[1,3,5,7,9,11] x= 1 3 5 7 9 11
Notice that a row vector is the default. We could have used spaces as the delimiter between columns
» x = [1 3 5 7 9 11] x= 1 3 5 7 9 11
There is a faster way to enter matrices or vectors that have a linear pattern. For example, the following command creates the previous vector
» x = 1:2:11 x= 1 3 5 7 9 11
transposing a row vector yields a column vector ( ' is thetranspose command in MATLAB)
» y = x' y= 1 3 5 7 9 11
If we want to make x a column-vector we use a semicolon as the delimeter between rows
Module 1. MATLAB
» x = [1;3;5;7;9;11] x= 1 3 5 7 9 11
To make x a row vector gain, we use the transpose
» x = x'
Say that we want to create a vector z, which has elements from 5 to 30, by 5's
» z = 5:5:30 z= 5 10 15 20 25 30