# GNU Octave Beginner's Guide

Formats:

save 15%!

save 37%!

**Free Shipping!**

Also available on: |

- The easiest way to use GNU Octave's power and flexibility for data analysis
- Work with GNU Octave's interpreter – declare and control mathematical objects like vectors and matrices
- Rationalize your scripts and control program flow
- Extend GNU Octave and implement your own functionality
- Get to know the vast built-in functionality that GNU Octave has to offer
- Build your own GNU Octave toolbox package to solve complex problems
- Learn Octave the simple way, with real-world examples and plenty of screenshots provided throughout the book

### Book Details

**Language :**English

**Paperback :**280 pages [ 235mm x 191mm ]

**Release Date :**June 2011

**ISBN :**1849513325

**ISBN 13 :**9781849513326

**Author(s) :**Jesper Schmidt Hansen

**Topics and Technologies :**All Books, Big Data and Business Intelligence, Beginner's Guides, Open Source

## Table of Contents

PrefaceChapter 1: Introducing GNU Octave

Chapter 2: Interacting with Octave: Variables and Operators

Chapter 3: Working with Octave: Functions and Plotting

Chapter 4: Rationalizing: Octave Scripts

Chapter 5: Extensions: Write Your Own Octave Functions

Chapter 6: Making Your Own Package: A Poisson Equation Solver

Chapter 7: More Examples: Data Analysis

Chapter 8: Need for Speed: Optimization and Dynamically Linked Functions

Pop Quiz Answers

Index

- Chapter 1: Introducing GNU Octave
- So what is GNU Octave?
- Applications
- Limitations of Octave
- Octave and MATLAB
- The Octave community
- Installing Octave
- Windows
- GNU/Linux
- Building Octave from the source under GNU/Linux
- Time for action – building Octave from source
- Checking your installation with peaks
- Time for action – testing with peaks
- Customizing Octave
- Time for action – creating an Octave home directory under Windows
- Creating your first .octaverc file
- Time for action – editing the .octaverc file
- More on .octaverc
- Installing additional packages
- Time for action – installing additional packages
- Uninstalling a package
- Getting help
- The behaviour of the Octave command prompt
- Summary

- Chapter 2: Interacting with Octave: Variables and Operators
- Simple numerical variables
- Accessing and changing array elements
- More examples
- Time for action – manipulating arrays
- Complex variables
- Text variables
- Higher-dimensional arrays
- Structures and cell arrays
- Structures
- Time for action – instantiating a structure
- Accessing structure fields
- Cell arrays
- Time for action – instantiating a cell array
- Getting information
- Time for action – using whos
- Size, rows, columns, and length
- Identifying the variable type
- Deleting variables from the workspace
- A few things that make life easier
- Basic arithmetic
- Addition and subtraction
- Time for action – doing addition and subtraction operations
- Matrix multiplication
- Time for action – doing multiplication operations
- Element-by-element, power, and transpose operations
- Operators for structures and cell arrays
- Solving linear equation systems: left and right division
- Time for action – doing left and right division
- Basic arithmetic for complex variables
- Summary of arithmetic operators
- Comparison operators and precedence rules
- Precedence rules
- Time for action – working with precedence rules
- A few hints
- Summary

- Chapter 3: Working with Octave: Functions and Plotting
- Octave functions
- Mathematical functions
- Time for action – using the cos function
- Polynomials in Octave
- More complicated mathematical functions

- Time for action – putting together mathematical functions
- Helper functions
- Generating random numbers
- min and max
- Sorting arrays
- find, any, and all
- floor, ceil, round, and fix

- Time for action – trying out floor, ceil, round, and fix
- sum and prod
- Absolute values
- Complex input arguments

- Operator functions
- Linear algebra

- Time for action – using Octave for advanced linear algebra
- Polynomials

- Two-dimensional plotting
- Time for action – making your first plot
- plot and set

- Time for action – changing the figure properties
- Adding lines and text to your plot
- Plot styles and colors
- Title and legends
- Ticks
- Grids
- fplot
- Clear the figure window

- Moving on

- Time for action – having multiple graphs in the same figure
- Multiple figure windows
- Subplots

- Time for action – making an inset
- Saving your plot

- Three-dimensional plotting
- Surface plot

- Time for action – making a surface plot
- view and colormap
- Contour plots
- Three-dimensional parametric plots

- Time for action – plotting parametric curves
- Summary

- Chapter 4: Rationalizing: Octave Scripts
- Writing and executing your first script
- Time for action – making your first script
- Improving your script: input and disp
- Time for action – interacting with the user
- Flush please
- Comments
- Very long commands
- Workspace
- For GNU/Linux and MacOS X users

