Scientific Computing with MATLAB
Total Page:16
File Type:pdf, Size:1020Kb
Scientific Computing with MATLAB Paul Gribble Fall, 2015 Tue 12th Jan, 2016, 06:25 ii About the author Paul Gribble is a Professor at the University of Western Ontario1 in London, On- tario Canada. He has a joint appointment in the Department of Psychology2 and the Department of Physiology & Pharmacology3 in the Schulich School of Medicine & Dentistry4. He is a core member of the Brain and Mind Institute5 and a core member of the Graduate Program in Neuroscience6. He received his PhD from McGill University7 in 1999. His lab webpage can be found here: http://www.gribblelab.org This document This document was typeset using LATEX version 3.14159265-2.6-1.40.16 (TeX Live 2015). These notes in their entirety, including the LATEX source, are avail- able on GitHub8 here: https://github.com/paulgribble/SciComp License This work is licensed under a Creative Commons Attribution 4.0 International License9. The full text of the license can be viewed here: http://creativecommons.org/licenses/by/4.0/legalcode iii LINKS Links 1http://www.uwo.ca 2http://psychology.uwo.ca 3http://www.schulich.uwo.ca/physpharm/ 4http://www.schulich.uwo.ca 5http://www.uwo.ca/bmi 6http://www.schulich.uwo.ca/neuroscience/ 7http://www.mcgill.ca 8https://github.com 9http://creativecommons.org/licenses/by/4.0/ LINKS iv Preface These are the course notes associated with my graduate-level course called Sci- entific Computing (Psychology 9040a), given inthe Department of Psychol- ogy10 at the University of Western Ontario11. The goal of the course is to provide you with skills in scientific computing: tools and techniques that you can use in your own scientific research. We will focus on learning to think about experi- ments and data in a computational framework, and we will learn to implement specific algorithms using a high-level programming language (MATLAB). Learn- ing how to program will significantly enhance your ability to conduct scientific research today and in the future. Programming skills will provide you with the ability to go beyond what is available in pre-packaged analysis tools, and code your own custom data processing, analysis and visualization pipelines. The course (and these notes) are organized around using MATLAB12 (Math- Works13), a high-level language and interactive environment for scientific com- puting. MATLAB version R2015b is used throughout. Chapters 13 and 14 on integrating ordinary differential equations and simulat- ing dynamical systems are based on notes from a previous course that were de- veloped in collaboration with Dr. Dinant Kistemaker (VU Amsterdam). For a much more detailed, comprehensive book on MATLAB and all of its func- tionality, I can recommend: Mastering MATLAB by Duane Hanselman & Bruce Littlefield. Pearson Eduction, Inc., publishing as Prentice Hall, Upper Saddle River, NJ, 2012. ISBN: 978-0-13- 601330-3. v LINKS vi They have a website associated with their book here: http://www.masteringmatlab.com vii LINKS Conventions in the notes Web links are given in blue font and are clickable when viewing this document as a .pdf on your computer, for example: Gribble Lab14. Code snippets are shown in monospaced font within a box with a gray backround such as this: >> disp('Hello, world!') Hello, world! Also note that Chapter headings, sections and subsections in the Table of Con- tents are hyperlinks in this .pdf, so that if you click on them you are taken di- rectly to the appropriate page. Comments Do you have ideas about how to improve these notes? Please get in touch, send me an email at [email protected] Links 10http://psychology.uwo.ca 11http://www.uwo.ca 12http://www.mathworks.com/products/matlab/ 13http://www.mathworks.com 14http://www.gribblelab.org Contents 1 What is computer code? 1 1.1 High-level vs low-level languages ............... 1 1.2 Interpreted vs compiled languages .............. 5 2 Digital representation of data 9 2.1 Binary ............................... 9 2.2 Hexadecimal ........................... 10 2.3 Floating point values ...................... 12 2.4 ASCII ............................... 15 Exercises .................................. 18 3 Basic data types, operators & expressions 21 3.1 Expressions ........................... 21 3.2 Operators ............................. 24 3.3 Variables ............................. 25 3.4 Basic Data Types ......................... 31 3.5 Special values .......................... 38 3.6 Getting help ........................... 40 3.7 Script M-files ........................... 42 3.8 MATLAB path .......................... 43 Exercises .................................. 46 4 Complex data types 49 4.1 Arrays ............................... 49 4.2 Matrices .............................. 57 viii ix CONTENTS 4.3 Multidimensional arrays .................... 69 4.4 Cell arrays ............................ 73 4.5 Structures ............................ 74 Exercises .................................. 80 5 Control flow 83 5.1 Loops ............................... 83 5.2 Conditionals ........................... 86 5.3 Switch statements ........................ 88 5.4 Pause, break, continue, return . 88 Exercises .................................. 92 6 Functions 95 6.1 Encapsulation .......................... 95 6.2 Function specification ..................... 97 6.3 Variable scope .......................... 98 6.4 Anonymous functions . 100 Exercises ..................................101 7 Input & Output 107 7.1 Plain text files . 108 7.2 Binary files ............................111 7.3 ASCII or binary? . 112 8 Debugging, profiling and speedy code 115 8.1 Debugging ............................115 8.2 Timing and Profiling . 119 8.3 Speedy Code . 126 8.4 MATLAB Coder . 137 9 Parallel programming 143 9.1 What is parallel computing? . 143 9.2 Multi-threading . 144 CONTENTS x 9.3 Symmetric Multiprocessing (SMP) . 144 9.4 Hyperthreading . 146 9.5 Clusters ..............................147 9.6 Grids ...............................148 9.7 GPU Computing . 149 9.8 Types of Parallel problems . 150 9.9 MATLAB .............................150 9.10 Shell scripts . 151 Exercises ..................................152 10 Graphical displays of data 157 Exercises ..................................159 11 Signals, sampling & filtering 173 11.1 Time domain representation of signals . 173 11.2 Frequency domain representation of signals . 174 11.3 Fast Fourier transform (FFT) . 175 11.4 Sampling .............................175 11.5 Power spectra . 178 11.6 Power Spectral Density . 180 11.7 Decibel scale . 182 11.8 Spectrogram . 183 11.9 Filtering ..............................183 11.10 Quantization . 194 11.11 Sources of noise . 195 Exercises ..................................197 12 Optimization & gradient descent 209 12.1 Analytic Approaches . 212 12.2 Numerical Approaches . 215 12.3 Optimization in MATLAB . 220 Exercises ..................................226 xi CONTENTS 13 Integrating ODEs & simulating dynamical systems 229 13.1 What is a dynamical system? . 229 13.2 Why make models? . 231 13.3 Modelling Dynamical Systems . 233 13.4 Integrating Differential Equations in MATLAB . 236 13.5 The power of modelling and simulation . 239 13.6 Simulating Motion of a Two-Joint Arm . 239 13.7 Lorenz Attractor . 242 Exercises ..................................250 14 Modelling Action Potentials 255 14.1 The Neuron Model . 256 14.2 Passive Properties . 257 14.3 Sodium Channels (Na) . 258 14.4 Potassium Channels (K) . 260 14.5 Summary .............................261 14.6 MATLAB code . 261 Exercises ..................................266 15 Basic statistical tests 269 15.1 Probability Distributions . 269 15.2 Hypothesis Tests . 273 15.3 Resampling techniques . 279 Exercises ..................................284 CONTENTS xii 1 What is computer code? What is a computer program? What is code? What is a computer language? A computer program is simply a series of instructions that the computer executes, one after the other. An instruction is a single command. A program is a series of instructions. Code is another way of referring to a single instruction or a series of instructions (a program). 1.1 High-level vs low-level languages The CPU15 (central processing unit) chip(s) that sit on the motherboard16 of your computer is the piece of hardware that actually executes instructions. A CPU only understands a relatively low-level language called machine code17. Often machine code is generated automatically by translating code written in assem- bly language18, which is a low-level programming language19 that has a rela- tively direcy relationship to machine code (but is more readable by a human). A utility program called an assembler20 is what translates assembly language code into machine code. In this course we will be learning how to program in MATLAB, which is a high- level programming language21. The “high-level” refers to the fact that the lan- guage has a strong abstraction from the details of the computer (the details of the machine code). A “strong abstraction” means that one can operate using high-level instructions without having to worry about the low-level details of carrying out those instructions. An analogy is motor skill learning. A high-level language for human action 1 CHAPTER 1. WHAT IS COMPUTER CODE? 2 might be drive your car to the grocery store and buy apples. A low-level version of this might be something like: (1) walk to your car; (2) open the door; (3) start the ignition; (4) put the transmission into Drive; (5) step on the gas pedal, and so on. An even lower-level description might