The Freemat Primer Page 1 of 218 Table of Contents About the Authors
Total Page:16
File Type:pdf, Size:1020Kb
The Freemat 4.0 Primer By Gary Schafer Timothy Cyders First Edition August 2011 The Freemat Primer Page 1 of 218 Table of Contents About the Authors......................................................................................................................................5 Acknowledgements...............................................................................................................................5 User Assumptions..................................................................................................................................5 How This Book Was Put Together........................................................................................................5 Licensing...............................................................................................................................................6 Using with Freemat v4.0 Documentation .............................................................................................6 Topic 1: Working with Freemat.................................................................................................................7 Topic 1.1: The Main Screen - Ver 4.0..................................................................................................7 Topic 1.1.1: The File Browser Section...........................................................................................10 Topic 1.1.2: The History Section....................................................................................................10 Topic 1.1.3: The Variables Section.................................................................................................11 Topic 1.1.4: The Debug Section.....................................................................................................11 Topic 1.1.5: The Working Directory...............................................................................................11 Topic 1.2: Setting up Freemat.............................................................................................................12 Topic 1.2.1: Setting the Working Directory Using the cd Command............................................13 Topic 1.2.2: Changing the Working Directory in Windows..........................................................18 Topic 1.3: Setting the Path List..........................................................................................................19 Topic 1.4: The Command Window....................................................................................................21 Topic 1.4.1: Seeing the Results (or Not)........................................................................................22 Topic 1.4.2: How Many Decimal Points Do You Want?...............................................................23 Topic 1.4.3: Understanding Variables............................................................................................23 Topic 1.4.3.1: Variable Types....................................................................................................24 Topic 1.4.3.2: Binary Types......................................................................................................26 Topic 1.4.3.3: Displaying Binary Numbers..............................................................................26 Topic 1.4.3.4: Calculating with Binary Numbers.....................................................................26 Topic 1.5: Strings................................................................................................................................27 Topic 1.5.1: Creating a String........................................................................................................27 Topic 1.5.2: Concatenating Strings................................................................................................28 Topic 1.6: Built-In Variables..............................................................................................................29 Topic 1.7: Using Built-In Functions...................................................................................................30 Topic 2: Working with Math....................................................................................................................32 Topic 2.1: Basic Math Operations......................................................................................................32 Topic 2.2: Precedence.........................................................................................................................32 Topic 2.3: Sum, Products, Cumulative Sums & Products, and Factorials........................................33 Topic 2.4: Exponentials and Logarithms............................................................................................34 Topic 2.5: Trignometric Functions......................................................................................................37 Topic 3: Matrices & Arrays......................................................................................................................42 Topic 3.1: Understanding Cells, Matrices, Vectors and Indexing.......................................................43 Topic 3.2: Creating a Sequential Array..............................................................................................45 Topic 3.3: Creating a Random Array..................................................................................................46 Topic 3.4: Viewing a Matrix Value.....................................................................................................48 Topic 3.5: Matrix Math.......................................................................................................................48 The Freemat Primer Page 2 of 218 Topic 3.5.1: Matrix Addition.........................................................................................................49 Topic 3.5.2: Matrix Subtraction.....................................................................................................49 Topic 3.5.3: Matrix Multiplication................................................................................................50 Topic 3.5.4: Matrix Division.........................................................................................................51 Topic 3.5.4.1: Dividing a Matrix by a Single Number.............................................................51 Topic 3.5.4.2: Dividing a Matrix by Another Matrix................................................................51 Topic 3.5.4.3: Calculating the Inverse of a Matrix...................................................................52 Topic 3.5.5: Element-wise Matrix Math........................................................................................53 Topic 4: Scripts & Functions...................................................................................................................56 Topic 4.1: The Freemat Editor.............................................................................................................57 Topic 4.2: Creating a Script................................................................................................................57 Topic 4.3: Running a Script................................................................................................................61 Topic 4.4: Creating & Using a Function............................................................................................61 Topic 4.5: Improving a Function's Utility..........................................................................................64 Topic 4.5.1: Checking Function Inputs..........................................................................................64 Topic 4.5.2: Using Element-Wise Math for Functions...................................................................65 Topic 4.6: What Was I Thinking? -or- Comment Your Functions and Scripts...................................66 Topic 4.7: The Anonymous Function..................................................................................................67 Topic 4.8: Program Inputs and Printing Text......................................................................................68 Topic 4.8.1: The printf Function....................................................................................................68 Topic 4.8.2: Printing Numbers.......................................................................................................70 Topic 4.8.3: Printing Special Characters.......................................................................................71 Topic 4.9: The input Function............................................................................................................74 Topic 4.10: Inputting Data from ASCII Text Files..............................................................................77 Topic 4.11: The File Read & Write Functions.....................................................................................79 Topic 5: Flow Control..............................................................................................................................81 Topic 5.1: For Loops..........................................................................................................................81 Topic 5.2: Comparison / Equality Operators......................................................................................84 Topic 5.3: While Loops......................................................................................................................85