You are on page 1of 6

ME365 MATLAB Introduction Notes

MATLAB INSTRUCTIONS FOR ME 365 (Updated: Fall 2004)


This document is a much shortened version of the instruction set for MATLAB. It is intended to help you get started. Once you know the basics, the MATLAB help function can be used to help you learn the commands and how to run them. I like the student version manual, because it has examples that are helpful.

-1-

ME365 MATLAB Introduction Notes

TABLE OF CONTENTS
A. The Preliminaries.......................................................................................................... 3 A.1 Computer Setup for MATLAB.................................................................................... 3 .2 Unix commands while running MATLAB on a UNIX machine. .................................. 4 A A.3. Some very basic commands. ................................................................................... A Getting 5 .4 Help ..............................................................................................................7 A.5 Creating an Mfile...................................................................................................... 9 A.6 Plotting a graph on the screen ................................................................................11 A.7 Sending the plot to the printer. ................................................................................13 A.10 Loading Data.........................................................................................................14 B. Some Examples of Things You May Do. .................................................................16 B.1 Entering Data and Plotting it....................................................................................16 B.2 The Dreaded "dot" Command, Subtitled: Loops Without Loops!............................. 18 B.3 Calculating and Plotting a Step Response. .............................................................21Function........................................ 22 B.4 Calculating an Plotting a Frequency Response C. A Little More Sophistication in MATLAB Programming..........................................25 C.1 Loops in a Fourier Series Example .........................................................................25 C.2 If Statements...........................................................................................................27 C.3 While Statements....................................................................................................27 C.4 Subroutines/Functions font sizes and line thickness, etc. ................................................ C.5 Changing graph ............................................................................................28 29

-2-

ME365 MATLAB Introduction Notes

A. The Preliminaries
On the PC's the basic commands like plotting, printing and editing are found in the menus that come up after you have launched MATLAB.

A.1 Computer MATLAB

Setup

for

First lets deal with the Computers running under the UNIX operating system. You should be in an X-windows environment when you initiate MATLAB. At the prompt, you just type:

matla b
and the computer will initiate the program. By default this generates a window (desktop) similar to the one that you are in when you launch MATLAB on a PC. The Command Window part of this desktop is where you type in your commands. You will see a prompt: >>, in the window. You can turn off the various elements of the desktop, as you wish. If you like to stay in a more UNIX-style environment, you can, instead, type:

matlab -nodesktop
at the prompt, and you will stay in your current X-window and a >> prompt will appear. To leave MATLAB just type:

>>quit
in the Command Window (or the X-Window if there is no desktop window) and all the associated MATLAB generated windows will disappear. On the PCs you need to find the Applications folder and in there you will see the MATLAB icon. Double click on this to launch MATLAB. It'll take a short while before a command window will pop up. To leave MATLAB pull down the menu and find the command that says EXIT MATLAB or just type:

>>quit

-3-

ME365 MATLAB Introduction Notes

A.2 Unix commands while running MATLAB on a UNIX machine.


Once you are running MATLAB in a UNIX environment, you can still use UNIX commands, so long as you put an exclamation mark before the command. So, for example, if you wanted to find the time and date, date is a UNIX command that gives you this information. In MATLAB type:

>>!date
and the UNIX command will be executed. If you are using the nodesktop option, you can use the simple v editor, by typing: i

>>!vi john.m
and this will allow you to create or update the file called john.m. When you type the :wq command to save and come out of the editor, you will find yourself back in MATLAB. Alternatively, and more convenient when developing programs (m-files), you can do your editing from another window. If you are using the desktop option (the default), you can use the inbuilt MATLAB editor (youll find it in one of the pull-down menus) or again, do your editing outside MATLAB in another application window. If you type

>>!ls
or

>>ls
you should see john.m and all the other files in that directory. Program files in MATLAB always have a ".m" extension. MATLAB looks for these m-files in the directory you are working in, when you type a command from within MATLAB. MATLAB also looks in its own directories for the command. Where MATLAB looks is stored in the path variable. You can extend the list of directories in which MATLAB looks for commands in by using the path command. Normally you will not be doing this in the UNIX environment because you are working in the directory where the files are kept. On the PCs, you will use this command to tell MATLAB to look on the floppy disk drive or on the drive connected to your ecn account.

-4-

ME365 MATLAB Introduction Notes

A.3. Some very basic commands.


MATLAB is happy to have variables that are scalars, vectors or matrices and it is easy for you to create them without having to declare data types as you do in C or Fortran. MATLAB can also interpret complex numbers and will do complex arithmetic. It will also do matrix algebra for you, so long as you have the correct dimensions for the vectors and matrices, and it will also solve sets of equations for you. As an example, suppose we have a set of three linear equations in matrix form: [A] x= b.,
1 1 1 4 1 9 6

where [A] = ,1 and 3 2

b = ,14
36

and we wish to solve for x. To do this we type:

>>A=[ 1 1 1;1 2 3;1 4 9 ]; >>b=[ 6 14 36 ]'; >>x=A\b; >>x'


which prints the result as a row vector. Note the use of the semicolon at the end of the commands; this suppresses output to the screen. Within the matrix specification, the semicolon is used to denotes next line of the matrix. The accent 'denotes transpose. . xand bare column vectors, but in this example it is easier to input and display them as row vectors. The square brackets are used to input vectors and matrices. The \operator in the A\b command tells MATLAB to solve the set of equations [A] x= b. Let's look at another example. Suppose we have a function defined by:
y = 24e- 2 x + 4 ,

and we wish to plot it for from x = -5 to x = 15. Lets plot it with x incrementing in steps of 0.1 as it goes from - 5 to15. Suppose we also wish to label axes and put a title on the plot. To do this, you type in the following commands:

>>x=(5:0.1:15); >>y=24*exp(-2*x) +4; >>plot(x,y) >>xlabel(Distance Kilometers) >>ylabel(Number Complaints) >>title(Community Model)

(x)

of Response

-5-

You might also like