Book Image

GNU Octave Beginner's Guide

By : Jesper Schmidt Hansen
Book Image

GNU Octave Beginner's Guide

By: Jesper Schmidt Hansen

Overview of this book

Today, scientific computing and data analysis play an integral part in most scientific disciplines ranging from mathematics and biology to imaging processing and finance. With GNU Octave you have a highly flexible tool that can solve a vast number of such different problems as complex statistical analysis and dynamical system studies.The GNU Octave Beginner's Guide gives you an introduction that enables you to solve and analyze complicated numerical problems. The book is based on numerous concrete examples and at the end of each chapter you will find exercises to test your knowledge. It's easy to learn GNU Octave, with the GNU Octave Beginner's Guide to hand.Using real-world examples the GNU Octave Beginner's Guide will take you through the most important aspects of GNU Octave. This practical guide takes you from the basics where you are introduced to the interpreter to a more advanced level where you will learn how to build your own specialized and highly optimized GNU Octave toolbox package. The book starts by introducing you to work variables like vectors and matrices, demonstrating how to perform simple arithmetic operations on these objects before explaining how to use some of the simple functionality that comes with GNU Octave, including plotting. It then goes on to show you how to write new functionality into GNU Octave and how to make a toolbox package to solve your specific problem. Finally, it demonstrates how to optimize your code and link GNU Octave with C and C++ code enabling you to solve even the most computationally demanding tasks. After reading GNU Octave Beginner's Guide you will be able to use and tailor GNU Octave to solve most numerical problems and perform complicated data analysis with ease.
Table of Contents (15 chapters)
GNU Octave
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

So what is GNU Octave?


In brief, GNU Octave is a multi-functional tool for sophisticated numerical analysis. GNU Octave provides you with:

  1. 1. A large set of build-in functionalities to solve many different problems.

  2. 2. A complete programming language that enables you to extend GNU Octave.

  3. 3. Plotting facilities.

This book will cover these features.

GNU Octave uses an interpreter to compile and execute a sequence of instructions given by the user at run-time. This is also how, for example, PHP and Python work. This is in contrast to pre-compiled programming languages such as C where the program is first compiled and then executed manually. Just like Python, you can give GNU Octave instructions in a prompt-like environment. We shall see many examples of this later. The following image shows a screenshot of GNU Octave in action—do not worry about what the plots are, for the time being.

GNU Octave is named after the chemist Octave Levenspiel and has nothing to do with music and harmonic waves. The project was started by James B. Rawlings and John G. Ekerdt, but it has mainly been developed by John W. Eaton, who has put a lot of effort into the project. GNU Octave is an official GNU project (hence, the GNU prefix), and the source code is released under the GNU General Public License (GPL).

In simple terms, this means that you are allowed to use the software for any purpose, copy, and distribute it, and make any changes you want to it. You may then release this new software under GPL. If you use GNU Octave's own programming language to extend the functionality, you are free to choose another license. I recommend you to have a look at the license agreement that comes with GNU Octave at http://www.gnu.org/software/octave/license.html.

In the remainder of the book, GNU Octave will simply be referred to as Octave for convenience. However, if you wish to sound like an Octave guru, use the "GNU" prefix!

Applications

As mentioned previously, Octave can be used to solve many different scientific problems. For example, a Copenhagen-based commercial software and consulting company specializes in optimization problems, especially for packing containers on large cargo ships. This can be formulated in terms of linear programming which involves solving large linear equation systems and to this end, the company uses Octave. Pittsburgh supercomputing center also used Octave to study social security number vulnerability. Here Octave ran on a massive parallel computer named Pople with 768 cores and 1.5 TB memory and enabled researches to carry out sophisticated analysis of different strategies before trying out new ones.

In the research community, Octave is used for data analysis, image processing, econometrics, advanced statistical analysis, and much more. We shall see quite a few examples of this throughout the book.

Limitations of Octave

Octave is mainly designed to perform numerical computations and is not meant to be a general purpose programming language such as C or C++. As it is always the case, you should choose your programming language depending on the problem you wish to solve. Nevertheless, Octave has a lot of functionality that can help you with, for example, reading from and writing to files, and you can even use a package named sockets for accessing a network directly.

The fact that Octave uses an interpreter means that Octave first has to convert the instructions into machine readable code before executing it. This has its advantages as well as drawbacks. The main advantage is that the instructions are easy to implement and change, without having to go through the edit, compile, and run phase and gives the programmer or user a very high degree of control. The major drawback is that the program executes relatively slowly compared to pre-compiled programs written in languages such as C or Fortran. Octave is therefore perhaps not the first choice if you want to do extremely large scale parallelized computations, such as state-of-art weather forecasting.

However, as you will experience later in the book, Octave will enable you solve very advanced and computationally demanding problems with only a few instructions or commands and with satisfactory speed. The last chapter of this book teaches you some optimization techniques and how you can use C++ together with Octave to speed things up considerably in some situations.

Octave is not designed to do analytical (or symbolic) mathematics. For example, it is not the best choice if you wish to find the derivative of a function, say f (x) = x2. Here software packages such as Maxima and Sage can be very helpful. It should be mentioned that there exists a package (a package is also referred to as a toolbox) for Octave which can do some basic analytical mathematics.

Octave and MATLAB

It is in place to mention MATLAB. Often Octave is referred to as a MATLAB-clone (MATLAB is a product from MathWorksTM). In my opinion, this is wrong! Rather, Octave seeks to be compatible with MATLAB. However, be aware, in some cases you cannot simply execute your Octave programs with MATLAB and vice-versa. Throughout the book, it will be pointed out where compatibility problems can occur, but we shall stick with Octave and make no special effort to be compatible with MATLAB.

The Octave community

The newest version of Octave can be found on the web page http://www.octave.org. Here you will also find the official manual, a Wiki page with tricks and tips, latest news, a bit of history, and other exciting stuff. From this web page, you can join Octave's mailing lists (the help-list is especially relevant), which only require a valid email address. The user community is very active and helpful, and even the developers will answer "simple" user questions. You can also learn quite a lot from browsing through the older thread archives.

There also exists an Usenet discussion group http://groups.google.com/group/comp.soft-sys.octave/topics?lnk. Unfortunately, this group seems quite inactive, so it could take a while for help to arrive.

There exist a very large number of additional packages that do not come with the standard Octave distribution. Many of these can be downloaded from the Octave-Forge http://octave.sourceforge.net. Here you will find specially designed packages for imaging processing, econometrics, information theory, analytical mathematics, and so on. After reading this book and solving the problems at the end of each chapter, you will be able to write your own Octave package. You can then share your work with others, and the entire Octave community can benefit from your efforts. Someone might even extend and improve what you started!