- Statements
- Prime numbers
- Decision making – the if statement
- Interlude: Boolean operators
- Element-wise Boolean operators
- Short-circuit Boolean operators
- Using Boolean operators with an if statement
- Nested statements
- The switch statement
- Loops
- The for statement
- The while and do statements
- Nested loops
- Putting it all together
- Exception handling

- Added flexibility – C style input and output functions
- printf

- Saving your work
- Loading your work
- Functional forms

- Summary

- Chapter 5: Extensions: Write Your Own Octave Functions
- Your first Octave function
- Time for action – programming the minmax function
- Scripts versus functions
- Defining functions at the command prompt
- Writing a function help text
- Checking the function call
- The usage, warning, and error functions
- nargin and nargout
- Writing and applying user-supplied functions
- Using fsolve
- Providing the Jacobian
- Using lsode – dynamical analysis of the Sel'kov model
- Time for action – using lsode for numerical integration
- Inline functions
- More advanced function programming: Monte Carlo integration
- The feval function
- Validating the user-supplied function
- Using quad and trapz for numerical integration
- Vectorized programming
- Time for action – vectorizing the Monte Carlo integrator
- Simple debugging
- Multiple function file
- Summary

- Chapter 6: Making Your Own Package: A Poisson Equation Solver
- The Poisson equation – two examples of heat conduction
- One-dimensional heat conduction
- Two-dimensional heat conduction
- The Poisson equation
- The Dirichlet boundary condition
- Finite differencing
- From finite difference to a linear equation system
- Interlude: Sparse matrices
- Time for action – instantiating a sparse matrix
- Memory usage
- Implementation and package functions
- The coefficient matrix for one dimension
- The coefficient matrix for two dimensions
- The conversion function
- Testing the functions
- The coefficient matrices
- Time for action – using imagesc
- Comparison with analytical solution

- Time for action – comparing with analytical solution
- Two dimensions

- Time for action – solving a two-dimensional Laplace equation
- More examples
- Wrapping everything into one function

- The pois-solv package
- Organizing files into the package directory
- The DESCRIPTION file
- The COPYING file
- The INDEX file
- Building the package
- Limitations and possible extensions to the package

- Summary

- Chapter 7: More Examples: Data Analysis
- Loading data files
- Simple descriptive statistics
- Histogram and moments
- Sample moments
- Comparing data sets
- The correlation coefficient
- The student t-test
- Function fitting
- Polynomial fitting
- Time for action – using polyfit
- Goodness of the fit

- Time for action – calculating the correlation coefficient
- Residual plot

- Non-polynomial fits
- Transforms
- General least squares fitting

- Time for action – using leasqr
- Fourier analysis
- The Fourier transform

- Time for action – using the fft function
- Fourier analysis of currency exchange rate

- Time for action – analysing the exchange rate
- Inverse Fourier transform and data smoothing
- The Butterworth filter

- Time for action – applying a low pass filter
- Summary

- Chapter 8: Need for Speed: Optimization and Dynamically Linked Functions
- A few optimization techniques
- tic, toc
- Time for action – using tic and toc
- Vectorization
- Initialization of variables
- Looping row-wise versus looping column-wise
- Dynamically linked functions
- The DEFUN_DLD function macro
- Time for action – writing a "Hello World" program
- Managing input arguments and outputs
- Time for action – checking user inputs and outputs
- Retrieving the inputs
- Class types
- Functions as input argument
- Optimization with oct-files

- Time for action – revisiting the Sel'kov model
- Summary

- Pop Quiz Answers
- Chapter 2: Interacting with Octave: Variables and Operators
- Working with arrays
- Understanding arrays
- Understanding simple operations
- Understanding precedence rules
- Chapter 3: Working with Octave: Functions and Plotting
- Using simple mathematical functions
- Understanding the plotting options
- Chapter 4: Rationalizing: Octave Scripts
- Understanding disp and input
- Understanding statements and Boolean operators
- Printing with printf
- Chapter 5: Extensions: Write Your Own Octave Functions
- Understanding functions
- Implementing mathematical functions as Octave functions
- Understanding vectorization
- Chapter 6: Making Your Own Package: A Poisson Equation Solver
- Identifying the Poisson equation

### Jesper Schmidt Hansen

### Code Downloads

Download the code and support files for this book.

### Submit Errata

Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.

### Errata

- 18 submitted: last submission 26 Jun 2013** Page 32 | Errata type: Code**

octave:22> Z = [1 -2.3I; 4I 5+6.7I]

Z =

1.0000 + 0.0000i 0.0000 – 2.3000i

0.0000 + 4.0000i 5.0000 + 6.7000i

