A Quick Start to Octave

1

Two days ago, I have started working on a new web application that manages a
power quality monitoring and analysis system. The system consists of multiple sensors
that are spread through multiple locations and periodically send readings to
the server which pass the data to an Octave
script to process and analyze.

In case you don’t know, GNU Octave, as described on its home page, is a
high-level language, primarily intended for numerical computations. It provides
a convenient command line interface for solving linear and nonlinear problems
numerically, and for performing other numerical experiments using a language
that is mostly compatible with Matlab.

Today, I’m going to show you how to install Octave on Windows and do some
simple tasks.

First, you need to go to GNU Octave Repository and download the latest version of
Octave Forge Windows. At the time of this writing, it is version 3.0.1
(octave-3.0.1-setup.exe). Run the downloaded file and follow the installation
wizard keeping all default options. Then, use the Windows "Start"
menu to open the Octave command window ("All Programs" > "GNU
Octave" > "Octave").

So, let’s try some commands (always press "Enter" after typing a
command):

To exit at any point, just type:

exit

Now, type:

x=1+2

and note that the response is shown in the command window. Then, type:

x=1+2;

and note that the response is not shown.

Type:

x

to show the value of the variable.

Now, type:

who 

to show the contents (variables) of the workspace (should now include x).

Type:

clear

to clear the workspace.

Type:

help sin

to get help on the "sin" command.

How about some matrices?

Type the following (press "Enter" after each line):

A = [1, 2, 3]
B = [4, 5, 6]
5 * A
B' 

This will create two matrixes, multiply "A" by 5 and finally get
the transpose of "B".

Let’s write a script

Type:

pwd

to print the working directory of Octave. For me, the response was:

ans = C:\Program Files\Octave

This is the directory where Octave looks for your scripts (*.m).

Use any text editor, like Microsoft Notepad, to write a simple script and
save the file as myscript.m in Octave’s working directory. Here is a simple
script that generates a plot:

t=[0:.1:100]';
u=-1+0.02*t; y=sin(0.2*t);
plot(t,y,t,u)
%This is a comment 
%y (y-axis) is plotted against t (x-axis) 
%and u (y-axis) is plotted against t (x-axis)

Type myscript (without the file extension .m)
in the command window to run the script. Octave will automatically create a
separate window to display the plot.

Using Windows Command Prompt

If you want to run Octave scripts without opening a Octave’s windows, you
just need to add C:\Program Files\Octave\bin to your Path
Windows Environment Variable.

Now, you can run a normal Windows Command Prompt and type octave. Type pwd
to know the current working directory. For me, the response was:

ans = C:\Documents and Settings\Hatem

Type exit to quit Octave.

Now, to run the script, copy it to the working directory and then type:

octave myscript.m

That’s all for today. Later, I may post more details on how I integrated
Octave with my Ruby on Rails application.

 

Comments

1

it was very useful to me

Post a Comment

eSpace podcast Prodcast

RSS iTunes