You are on page 1of 4

Introduction to UNIX and MATLAB

Joseph C. Slater Jason J. Blair January 3, 1997 MATLAB ( MATrix LABoratory) is a mathematical computing package that combines numerical analysis, matrix computation, and advanced graphics in a user-friendly environment. MATLAB is a language that is similar C or FORTRAN but has greatly enhanced capabilities. Programs are written and stored in script or function files in ASCII (or text) format called M-files that carry the .m extension (e.g. vtb1_1.m ). MATLAB is available via accounts on gamma, which can be accessed from the X-terminal lab in RUSS 152. To login to use MATLAB, you must first connect to gamma. Do this by clicking on the gamma button and then clicking on the connect button. When prompted, enter your username then password. The first time you log in you should change your password. Do this with the yppasswd command at the prompt. Note: UnIX Is caSe SenSItIve. You next need to set your DISPLAY environment so the main server knows where you are. This will enable your graphics display to be sent to the X-Windows terminal that you are using. Do this by typing setenv DISPLAY machine_number:0.0 where terminal number is something like 130.108.11.13. Be sure to include all numbers and to put a :0.0 on the end (this should be displayed on the front of the terminal). Also, remember the case sensitivity. (If you are running at home without X-terminal capabilities, you can use the command unsetenv DISPLAY and type terminal at the M ATLAB prompt. This does not work well, but you may be able to at least display the graphics.) To use email, I recommend trying pine. To start MATLAB, type matlab from the UNIX prompt. Type demo for a brief introduction to the capabilities of MATLAB. All of the UNIX commands can be used within MATLAB by placing ! in front of them. For example !ls, lists the contents of the current directory. MATLAB is an interactive language. You can use the commandhelp from within MATLAB for help on MATLAB commands. A set of MATLAB commands can be executed by storing them in a script file in your MATLAB path. To execute a script file, type the name of the script file (without the .m ) at the MATLAB prompt. To write these scripts you will need use a UNIX editor. There are several editors to choose from (e.g. vi, axe, dxnotepad, emacs and pico). To start an editor from within M ATLAB type !axe, !vi, !pico... at the M ATLAB command prompt or simply type the editors name in lower case from the UNIX prompt. Alternatively, typing filebar at the M ATLAB prompt may simplify this task for you. Of the editors, vi is considered by many to be the hardest to use. I recommend you use dxnotepad, axe, or emacs. Once written, M-files must be saved with the .m extension. If the file is simply a script file (a list of commands that does not take input arguments or return output variables), you can run the program by simply typing the script name without the .m extension (make sure you use the right case. Its advisable to use all lower case in naming your M-files. All of the student edition of the Vibration Toolbox codes are what you would call function files. They accept an input argument, and some return a result (output argument). This is functionally how commands such as sin , cos, and plot work. Use the command type to view the file vtb1_1 . You can also open it for read-only in an editor if you want to save and modify it to make your own codes. It can be found in the directory /nfs/valhalla/users26/me/jslater/vtoolbox.

Sample script file example.m (defines 2 row vectors, multiplies them, then plots one versus the other) 1) From MATLAB prompt type !dxnotepad example.m 2) type a = [1 2 3 4 5 6]; ( ; suppresses the output to screen) 3) type b = [7 8 9 10 11 12]; 4) type a (no ; this displays contents of a on screen) 5) type b (this displays contents of a on screen) 6) type x = a.*b (notice the . to signify array or element by element multiplication) 7) type plot(a,x); (plots a versus x ); 8) type xlabel(Array a); 9) type ylabel(Array x); 10) type title(a versus x); 12) To exit the pico editor hit the ctrl and x key at the same time 13) answer yes to save changes to example.m 14) at the MATLAB prompt type example to run the program A function file is one that takes input arguments and returns output variables to the workspace (or another M-file if it was called from one). For instance, edit the file vtb4_1.m . You can do this by typing: pico /nfs/valhalla/users26/me/jslater/vtoolbox/vtb4_1.m (Dont worry, you wont be able to mess the file up). The first few lines appear as:
function [P,w,U]=vtb4_1(M,K,dispar) %VTB4_1 Natural frequencies and eigenvectors for an undamped %system.

From the MATLAB prompt, if you type help vtb4_1 you will notice the response is the contents of the first set of lines that start with %. This allows you to put a help section in your program. The first line of the function declares that it is a function file, the outputs are P, w , and U and the inputs are M, K, and dispar . Also notice that the name of the file appears in the first line. This should agree with the filename when you create your own function files. If you want to perform the operation that vtb4_1 has been programmed to do, all you need to do is type vtb4_1(M,K,dispar) from the M ATLAB prompt where M, K, and dispar are variables that have already been assigned. Alternatively, you can run the function by typing it as vtb4_1(1,2,3) and the variables M, K, and dispar will take on corresponding values inside the function. (Only the output variables of the function will change the values of variables in the workspace. The rest of the variables internal to the function are local to the function.) If you want to obtain the output of a function, you must use output arguments in giving the command (this lets MATLAB know where to store the information). For instance [P2,w2,U2] = vtb4_1(M,K,dispar) will execute the commands of vtb4_1 using the input variables M, K, and dispar . Once the function has completed running, the values of P, w , and U inside the function will be assigned to the variables P2 , w2 , and U2 outside the function in the workspace (or wherever the function was called from). In this way, functions can act as stand-alone programs inside MATLAB or as subroutines for other functions or scripts.

Basic UNIX operations. Command ls mv old new cp old new rm filename mkdir mydir cd .. cd mydir rmdir mydir man topic lps1 text softlist showquota exit or lo ^c ^z bg jobs %n pwd command & pine Action List files in current directory moves file old to file new copies file old to file new removes filename makes a directory mydir move up 1 directory move into directory mydir removes directory mydir help on topic prints file text to lps1 printer as text shows available software shows user disk space log out escape/kill stop (do not use this to exit MATLAB) continue to run stopped job in background list the names and numbers of all jobs reconnect to stopped job n show present working directory run command in the background Read, edit, and send email

Basic MATLAB commands. Command


help + * / ^ a = [1 2 3] who A=[1 2 ; 3 4] inv(A) A plot(x,y) xlabel(title) ylabel(title) title(title) hold figure figure(n) x = 1:5 x = 1:.5:5 .* ./ ; diary

Action help Addition subtraction multiplication division power denotes vector a = [1 2 3] shows variables define matrix A matrix inverse of A transpose of matrix A ([1 3 ; 2 4]) plots x versus y (they must have same length) assigns name title to x axis of plot assigns name title to y axis of plot assigns name title to the title of plot freezes the plot screen opens a graphics window selects figure number n (integer n) generates x = 1 2 3 4 5 in steps of 1 generates x from 1 to 5 in steps of .5 multiplies two arrays element by element divides two arrays element by element used at end of command to suppress output to the screen creates a diary file of whats on the screen

You might also like