Intermediate Physics Lab

PHYS 399 - Spring 2009
MATLAB Information

Updated

Overview

MATLAB is a widely used system in Science and Engineering for performing computation, data analysis, and data visualization. We will only scratch the surface of what MATLAB can do, but getting some hands-on experience with this powerful tool is one of the main goals of PHYS 399. Each student needs to come up with a way to run MATLAB which they are comfortable with, as many of the lab assignments will involve doing some amount of work in MATLAB.

MATLAB Tutorials

The book Getting Started with MATLAB 7 by Rudra Pratap is a pragmatic introduction to MATLAB which should be all you need for this course. MATLAB has a very good built-in help system, which also includes a series of tutorials. These may also be useful to look at for some students. There are also a variety of online tutorials and quick reference guides available. Some are listed from the Mathworks MATLAB Tutorial page. For students interested in a more detailed reference, I would recommend Mastering MATLAB 7. Note that the behavior of MATLAB has changed slightly over the years, so some older tutorials may not work exactly the same under MATLAB 7.

Getting MATLAB

Each student needs to find an acceptable solution to allow them to work regularly with MATLAB. The University of Oregon has a site license for MATLAB, and the Physics department has its own academic license, but installing this application on personal machines is not always trivial. In principle it does run on Window, Mac OS X, and Linux (amongst others) so you should be able to find a solution which works for you. The following is a list of possibilities in rough order of ease for the student.

Running MATLAB

If you have successfully found a version of MATLAB which you can run, you should be able to work through the tutorial exercises in Pratap. If you have a complete install with graphics, however, it is good to check that you can make a plot using the commands below.

>> theta = linspace(0, 2*pi, 100);
>> plot(sin(theta), cos(theta), 'ro-')

Note that linspace builds an array of 100 values. You could also specify a specific spacing by using theta = 0 : 0.1 : 2*pi.

Using MATLAB

It is very convenient to be able to type commands in at the MATLAB prompt and manipulate values interactively. This is also a very poor habit to get into for doing any real work in MATLAB. Anything worth doing once will almost certainly end up being done again, and anything beyond a very trivial manipulation will typically involve several steps to complete. You should very quickly get in the habit of writing M-files with all your commands and executing these M-files by simply typing the filename (without the .m suffix) at the Matlab prompt. Even if you aren't writing functions, having a series of commands in a file gives you a direct record of what you have done, and once you get it working properly, you can save it to use in the future. If you are working in the full MATLAB environment, it is easy to create and edit MATLAB M-files through the browser window. Just make sure you know where your files are going by setting the Working Directory appropriately before you start. Depending on the version of MATLAB and the platform, this may not be what you expect.

Installing X11

To get graphics when running MATLAB remotely (for instance on acad-cl1), you need to have an X11 server running on your local machine. Some tips for doing this on Windows have been contributed by Eric Carlson. On OS X, you need to install X11 which isn't part of the system by default. This can be found on the "Developer Tools CD", or else downloaded from Apple. Once X11 is installed and running, use the Terminal to ssh into acad_cl1.

>ssh -Y acad-cl1.uoregon.edu
The -Y option causes any X11 windows opened by your shell on acad-cl1 to be tunnelled through the ssh connection and appear on your local machine. Linux uses X11 natively, so you only need to make sure you use the ssh X11 tunnel. This varies for different varieties of ssh/Linux, but will likely either be -X or -Y.