GNU Octave.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
GNU Octave A high-level interactive language for numerical computations Edition 3 for Octave version 3.0.0 July 2007 John W. Eaton David Bateman Søren Hauberg Copyright c 1996, 1997, 1999, 2000, 2001, 2002, 2005, 2006, 2007 John W. Eaton. This is the third edition of the Octave documentation, and is consistent with version 3.0.0 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 How You Can Contribute to Octave ............................... 3 Distribution ..................................................... 4 1 A Brief Introduction to Octave ............. 5 1.1 Running Octave ............................................. 5 1.2 Simple Examples ............................................ 5 1.2.1 Creating a Matrix ...................................... 5 1.2.2 Matrix Arithmetic ...................................... 6 1.2.3 Solving Linear Equations ................................ 6 1.2.4 Integrating Differential Equations ........................ 6 1.2.5 Producing Graphical Output ............................ 7 1.2.6 Editing What You Have Typed .......................... 7 1.2.7 Help and Documentation ................................ 7 1.3 Conventions ................................................ 8 1.3.1 Fonts .................................................. 8 1.3.2 Evaluation Notation .................................... 8 1.3.3 Printing Notation ....................................... 9 1.3.4 Error Messages ......................................... 9 1.3.5 Format of Descriptions .................................. 9 1.3.5.1 A Sample Function Description...................... 9 1.3.5.2 A Sample Command Description ................... 10 1.3.5.3 A Sample Variable Description ..................... 10 2 Getting Started........................... 11 2.1 Invoking Octave from the Command Line .................... 11 2.1.1 Command Line Options ................................ 11 2.1.2 Startup Files .......................................... 14 2.2 Quitting Octave ............................................ 14 2.3 Commands for Getting Help ................................ 15 2.4 Command Line Editing ..................................... 17 2.4.1 Cursor Motion......................................... 17 2.4.2 Killing and Yanking.................................... 18 2.4.3 Commands For Changing Text.......................... 18 2.4.4 Letting Readline Type For You ......................... 19 2.4.5 Commands For Manipulating The History ............... 19 2.4.6 Customizing readline ................................. 22 2.4.7 Customizing the Prompt ............................... 22 2.4.8 Diary and Echo Commands............................. 23 2.5 How Octave Reports Errors ................................. 24 2.6 Executable Octave Programs ................................ 25 2.7 Comments in Octave Programs .............................. 26 ii GNU Octave 3 Data Types .............................. 29 3.1 Built-in Data Types ........................................ 29 3.1.1 Numeric Objects....................................... 30 3.1.2 Missing Data .......................................... 30 3.1.3 String Objects ......................................... 30 3.1.4 Data Structure Objects ................................ 31 3.1.5 Cell Array Objects..................................... 31 3.2 User-defined Data Types.................................... 31 3.3 Object Sizes ............................................... 31 4 Numeric Data Types...................... 33 4.1 Matrices ................................................... 34 4.1.1 Empty Matrices ....................................... 36 4.2 Ranges .................................................... 37 4.3 Integer Data Types......................................... 38 4.3.1 Integer Arithmetic ..................................... 39 4.4 Bit Manipulations .......................................... 40 4.5 Logical Values ............................................. 42 4.6 Predicates for Numeric Objects.............................. 43 5 Strings ................................... 45 5.1 Creating Strings............................................ 46 5.2 Comparing Strings ......................................... 48 5.3 Manipulating Strings ....................................... 49 5.4 String Conversions ......................................... 54 5.5 Character Class Functions .................................. 58 6 Data Containers .......................... 61 6.1 Data Structures ............................................ 61 6.1.1 Structure Arrays....................................... 63 6.1.2 Creating Structures .................................... 65 6.1.3 Manipulating Structures................................ 67 6.1.4 Processing Data in Structures .......................... 68 6.2 Cell Arrays ................................................ 69 6.2.1 Creating Cell Array .................................... 70 6.2.2 Indexing Cell Arrays ................................... 71 6.2.3 Cell Arrays of Strings .................................. 72 6.2.4 Processing Data in Cell Arrays ......................... 73 6.3 Comma Separated Lists..................................... 75 7 Variables ................................. 77 7.1 Global Variables ........................................... 77 7.2 Persistent Variables ........................................ 78 7.3 Status of Variables ......................................... 80 7.4 Summary of Built-in Variables .............................. 84 7.5 Defaults from the Environment .............................. 86 iii 8 Expressions .............................. 89 8.1 Index Expressions .......................................... 89 8.2 Calling Functions .......................................... 91 8.2.1 Call by Value.......................................... 92 8.2.2 Recursion ............................................. 93 8.3 Arithmetic Operators ....................................... 93 8.4 Comparison Operators ...................................... 95 8.5 Boolean Expressions ........................................ 96 8.5.1 Element-by-element Boolean Operators .................. 96 8.5.2 Short-circuit Boolean Operators ........................ 97 8.6 Assignment Expressions .................................... 98 8.7 Increment Operators ...................................... 100 8.8 Operator Precedence ...................................... 101 9 Evaluation .............................. 103 9.1 Calling a Function by its Name............................. 103 9.2 Evaluation in a Different Context........................... 105 10 Statements ............................. 107 10.1 The if Statement ........................................ 107 10.2 The switch Statement ................................... 109 10.2.1 Notes for the C programmer .......................... 110 10.3 The while Statement..................................... 111 10.4 The do-until Statement ................................. 112 10.5 The for Statement ....................................... 112 10.5.1 Looping Over Structure Elements ..................... 113 10.6 The break Statement..................................... 114 10.7 The continue Statement ................................. 115 10.8 The unwind_protect Statement........................... 116 10.9 The try Statement ....................................... 116 10.10 Continuation Lines ...................................... 117 11 Functions and Script Files............... 119 11.1 Defining Functions ....................................... 119 11.2 Multiple Return Values ................................... 121 11.3 Variable-length Argument Lists ........................... 123 11.4 Variable-length Return Lists .............................. 124 11.5 Returning From a Function ............................... 125 11.6 Default Arguments ....................................... 126 11.7 Function Files ........................................... 126 11.7.1 Manipulating the load path........................... 127 11.7.2 Subfunctions ........................................ 129 11.7.3 Overloading and Autoloading ......................... 129 11.7.4 Function Locking .................................... 131 11.8 Script Files .............................................. 132 11.9 Function Handles, Inline Functions, and Anonymous Functions .......................................................... 133 iv GNU Octave 11.9.1 Function Handles .................................... 133 11.9.2 Anonymous Functions................................ 134 11.9.3 Inline Functions ..................................... 135 11.10 Commands ............................................