Revised Matlab Notes Fall2004
Revised Matlab Notes Fall2004
-1-
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-
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.
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-
>>!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-
b = ,14
36
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-