# Matlab

Matlab is a very nice general-purpose programming and computation environment.  Compared to C, C++, or Java, Matlab coding is easier because you do not need to declare variable types (integer, double precision) or pre-allocate arrays.  Matlab has lots of very nice graphics routines that you can easily use.  On the other hand, programs written in Matlab do not run as fast as those written in a compiled language, and Matlab needs to be purchased or used in a computer lab.

If you need help learning how to program, also refer to my page on learning to program with Matlab or Octave.

One way to learn Matlab syntax is to sit at the console, type Matlab commands, and see the result.  I have developed sets of instructions of this type for classes I have taught:

• Instructions1 covers Matlab basics, random number generation, simple programs, and some plotting.  (for Chemistry 694)
• Instructions2 covers Markov chains with absorbing states, run lengths, Monopoly, RNA sequences, HMM, and SCFG (for Chemistry 694)
• Matlab_intro covers Matlab basics, graphing, random number generation, and Matlab programs (for Math 682 Stochastic differential equations)

# Octave

Octave does not have a graphical user interface (GUI) like Matlab does.  You will want to spend a few minutes setting some parameters to make it easier to use Octave.  First, when Octave runs, its working directory is C:\Octave\3.2.3_gcc....  You can always see the current working directory using the "pwd" command (print working directory).  The working directory is where Octave will look for the program files that you tell it to run.  You can change the working directory on a Windows machine in two ways.  First, you can type something like:  cd 'c:\documents and settings\zirbel\my documents\octave' to set the working directory to the Octave folder in your My Documents folder.  You will put all of the programs you write there.  Second, to prevent having to do this each time you start Octave, you can tell Octave what directory to start in.  To do this, find the icon you will use to start Octave (usually under the Start menu, Programs, Octave) and right click it, then select Properties.  You'll see a window like this:

Edit the "Start in:" box to be the path to where you will store your Octave files.  In my case, I'd like Octave to start in "C:\documents and settings\zirbel\my documents\matlab".

## Writing a program and running it in Octave

To write a program, start Notepad and type your program.  For example, you could type:

for i = 1:10,
i
end

This will print out the numbers from 1 to 10.  Save it where you will store your Octave files with a name like sample.m.  (Program names must start with a letter and can consist only of letters and numbers, no spaces or special characters.)  If you have the option, save it as type "All files," otherwise Notepad might save it as sample.m.txt.  Then, in the Octave window, type sample and hit return.  Octave will look in its current directory (which you have already set correctly), it will read the commands in the program, and it will execute them, showing the output on the screen.

## Octave details and quirks

The command window in Octave is not as nice as in Matlab.  You can modify things a little bit by right clicking the title bar of the Octave window.  Select Properties to change the appearance of the window.  Select Edit, Mark to mark text that you want to copy.

The Figure window in Octave does not have any useful controls like File, Print.  The best way I know to output a figure is to use this command in the command window:

print filename.png
print filename.pdf

You can read about the print command by typing "help print".  You can put a .png file into a Word document using Insert, Image from File, etc.  The best way to print a graph from Octave is to create a .pdf file and print that.

The program simulate_chain.m makes graphs with the points marked by circles.  For reasons I do not understand, Octave uses really big circles.  You can replace the plot command in simulate_chain.m with this line:

plot(t, x, 'o', 'markersize', 1);

The o's will be smaller and look much better.