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 - editing the .octaverc file


  1. 1. Start Octave if you have not already done so, and open the default editor:

    octave:1> edit
    
    
  2. 2. Copy the following lines into the file and save the file as .octaverc under the Octave home directory if you use Windows, or under the user home directory if you use GNU/Linux. (Without the line numbers, of course.) Alternatively, just use your favorite editor to create the file.

    PS1 (">> ");
    edit mode "async"
    
  3. Exit the editor and restart Octave. Did the appearance of the Octave prompt change? It should look like this

    >>
    

    Note

    Instead of restarting Octave every time you make changes to your setup files, you can type, for example, octave:1> source(".octaverc"). This will read the commands in the .octaverc file.

What just happened?

PS1(">> ") sets the primary prompt string in Octave to the string given. You can set it to anything you may like. To extend the preceding example given previously, PS1("\\#>> ") will keep the command counter before the>> string. You can test which prompt string is your favorite directly from the command prompt, that is, without editing .octaverc. Try, for example, to use \\d and Hello give a command, \\u. In this book, we will stick with the default prompt string, which is \\s:\\#>.

The command edit mode "async" will ensure that when the edit command is given, you can use the Octave prompt without having to close the editor first. This is not default in GNU/Linux.

Finally, note that under Windows, the behavior will be global because we instructed Octave to look for this particular .octaverc file every time Octave is started. Under GNU/Linux, the .octaverc is saved in the user's home directory and will therefore only affect that particular user.

More on .octaverc

The default editor can be set in .octaverc. This can be done by adding the following line into your .octaverc file

edit editor name of the editor

where name of the editor is the editor. You may prefer a notepad if you use Windows, or gedit in GNU/Linux. Again, before adding this change to your .octaverc file, you should test whether it works directly from the Octave prompt.

Later in the book, we will write script and function files. Octave will have to be instructed where to look for these files in order to read them. Octave uses a path hierarchy when searching for files, and it is important to learn how to instruct Octave to look for the files in certain directories. I recommend that you create a new directory in your home directory (Octave home directory in Windows) named octave. You can then place your Octave files in this directory and let the interpreter search for them here.

Let us first create the directory. It is easiest simply to enter Octave and type the following:

octave1:> cd ~/
octave2:> mkdir octave

It should be clear what these commands do. Now type this:

octave3:> edit .octaverc

Add the following line into the .octaverc file:

addpath("~/octave");

Save the file, exit the editor, and restart Octave, or use source(".octaverc"). At the Octave prompt, type the following:

octave1:> path

You should now see that the path ~/octave/ is added to the search path. Under Windows, this path will be added for all users. The path list can be long, so you may need to scroll down (using the arrow key) to see the whole list. When you reach the end of the list, you can hit the Q key to return to Octave's command prompt.