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

Preface

Using a range of very different examples, this beginner's guide will take you through the most important aspects of GNU Octave. The book starts by introducing how you work with mathematical objects like vectors and matrices, demonstrating how to perform simple arithmetic operations on these objects and 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 extend and implement new functionality into GNU Octave, how to make a toolbox package to solve your specific problem, and how to use GNU Octave for complicated data analysis. Finally, it demonstrates how to optimize your code and link GNU Octave with C++ code enabling you to solve even the most computational 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.

What this book covers

Chapter 1, Introducing GNU Octave briefly introduces you to GNU Octave. It explains how you can install GNU Octave and test your installation. This first chapter also discusses how to customize the appearance and the behavior of GNU Octave as well as how you install additional packages.

Chapter 2, Interacting with Octave: Variables and Operators shows you how to interact with GNU Octave through the interactive environment. Learn to instantiate objects of different types, control their values, and perform simple operations on and between them.

Chapter 3, Working with Octave: Functions and Plotting explains GNU Octave functions and shows several examples of the very useful functionalities that come with GNU Octave. In this chapter, you will see how you can perform two- and three-dimensional plotting, control the graph appearance, how to have multiple plots in the same figure window, and much more.

Chapter 4, Rationalizing: Octave Scripts looks at how you can rationalize your work using scripts. It will teach you how to control the programming flow in your script and how to perform loops using different statements. At the end of the chapter, you are shown how you can save your work and load it back into GNU Octave's workspace.

Chapter 5, Extensions: Write Your Own Octave Functions takes a closer look at functions and teaches how you can write your own GNU Octave functions. You will learn how to control and validate user input to the function. The important concept of vectorization is discussed and an example of this is given in the last part of the chapter.

Chapter 6, Making Your Own Package: A Poisson Equation Solver teaches you how to make your own GNU Octave package from a collection of related functions. The package will be able to solve one- and two-dimensional Poisson equations and is therefore relevant for many problems encountered in science and engineering. In this chapter, you will also learn how to work with sparse matrices in GNU Octave.

Chapter 7, More Examples: Data Analysis shows you examples of how GNU Octave can be used for data analysis. These examples range from simple statistics, through data fitting, to Fourier analysis and data smoothing.

Chapter 8, Need for Speed: Optimization and Dynamically Linked Functions discusses how you can optimize your code. This includes vectorization, partial looping, pre-instantiation of variables, and dynamically linked functions. The main part of the chapter shows how to use GNU Octave's C++ library and how to link this to the GNU Octave interactive environment. Special attention is paid to explaining when and when not to consider using dynamically linked functions.

What you need for this book

If you use Windows, you basically only need to have a full version of GNU Octave installed on your computer. For GNU/Linux, you may need a plotting program like gnuplot, a Fortran and C/C++ compiler like gcc, and the GNU make utility; fortunately these are standard packages on almost all GNU/Linux distributions. In Chapter 1, it will be shown how you install GNU Octave under Windows and GNU/Linux.

Who this book is for

This book is intended for anyone interested in scientific computing and data analysis. The reader should have a good knowledge of mathematics and also a basic understanding of programming will be useful, although it is not a prerequisite.

Conventions

In this book, you will find several headings appearing frequently.

To give clear instructions of how to complete a procedure or task, we use:

Time for action - heading

  1. 1. Action 1

  2. 2. Action 2

  3. 3. Action 3

Instructions often need some extra explanation so that they make sense, so they are followed with:

What just happened?

This heading explains the working of tasks or instructions that you have just completed.

You will also find some other learning aids in the book, including:

Pop quiz - heading

These are short multiple-choice questions intended to help you test your own understanding.

Have a go hero - heading

These set practical challenges and give you ideas for experimenting with what you have learned.

You will also find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "For example, the inverse of sine is called asin and the inverse hyperbolic of sine is asinh."

A block of code is set as follows:

# flush the output stream
fflush(stdout);
# Get the number of rows and columns from the user
nr = input("Enter the number of rows in the matrix: ");
nc = input("Enter the number of columns in the matrix: ");

Any command-line input or output is written as follows:

octave:35> projectile = struct("mass", 10.1, "velocity", [1 0 0], "type", "Cannonball");

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Now go to the Octave-Forge web page, find the msh package, and click on Details (to the right of the package name)."

Specific commands entered in Octave are referred to using the relevant command numbers:

octave:5 > A = [1 2 3; 4 5 6]
A =
1 2 3
4 5 6

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to, and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.