Cover
Essential MATLAB
for Engineers and Scientists
Copyright
Preface
Acknowledgments
Part 1 Essentials
1 Introduction
1.1 Using MATLAB
1.1.1 Arithmetic
1.1.2 Variables
1.1.3 Mathematical functions
1.1.4 Functions and commands
1.1.5 Vectors
1.1.6 Linear equations
1.1.7 Tutorials and demos
1.2 The desktop
1.2.1 Using the Editor and running a script
1.2.2 Help, Publish and View
1.2.3 Symbolics in live scripts
1.2.4 APPS
1.2.5 Additional features
1.3 Sample program
1.3.1 Cut and paste
1.3.2 Saving a program: Script files
Current directory
Running a script from the Current Folder browser
1.3.3 A program in action
Summary
Exercises
1.A Supplementary material
2 MATLAB Fundamentals
2.1 Variables
2.1.1 Case sensitivity
2.2 The workspace
2.2.1 Adding commonly used constants to the workspace
2.3 Arrays: Vectors and matrices
2.3.1 Initializing vectors: Explicit lists
2.3.2 Initializing vectors: The colon operator
2.3.3 The linspace and logspace functions
2.3.4 Transposing vectors
2.3.5 Subscripts
2.3.6 Matrices
2.3.7 Capturing output
2.3.8 Structure plan
2.4 Vertical motion under gravity
2.5 Operators, expressions, and statements
2.5.1 Numbers
2.5.2 Data types
2.5.3 Arithmetic operators
2.5.4 Operator precedence
2.5.5 The colon operator
2.5.6 The transpose operator
2.5.7 Arithmetic operations on arrays
2.5.8 Expressions
2.5.9 Statements
2.5.10 Statements, commands, and functions
2.5.11 Formula vectorization
2.6 Output
2.6.1 The disp statement
2.6.2 The format command
2.6.3 Scale factors
2.7 Repeating with for
2.7.1 Square roots with Newton's method
2.7.2 Factorials!
2.7.3 Limit of a sequence
2.7.4 The basic for construct
2.7.5 for in a single line
2.7.6 More general for
2.7.7 Avoid for loops by vectorizing!
2.8 Decisions
2.8.1 The one-line if statement
2.8.2 The if-else construct
2.8.3 The one-line if-else statement
2.8.4 elseif
2.8.5 Logical operators
2.8.6 Multiple ifs versus elseif
2.8.7 Nested ifs
2.8.8 Vectorizing ifs?
2.8.9 The switch statement
2.9 Complex numbers
Summary
Exercises
2.A Supplementary material
3 Program Design and Algorithm Development
3.1 The program design process
3.1.1 The projectile problem
3.2 Programming MATLAB functions
3.2.1 Inline objects: Harmonic oscillators
3.2.2 MATLAB function: y = f(x)
Summary
Exercises
4 MATLAB Functions and Data Import-Export Utilities
4.1 Common functions
4.2 Importing and exporting data
4.2.1 The load and save commands
4.2.2 Exporting text (ASCII) data
4.2.3 Importing text (ASCII) data
4.2.4 Exporting binary data
4.2.5 Importing binary data
Summary
Exercises
5 Logical Vectors
5.1 Examples
5.1.1 Discontinuous graphs
5.1.2 Avoiding division by zero
5.1.3 Avoiding infinity
5.1.4 Counting random numbers
5.1.5 Rolling dice
5.2 Logical operators
5.2.1 Operator precedence
5.2.2 Danger
5.2.3 Logical operators and vectors
5.3 Subscripting with logical vectors
5.4 Logical functions
5.4.1 Using any and all
5.5 Logical vectors instead of elseif ladders
Summary
Exercises
5.A Supplementary material
6 Matrices and Arrays
6.1 Matrices
6.1.1 A concrete example
6.1.2 Creating matrices
6.1.3 Subscripts
6.1.4 Transpose
6.1.5 The colon operator
6.1.6 Duplicating rows and columns: Tiling
6.1.7 Deleting rows and columns
6.1.8 Elementary matrices
6.1.9 Specialized matrices
6.1.10 Using MATLAB functions with matrices
6.1.11 Manipulating matrices
6.1.12 Array (element-by-element) operations on matrices
6.1.13 Matrices and for
6.1.14 Visualization of matrices
6.1.15 Vectorizing nested fors: loan repayment tables
6.1.16 Multi-dimensional arrays
6.2 Matrix operations
6.2.1 Matrix multiplication
6.2.2 Matrix exponentiation
6.3 Other matrix functions
6.4 Population growth: Leslie matrices
6.5 Markov processes
6.5.1 A random walk
6.6 Linear equations
6.6.1 MATLAB's solution
6.6.2 The residual
6.6.3 Over-determined systems
6.6.4 Under-determined systems
6.6.5 Ill conditioning
6.6.6 Matrix division
6.7 Sparse matrices
Summary
Exercises
7 Function M-files
7.1 Example: Newton's method again
7.2 Basic rules
7.2.1 Subfunctions
7.2.2 Private functions
7.2.3 P-code files
7.2.4 Improving M-file performance with the Profiler
7.3 Function handles
7.4 Command/function duality
7.5 Function name resolution
7.6 Debugging M-files
7.6.1 Debugging a script
7.6.2 Debugging a function
7.7 Recursion
Summary
Exercises
7.A Supplementary material
8 Loops
8.1 Determinate repetition with for
8.1.1 Binomial coefficient
8.1.2 Update processes
8.1.3 Nested fors
8.2 Indeterminate repetition with while
8.2.1 A guessing game
8.2.2 The while statement
8.2.3 Doubling time of an investment
8.2.4 Prime numbers
8.2.5 Projectile trajectory
8.2.6 break and continue
8.2.7 Menus
Summary
Exercises
9 MATLAB Graphics
9.1 Basic 2-D graphs
9.1.1 Labels
9.1.2 Multiple plots on the same axes
9.1.3 Line styles, markers and color
9.1.4 Axis limits
axes and axis?
9.1.5 Multiple plots in a figure: subplot
9.1.6 figure, clf and cla
9.1.7 Graphical input
9.1.8 Logarithmic plots
9.1.9 Polar plots
9.1.10 Plotting rapidly changing mathematical functions: fplot
9.1.11 The Property Editor
9.2 3-D plots
9.2.1 plot3
9.2.2 Animated 3-D plots with comet3
9.2.3 Mesh surfaces
9.2.4 Contour plots
9.2.5 Cropping a surface with NaNs
9.2.6 Visualizing vector fields
9.2.7 Visualization of matrices
9.2.8 Rotation of 3-D graphs
9.3 Handle Graphics
9.3.1 Getting handles
9.3.2 Graphics object properties and how to change them
9.3.3 A vector of handles
9.3.4 Graphics object creation functions
9.3.5 Parenting
9.3.6 Positioning figures
9.4 Editing plots
9.4.1 Plot edit mode
9.4.2 Property Editor
9.5 Animation
9.5.1 Animation with Handle Graphics
9.6 Color etc.
9.6.1 Colormaps
9.6.2 Color of surface plots
9.6.3 Truecolor
9.7 Lighting and camera
9.8 Saving, printing and exporting graphs
9.8.1 Saving and opening figure files
9.8.2 Printing a graph
9.8.3 Exporting a graph
Summary
Exercises
10 Vectors as Arrays and Other Data Structures
10.1 Update processes
10.1.1 Unit time steps
10.1.2 Non-unit time steps
10.1.3 Using a function
10.1.4 Exact solution
10.2 Frequencies, bar charts and histograms
10.2.1 A random walk
10.2.2 Histograms
10.3 Sorting
10.3.1 Bubble Sort
10.3.2 MATLAB's sort
10.4 Structures
10.5 Cell arrays
10.5.1 Assigning data to cell arrays
10.5.2 Accessing data in cell arrays
10.5.3 Using cell arrays
10.5.4 Displaying and visualizing cell arrays
10.6 Classes and objects
Summary
11 Errors and Pitfalls
11.1 Syntax errors
11.1.1 Incompatible vector sizes
11.1.2 Name hiding
11.2 Logic errors
11.3 Rounding error
Summary
Chapter exercises
Part 2 Applications
12 Dynamical Systems
12.1 Cantilever beam
12.2 Electric current
12.3 Free fall
12.4 Projectile with friction
Summary
Exercises
13 Simulation
13.1 Random number generation
13.1.1 Seeding rand
13.2 Spinning coins
13.3 Rolling dice
13.4 Bacteria division
13.5 A random walk
13.6 Traffic flow
13.7 Normal (Gaussian) random numbers
Summary
Exercises
14 Introduction to Numerical Methods
14.1 Equations
14.1.1 Newton's method
14.1.1.1 Complex roots
14.1.2 The Bisection method
14.1.3 fzero
14.1.4 roots
14.2 Integration
14.2.1 The Trapezoidal rule
14.2.2 Simpson's rule
14.2.3 quad
14.3 Numerical differentiation
14.3.1 diff
14.4 First-order differential equations
14.4.1 Euler's method
14.4.2 Example: Bacteria growth
14.4.3 Alternative subscript notation
14.4.4 A predictor-corrector method
14.5 Linear ordinary differential equations (LODEs)
14.6 Runge-Kutta methods
14.6.1 A single differential equation
14.6.2 Systems of differential equations: Chaos
14.6.3 Passing additional parameters to an ODE solver
14.7 A partial differential equation
14.7.1 Heat conduction
14.8 Complex variables and conformal mapping
Joukowski airfoil
14.9 Other numerical methods
Summary
Exercises
15 Signal Processing
15.1 Harmonic analysis
15.2 Fast Fourier Transform (FFT)
16 SIMULINK Toolbox
16.1 Mass-spring-damper dynamic system
16.2 Bouncing ball dynamic system
16.3 The van der Pol oscillator
16.4 The Duffing oscillator
Exercises
17 Symbolics Toolbox
17.1 Algebra
17.1.1 Polynomials
17.1.2 Vectors
17.1.3 Matrices
17.2 Calculus
17.3 Laplace and Z transforms
17.4 Generalized functions*
17.5 Differential equations
17.6 Implementation of funtool, MuPAD and help
17.6.1 The funtool
17.6.2 The MuPAD notebook* and Symbolic help
Exercises
APPENDIX
A Syntax: Quick Reference
A.1 Expressions
A.2 Function M-files
A.3 Graphics
A.4 if and switch
A.5 for and while
A.6 Input/output
A.7 load/save
A.8 Vectors and matrices
APPENDIX
B Operators
APPENDIX
C Command and Function: Quick Reference
C.1 General-purpose commands
C.1.1 Managing variables and the workspace
C.1.2 Files and the operating system
C.1.3 Controlling the Command Window
C.1.4 Starting and quitting MATLAB
C.2 Logical functions
C.3 MATLAB programming tools
C.3.1 Interactive input
C.4 Matrices
C.4.1 Special variables and constants
C.4.2 Time and date
C.4.3 Matrix manipulation
C.4.4 Specialized matrices
C.5 Mathematical functions
C.6 Matrix functions
C.7 Data analysis
C.8 Polynomial functions
C.9 Function functions
C.10 Sparse matrix functions
C.11 Character string functions
C.12 File I/O functions
C.13 2D graphics
C.14 3D graphics
C.15 General
APPENDIX
D Solutions to Selected Exercises
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 11
Chapter 13
Chapter 14
Index
Back Cover