Be careful! If an array element has non-zero real and imaginary parts, DO LEAVE any blanks (space characters) between the two parts. For example, had we used Z=[1 -2.3I; 4I 5 + 6.7I] in Command 22, the last element would be interpreted as two separate elements (5 and 6.7i). This would lead to dimension mismatch.

It Should be DO NOT LEAVE instead of DO LEAVE in the above sentence.

** Page 53 | Errata type: Code**

The linear system described by Equation (2.6) on page 52 doesn't seem to match its matrix form in Equation (2.8) on page 53:

Seems:

y(2) should be 1 or

2nd equation should equal 3

Additionally,

Octave Command 93 in the 'Time for action' section doesn't seem to match Equations (2.6 or 2.8):

Seems:

A(2,2) should be 2, not -2

** Page 92 | Errata type: Code**

In "Time for action-making an inset", item 5 the command

octave:100> plot(x, f_2, "red","linewidth", 5)

It is missing the "color" before "red".

** Page 45 | Errata type: Typo**

At the top, item b) of quiz that began on page 44, the row command: the "s" is missing. (rows)

** Page 39 | Errata type: Typo**

At the top, in cell array table, the vector [1 0 0] in cell{1,2} is without brackets.

** Page 36 | Errata type: Typo**

In "Time for action – instantiating a structure", item 2, the display of velocity vector, the number 1 is missing.

** Page 145 | Errata type: Code**

In ref (5.2), the second argument of f_2 is y_2 and should be x_2.

** Page 61 | Errata type: Typo**

In paragraph "What just happened?", third line:

"In command 190..."

It's not 190, it's 119.

** Page 30 | Errata type: Language**

The second sentence in the ‘What just happened?’ section, on page 30, is better stated as:

Instead of deleting a column, we could have deleted a row by assigning an empty row vector to row 2 in A, by the following command:

Octave:14> A(2,:)=[]

** Page 57 | Errata type: Layout**

Operator *: In Example column, "ans= ", second row, the 3 and 4 are joint!

Operator /: In Description column, the matrices are in incorrect order. It should be

1 2 * inv(I)

3 4

** Page 51 | Errata type: Code**

Octave Command 84 in the Notes section, on page 51, doesn't seem to work ...

B=[1 2 3; 4 5 6]

B.^2.1 works

B^2.1 gives error

** Page 140 | Errata type: Code**

Command 7> The output for maxa is 3 4 instead of 1 4.

Command 8> The output variables are a b and c and not mina and maxa.

** Page 175 | Errata type: Code**

In equation (6.11) and (6.12), the expressions with the delx^2 in the denominators should have i+1,j subscripts in the third phi terms of the numerators.

In equation (6.13), the first = signs in the second and third equations should be changed to minus signs.

** Page 187 | Errata type: Code**

octave:16> T_0 = 300; but the octave:18> line is using the variable T0.

Also in octave:18> the third expression should read

B(N_grids,:) = T0; not (N_grids,:) = T0 as shown

**Errata type: Typo**

**Page 18:**

Please find the command line in the 4th numbered bullet: **setenv('HOME', 'C:\Document and Settings\GNU Octave\');**

The word **Document** should be changed to **Documents**.

**Page 19:**

Please find the last line in the 1st paragraph: The first instructed the interpreter to set the home directory to **C:\Document and Settings\GNU Octave\**, and the second made it enter that directory.

The word **Document** should be changed to **Documents**.

**Errata type: Typo**

**Page 80:**

Find the word **The determinate** under the "Description" column in the table.

This word should be changed to **Determinant**.

**Errata type: Code**

**Page 97:**

Please find these code lines:**octave:108> set(gca, "xlabel", text("string", "x", "fontsize", 30)octave:109> set(gca, "ylabel", text("string", "y", "fontsize", 30)**

The brackets are missing at the end. These should be changed to the following:

**octave:108> set(gca, "xlabel", text("string", "x", "fontsize", 30)) octave:109> set(gca, "ylabel", text("string", "y", "fontsize", 30))**

**Errata type: Code**

**Page:150**

In the **Step 1** of **''Time for action''** section,

the command **t = linespace(0,50,200); **should read **t = linspace(0,50,200);**

### Sample chapters

You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

- Work with the Octave interpreter
- Declare and control different variable types
- Use arithmetic operations between variables
- Use logical operations
- Control the program flow
- Write scripts
- Implement new functions
- Perform complex data analysis
- Write your own toolbox package
- Optimize your code

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.

This is a practical, step-by-step guide that will help you to quickly become a proficient Octave user. The book is packed with clear examples, screenshots, and code to carry out your data analysis without any problems.

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