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

Time for action - testing with peaks


  1. 1. You can enter the interactive environment by typing octave in your shell under GNU/Linux, or by double-clicking the Octave icon in Windows. You should now see the Octave prompt:

octave:1>
  1. 2. You have already learned how to exit the interactive environment. Simply give Octave the command exit or press Ctrl + D, but we do not want to exit just yet!

  2. 3. At the prompt, type as follows:

octave:1> surf(peaks)
  1. 4. You should now see a three-dimensional surface plot as depicted on the left-hand side figure shown next. If not, your installation has not been successful. Now, put your mouse pointer over the figure window, hold the left mouse button down, and move the pointer. If the plotting program supports it, the figure should now rotate in the direction you move the pointer. If you click on the figure window using mouse button three (or the scroll wheel) you can zoom by moving the pointer side to side or up and down.

  2. 5. Let us try a contour plot. Type as follows:

octave:2> contourf(peaks)
  1. 6. Does it look like the following figure on the right? If not, it can be because you are using Octave version 3.2.4 and have the package oct2mat loaded. Try typing

octave:3> pkg unload oct2mat
  1. 7. Now retype the previous command.

  2. 8. Click somewhere on the window of the right-hand side figure with button three. A cross and two numbers appear in the window if you are using gnuplot with Octave. The cross is just the point where you clicked. The two numbers show the x axis and y axis values.

Note

Octave can use different plotting program, for example, gnuplot or its own native plotting program. Therefore, your figures may look a bit different, depending on that program.

What just happened?

The figure to the left shows a graph of a mathematical function, which is a scalar function of two variables y and x given by:

(1.1)

The function value (range) is evaluated in Octave by the command peaks, which is the nick name for the function f. The graph is then plotted using the Octave function surf. As a default, Octave calculates the range of f using 50 x and y values in the interval [ 3; 3]. As you might have guessed already, the contourf Octave function plots the contours of f. Later we will learn how to label the axis, add text to the figures, and much more.

Did you notice the phrase "Octave function" previously? An Octave function is not necessarily a mathematical function as Equation (1.1), but can perform many different types of operations and have many different functionalities. Do not worry about this for now. We will discuss Octave functions later in the book.

Notice that the interpreter keeps track of the number of commands that have been entered and shows this at the prompt.