GNU Octave Free Your Numbers
Total Page:16
File Type:pdf, Size:1020Kb
GNU Octave A high-level interactive language for numerical computations Edition 4 for Octave version 4.0.1 March 2015 Free Your Numbers John W. Eaton David Bateman Søren Hauberg Rik Wehbring Copyright c 1996, 1997, 1999, 2000, 2001, 2002, 2005, 2006, 2007, 2011, 2013, 2015 John W. Eaton. This is the fourth edition of the Octave documentation, and is consistent with version 4.0.1 of Octave. Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modified versions of this manual under the con- ditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. Permission is granted to copy and distribute translations of this manual into another lan- guage, under the same conditions as for modified versions. Portions of this document have been adapted from the gawk, readline, gcc, and C library manuals, published by the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301{1307, USA. i Table of Contents Preface:::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 Acknowledgements ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 Citing Octave in Publications :::::::::::::::::::::::::::::::::::::::::::::::::::::: 5 How You Can Contribute to Octave :::::::::::::::::::::::::::::::::::::::::::::::: 5 Distribution:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 6 1 A Brief Introduction to Octave ::::::::::::::::::::::::::: 7 1.1 Running Octave::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 7 1.2 Simple Examples:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 7 1.2.1 Elementary Calculations:::::::::::::::::::::::::::::::::::::::::::::::::: 7 1.2.2 Creating a Matrix :::::::::::::::::::::::::::::::::::::::::::::::::::::::: 8 1.2.3 Matrix Arithmetic ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 8 1.2.4 Solving Systems of Linear Equations:::::::::::::::::::::::::::::::::::::: 8 1.2.5 Integrating Differential Equations :::::::::::::::::::::::::::::::::::::::: 9 1.2.6 Producing Graphical Output :::::::::::::::::::::::::::::::::::::::::::: 10 1.2.7 Editing What You Have Typed:::::::::::::::::::::::::::::::::::::::::: 10 1.2.8 Help and Documentation :::::::::::::::::::::::::::::::::::::::::::::::: 10 1.3 Conventions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 11 1.3.1 Fonts ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 11 1.3.2 Evaluation Notation::::::::::::::::::::::::::::::::::::::::::::::::::::: 11 1.3.3 Printing Notation ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 12 1.3.4 Error Messages:::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 12 1.3.5 Format of Descriptions :::::::::::::::::::::::::::::::::::::::::::::::::: 12 1.3.5.1 A Sample Function Description ::::::::::::::::::::::::::::::::::::: 12 1.3.5.2 A Sample Command Description ::::::::::::::::::::::::::::::::::: 13 2 Getting Started ::::::::::::::::::::::::::::::::::::::::::: 15 2.1 Invoking Octave from the Command Line :::::::::::::::::::::::::::::::::::: 15 2.1.1 Command Line Options ::::::::::::::::::::::::::::::::::::::::::::::::: 15 2.1.2 Startup Files:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 19 2.2 Quitting Octave ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 19 2.3 Commands for Getting Help ::::::::::::::::::::::::::::::::::::::::::::::::: 20 2.4 Command Line Editing :::::::::::::::::::::::::::::::::::::::::::::::::::::: 25 2.4.1 Cursor Motion :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 25 2.4.2 Killing and Yanking ::::::::::::::::::::::::::::::::::::::::::::::::::::: 26 2.4.3 Commands For Changing Text :::::::::::::::::::::::::::::::::::::::::: 27 2.4.4 Letting Readline Type For You:::::::::::::::::::::::::::::::::::::::::: 27 2.4.5 Commands For Manipulating The History ::::::::::::::::::::::::::::::: 28 2.4.6 Customizing readline :::::::::::::::::::::::::::::::::::::::::::::::::: 32 2.4.7 Customizing the Prompt :::::::::::::::::::::::::::::::::::::::::::::::: 32 2.4.8 Diary and Echo Commands ::::::::::::::::::::::::::::::::::::::::::::: 34 2.5 How Octave Reports Errors :::::::::::::::::::::::::::::::::::::::::::::::::: 35 ii GNU Octave 2.6 Executable Octave Programs ::::::::::::::::::::::::::::::::::::::::::::::::: 36 2.7 Comments in Octave Programs::::::::::::::::::::::::::::::::::::::::::::::: 37 2.7.1 Single Line Comments :::::::::::::::::::::::::::::::::::::::::::::::::: 37 2.7.2 Block Comments :::::::::::::::::::::::::::::::::::::::::::::::::::::::: 37 2.7.3 Comments and the Help System ::::::::::::::::::::::::::::::::::::::::: 38 3 Data Types :::::::::::::::::::::::::::::::::::::::::::::::: 39 3.1 Built-in Data Types:::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 39 3.1.1 Numeric Objects :::::::::::::::::::::::::::::::::::::::::::::::::::::::: 42 3.1.2 Missing Data:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 43 3.1.3 String Objects :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 43 3.1.4 Data Structure Objects ::::::::::::::::::::::::::::::::::::::::::::::::: 43 3.1.5 Cell Array Objects :::::::::::::::::::::::::::::::::::::::::::::::::::::: 44 3.2 User-defined Data Types ::::::::::::::::::::::::::::::::::::::::::::::::::::: 44 3.3 Object Sizes ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 44 4 Numeric Data Types ::::::::::::::::::::::::::::::::::::: 47 4.1 Matrices ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 48 4.1.1 Empty Matrices::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 51 4.2 Ranges :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 52 4.3 Single Precision Data Types:::::::::::::::::::::::::::::::::::::::::::::::::: 53 4.4 Integer Data Types :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 54 4.4.1 Integer Arithmetic :::::::::::::::::::::::::::::::::::::::::::::::::::::: 56 4.5 Bit Manipulations ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 57 4.6 Logical Values ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 60 4.7 Promotion and Demotion of Data Types ::::::::::::::::::::::::::::::::::::: 61 4.8 Predicates for Numeric Objects :::::::::::::::::::::::::::::::::::::::::::::: 62 5 Strings ::::::::::::::::::::::::::::::::::::::::::::::::::::: 67 5.1 Escape Sequences in String Constants :::::::::::::::::::::::::::::::::::::::: 67 5.2 Character Arrays :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 68 5.3 Creating Strings ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 69 5.3.1 Concatenating Strings::::::::::::::::::::::::::::::::::::::::::::::::::: 70 5.3.2 Converting Numerical Data to Strings ::::::::::::::::::::::::::::::::::: 73 5.4 Comparing Strings ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 76 5.5 Manipulating Strings::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 77 5.6 String Conversions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 91 5.7 Character Class Functions ::::::::::::::::::::::::::::::::::::::::::::::::::: 96 iii 6 Data Containers ::::::::::::::::::::::::::::::::::::::::: 101 6.1 Structures :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 101 6.1.1 Basic Usage and Examples ::::::::::::::::::::::::::::::::::::::::::::: 101 6.1.2 Structure Arrays ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 105 6.1.3 Creating Structures :::::::::::::::::::::::::::::::::::::::::::::::::::: 106 6.1.4 Manipulating Structures ::::::::::::::::::::::::::::::::::::::::::::::: 109 6.1.5 Processing Data in Structures :::::::::::::::::::::::::::::::::::::::::: 113 6.2 Cell Arrays ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 114 6.2.1 Basic Usage of Cell Arrays ::::::::::::::::::::::::::::::::::::::::::::: 114 6.2.2 Creating Cell Arrays ::::::::::::::::::::::::::::::::::::::::::::::::::: 116 6.2.3 Indexing Cell Arrays ::::::::::::::::::::::::::::::::::::::::::::::::::: 118 6.2.4 Cell Arrays of Strings :::::::::::::::::::::::::::::::::::::::::::::::::: 120 6.2.5 Processing Data in Cell Arrays ::::::::::::::::::::::::::::::::::::::::: 121 6.3 Comma Separated Lists::::::::::::::::::::::::::::::::::::::::::::::::::::: 122 6.3.1 Comma Separated Lists Generated from Cell Arrays:::::::::::::::::::: 123 6.3.2 Comma Separated Lists Generated from Structure Arrays :::::::::::::: 124 7 Variables ::::::::::::::::::::::::::::::::::::::::::::::::: 125 7.1 Global Variables :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 126 7.2 Persistent Variables ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 128 7.3 Status of Variables:::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 129 8 Expressions :::::::::::::::::::::::::::::::::::::::::::::: 137 8.1 Index Expressions::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 137 8.1.1 Advanced Indexing::::::::::::::::::::::::::::::::::::::::::::::::::::: 138 8.2 Calling Functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 141 8.2.1 Call by Value :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 142 8.2.2 Recursion :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 143 8.3 Arithmetic Operators ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 143 8.4 Comparison Operators :::::::::::::::::::::::::::::::::::::::::::::::::::::: 147 8.5