web page:
- Octave for Windows (click on
the "Octave 3.0.1 for Windows Installer") The first setup program listed
for download seems to be for XP; the second one (with the vs2008 string)
is probably for Vista.
Note that octave uses gnuplot for plotting. These Windows installer
should
automatically install gnuplot, too, and place it in the right location for
use by octave. Also note that the installer has options for gnuplot
plotting and some sort of experimental javascript plotting; I've only
tried the gnuplot installation.
- Octave for Macs (get the "2008-08-23 binary of Octave 3.0.2")
The Mac package does not install gnuplot; Mac users would need to install
gnuplot separately.
- For Debian-based Linux distrubutions (like Ubuntu):
"sudo apt-get install octave gnuplot"
Octave Basics
When you start octave, a command window will run with a prompt
that looks something like:
octave>
A semicolon at the end of a line will hide various output items.
Try these two commands and note the results:
octave> a=4
octave> a=4;
Test the plotting routine by entering these commands (the first command
creates a linear one-dimensional array of 100 x-values ranging from 0 to 10):
octave> x=linspace(0,10,100);
octave> y=sin(x);
octave> plot(x,y)
(a separate plot window should appear with the sin(x) graph)
To generate a plot file, use one of these print commands (note that
the plot command above must be run before the print command will work).
The first command writes a graphics file in jpeg format; the second
command writes a postscript file (another graphics format).
octave> print -djpg junk1.jpg
octave> print -dps junk1.ps
If you are not sure where files are being read and written, use the
pwd command to show the path in use by octave:
octave>pwd
C:\Program Files\Octave
Using the lsode Routine to Integrate a System of Rate Equations
To use the routine, the user must first write a function called xdot
(with the rate equations), then the user must set up initial conditions
in an array called x0 and time in an array called t, and once parameters
are set, the user runs the lsode command.
Here is a sample session:
Set up the rate-equation function. Concentrations correspond to
x(1),x(2), etc., and xdot(1) is dx(1)/dt.
octave> global k1 k2 k3;
octave> function xdot = f(x,t)
> global k1 k2 k3;
> xdot(1)=-k1*x(1) + k2*x(2);
> xdot(2)= k1*x(1) - (k2+k3)*x(2);
> xdot(3)= k3*x(2);
>endfunction
Assign values for rate constants, set the initial concentrations (x0,
note the semicolons between values), set up a linear array of time
values (t), run lsode, and plot the result.
octave> k1 = 1.0;
octave> k2 = 2.0;
octave> k3 = 2.0;
octave> x0 = [1.0;0.0;0.0];
octave> t = linspace(0,5,100);
octave> y = lsode("f",x0,t);
octave> plot(t,y);
To change a rate constant and run again:
octave> k2 = 4.0;
octave> y = lsode("f",x0,t);
octave> plot(t,y);
To change the time range and run again:
octave> t = linspace(0,10,100);
octave> y = lsode("f",x0,t);
octave> plot(t,y);
You may want to print the numerical results to a file. The following
set of commands will do this (note that the integrated solution is
stored in a 2D array called y):
octave> file1=fopen("junk1.out","w");
octave> for i=(1:100)
> fprintf(file1,"%f %f %f %f \r\n",t(i),y(i,1),y(i,2),y(i,3));
> endfor
octave> fclose(file1);
(Note for linux, unix, and probably Mac OS X, use \n in place of \r\n
in the fprintf command [\n is linefeed, \r is carriage return])
The output file, junk1.out, will contain four columns of numbers:
t y1 y2 y3 (the y's correspond to the three concentrations - x becomes
y for the result computed in lsode).
Octave Scripts
Octave commands can be saved in text files (use a text editor like notepad
if using Windows). Here is a sample script to integrate a first-order
rate equation. Note that the first line of a script cannot be a function,
so a dummy line (a=1) is shown first.
a = 1;
function xdot = f(x,t)
xdot(1) = -2.0*x(1);
endfunction
x0=[1.0];
t=linspace(0,10,50);
y=lsode("f",x0,t);
plot(t,y);
If this file was named firstorder.m, then the following command would
load and run it in octave:
octave> source "firstorder.m";
Be careful not to give your scripts the same name as Octave built-in
functions (don't use "print.m", for example).
More Help
The octave web page (google octave) has a detailed manual (see the appendicies
good lists of commands and function), a wiki, and a FAQ. There
are also many helpful online tutorials (google octave tutorial).