------------------------------------------------------------------------- PHYS210: MATLAB LAB 2 ------------------------------------------------------------------------- o Login and open a terminal window and have a browser pointed to these notes as usual o In the previous lab we learned how to use Matlab to perform simple calculations interactively, i.e. by explicitly typing expressions and commands in the command window o Paralleling what we did in Maple, we now want to learn how to prepare Matlab commands in a text file --- a.k.a. a Matlab source file --- that can then be "read into" a Matlab session o Typing the commands into a source file has the same benefits in Matlab as it does in Maple, and, again, is particularly crucial when we are writing programs in the language o As we will see in more detail, a fundamental unit of Matlab programming is the 'function', which is completely analogous to the 'procedure' in Maple. Each function has a name---chosen by the programmer---such as ... myfcn minmax ... etc. o In addition to functions, the other key unit of Matlab programming is known as a 'script' and is simply a sequence of an arbitrary number of Matlab commands and statements, which will often include calls to functions that the user/programmer has defined o For example, all of the commands that you typed at the Matlab prompt in the last lab, if they were entered in a text file, would constitute a script o Given this brief description of the two main units of Matlab programs, there are two basic rules that govern how we must prepare and process Matlab source files: 1) All Matlab source files (which are text files) must have names that include an extension .m For example --- foo.m myfunction.m myscript.m ... are valid filenames in this context, while ... foo myfunction.x myscript.somethingelse ... are not 2) There is no analog of the Maple 'read' command in Matlab. Rather, in order to make Matlab process ("read") a source file, we simply type the filename WITHOUT THE .m EXTENSION at the prompt. For the time being, in order for this to work we need to ensure that the source file RESIDES IN THE DIRECTORY FROM WHICH MATLAB WAS STARTED/LAUNCHED o Let's see how this works ... o First, in a terminal window, change to the 'matlab' directory that we created in the last lab % cd ~/matlab % pwd /home/phys210d/matlab If you haven't yet made the directory, then do so now, via ... % cd % mkdir matlab ... then execute ... % cd ~/matlab % pwd /home/phys210d/matlab o Now, we will copy a (very) basic script file, myscript.m, from ~phys210/matlab % cp ~phys210/matlab/myscript.m . o Examine the contents of the file % cat myscript.m % This is a simple Matlab script a = 1 b = [1 2 3 4] c = [1:3:22] d = linspace(0, 1, 6) e = [10; 20; 30] o Now, start Matlab FROM THE COMMAND LINE IN THE SAME TERMINAL WINDOW SO THAT THE APPLICATION STARTS IN THE DIRECTORY ~/matlab % matlab & o In the Matlab command window, enter the following ... >> myscript ... and you should see the output ... a = 1 b = 1 2 3 4 c = 1 4 7 10 13 16 19 22 d = 0 0.2000 0.4000 0.6000 0.8000 1.0000 e = 10 20 30 ... which is precisely what you would see if you entered the five statements (the comment line doesn't count) in myscript.m interactively o And executing a Matlab script is as easy as that! o Now it's your turn ... ------------------------------------------------------------------------- EXERCISE 1: ------------------------------------------------------------------------- o Download ... http://laplace.phas.ubc.ca/210/Labs/matlab-ex1.pdf ... also available via the Syllabus section: MATLAB Exercises 1 [PDFl], and follow the instructions. ------------------------------------------------------------------------- EXERCISE 2: ------------------------------------------------------------------------- o Add the following bash alias to your ~/.aliases file, so that alias mat='matlab -nodesktop -nosplash' o Again, it is best to make an explicit backup of ~/.aliases before you modify it. For example % cd % cp .aliases .aliases.O o Once you've added the alias, test it by starting a new terminal session and typing ... % mat ... to ensure that command line Matlab *does* start o Ask for help as necessary