A place to breathe

Wednesday, May 7, 2008

Fun with GNU Octave

Hello World!

Let's explore the capabilities of GNU Octave. Basically, I would like to write some simple programs so that I could do some other cool stuff with GNU Octave (I don't know what is it yet). Well, it's always good to start with, guess what, hello world program!

Using my text editor, I simply write this line:

printf("hello, world \n");

and I save it as "hello.m". So to run this program, we can easily just invoke "hello" at the GNU Octave command prompt. Like this (see picture below):



Yes!, we're now a Matlab programmer. Let's put that in our resume.

Simple Calculation

Now I would like to explore the true mathematical power of Octave. The thing that I found about Octave is that, it's so easy to use. For example, at a command prompt, I can easily do mathematical computation by entering this :

octave:1> 1+2
ans = 3

As you can see, Octave doesn't need to enter into special mode of some sort to do simple calculations like this. It just sort of know how to do it. Of course, you have to get into the Octave shell to be able to do this so easily (if you still haven't gotten your GNU Octave working right, please email me). Saving this calculation in calc.m, I can execute this command like this:

octave:11> calc
ans = 3

Ah, beautiful.

Matrices

I'm sure you still remember (at least vividly) of matrix operation from your high school/college education. As we can guess, matrix operations plays significant role in mathematical computations. You may ask, "what's the good of matrix operations for me in my daily life? After all, I'm not a mathematician to begin with." Neither do I. I absolutely have no idea why I explore into this realm of computational program like Octave if I can get my job done (as in my work) in C++ or Perl, or TCL. So, the question is not so much of whether it has any practical applications as of now, as the time you're reading this writing. The question is to appreciate the beauty of matrix operations, and how cool it is to be able to do matrix operations using Octave so easily. When you know you can do something about it, you may get an idea what sort of thing you want to do with the new knowledge that you just gained.

OK, enough blabbering. Let's start trying to enter some matrix operations:

octave:17> v = [1 2 3 4]
v =

1 2 3 4

Here, I assign v as a 1x4 matrix, [1 2 3 4]. The matrix doesn't have to be 1x4 in dimension. It could also be 2x4 dimension, etc. For example, for 2x4 matrix, I can just enter:

octave:19> v = [1 2 3 4
> 1 3 4 5]

v =

1 2 3 4
1 3 4 5

As you can see, I simply enter and making a new line after [1 2 3 4 to define my next matrix entry 1 3 4 5]. The result would be a 2x4 matrix of v.

No comments:

About Me

I'm currently a software engineer. My specific interest is games and networking. I'm running software company called Nusantara Software.