You are previewing Essential MATLAB for Engineers and Scientists Fourth Edition.
O'Reilly logo
Essential MATLAB for Engineers and Scientists Fourth Edition

Book Description

The essential guide to MATLAB as a problem solving tool This text presents MATLAB both as a mathematical tool and a programming language, giving a concise and easy to master introduction to its potential and power. The fundamentals of MATLAB are illustrated throughout with many examples from a wide range of familiar scientific and engineering areas, as well as from everyday life. The new edition has been updated to include coverage of Symbolic Math and SIMULINK. It also adds new examples and applications, and uses the most recent release of Matlab.

Table of Contents

  1. Copyright
  2. Preface
    1. Acknowledgments
  3. 1. Essentials
    1. 1. Introduction
      1. 1.1. Using MATLAB
        1. 1.1.1. Arithmetic
        2. 1.1.2. Variables
        3. 1.1.3. Mathematical functions
        4. 1.1.4. Functions and commands
        5. 1.1.5. Vectors
        6. 1.1.6. Linear equations
        7. 1.1.7. Demo
        8. 1.1.8. Help
        9. 1.1.9. Additional features
      2. 1.2. The MATLAB Desktop
      3. 1.3. Sample Program
        1. 1.3.1. Cut and paste
        2. 1.3.2. Saving a program: script files
          1. Current Directory
          2. Running a script from the Current Directory browser
        3. 1.3.3. A program in action
      4. Summary
      5. Chapter Exercises
    2. 2. MATLAB Fundamentals
      1. 2.1. Variables
        1. 2.1.1. Case sensitivity
      2. 2.2. The Workspace
        1. 2.2.1. Adding commonly used constants to the workspace
      3. 2.3. Arrays: Vectors and Matrices
        1. 2.3.1. Initializing vectors: Explicit lists
        2. 2.3.2. Initializing vectors: The colon operator
        3. 2.3.3. The linspace function
        4. 2.3.4. Transposing vectors
        5. 2.3.5. Subscripts
        6. 2.3.6. Matrices
        7. 2.3.7. Capturing output
      4. 2.4. Vertical Motion Under Gravity
      5. 2.5. Operators, Expressions, and Statements
        1. 2.5.1. Numbers
        2. 2.5.2. Data types
        3. 2.5.3. Arithmetic operators
        4. 2.5.4. Operator precedence
        5. 2.5.5. The colon operator
        6. 2.5.6. The transpose operator
        7. 2.5.7. Arithmetic operations on arrays
        8. 2.5.8. Expressions
        9. 2.5.9. Statements
        10. 2.5.10. Statements, commands, and functions
        11. 2.5.11. Formula vectorization
      6. 2.6. Output
        1. 2.6.1. The disp statement
        2. 2.6.2. The format command
        3. 2.6.3. Scale factors
      7. 2.7. Repeating with for
        1. 2.7.1. Square roots with Newton’s method
        2. 2.7.2. Factorials!
        3. 2.7.4. The basic for construct
        4. 2.7.5. for in a single line
        5. 2.7.6. More general for
        6. 2.7.7. Avoid for loops by vectorizing!
      8. 2.8. Decisions
        1. 2.8.1. The one-line if statement
        2. 2.8.2. The if-else construct
        3. 2.8.3. The one-line if-else statement
        4. 2.8.4. elseif
        5. 2.8.5. Logical operators
        6. 2.8.6. Multiple ifs versus elseif
        7. 2.8.7. Nested ifs
        8. 2.8.8. Vectorizing ifs?
        9. 2.8.9. The switch statement
      9. 2.9. Complex Numbers
      10. 2.10. More on Input and Output
        1. 2.10.1. fprintf
        2. 2.10.2. Output to a disk file with fprintf
        3. 2.10.3. General file I/O
        4. 2.10.4. Saving and loading data
      11. 2.11. Odds and Ends
        1. 2.11.1. Variables, functions, and scripts with the same name
        2. 2.11.2. The input statement
        3. 2.11.3. Shelling out to the operating system
        4. 2.11.4. More Help functions
      12. 2.12. Programming Style
      13. Summary
      14. Chapter Exercises
    3. 3. Program Design and Algorithm Development
      1. 3.1. The Program Design Process
        1. 3.1.1. The projectile problem
      2. 3.2. Structure Plan Examples
        1. 3.2.1. Quadratic equation
      3. 3.3. Structured Programming with Functions
      4. Summary
      5. Chapter Exercises
    4. 4. MATLAB Functions and Data Import–Export Utilities
      1. Common Functions
      2. 4.2. Importing and Exporting Data
        1. 4.2.1. The load and save commands
        2. 4.2.2. Exporting text (ASCII) data
        3. 4.2.3. Importing text (ASCII) data
        4. 4.2.4. Exporting and importing binary data
        5. 4.2.5. The Import Wizard
          1. Importing ASCII data
          2. Importing binary data
        6. 4.2.6. *Low-level file I/O functions
          1. Writing binary data
          2. Reading binary data
          3. Changing binary data
        7. 4.2.7. *Other import/export functions
      3. Summary
      4. Chapter Exercises
    5. 5. Logical Vectors
      1. 5.1. Examples
        1. 5.1.1. Discontinuous graphs
        2. 5.1.2. Avoiding division by zero
        3. 5.1.3. Avoiding infinity
        4. 5.1.4. Counting random numbers
        5. 5.1.5. Rolling dice
      2. 5.2. Logical Operators
        1. 5.2.1. Operator precedence
        2. 5.2.2. Incorrect conversion
        3. 5.2.3. Logical operators and vectors
      3. 5.3. Subscripting with Logical Vectors
      4. 5.4. Logical Functions
        1. 5.4.1. Using any and all
      5. 5.5. Logical Vectors Instead of elseif Ladders
      6. Summary
      7. Chapter Exercises
    6. 6. Matrices of Numbers and Arrays of Strings
      1. 6.1. Matrices
        1. 6.1.1. A concrete example
        2. 6.1.2. Creating matrices
        3. 6.1.3. Subscripts
        4. 6.1.4. The transpose operator
        5. 6.1.5. The colon operator
        6. 6.1.6. Duplicating rows and columns: Tiling
        7. 6.1.7. Deleting rows and columns
        8. 6.1.8. Elementary matrices
        9. 6.1.9. Specialized matrices
        10. 6.1.10. Using MATLAB functions with matrices
        11. 6.1.11. Manipulating matrices
        12. 6.1.12. Array (element-by-element) operations on matrices
        13. 6.1.13. Matrices and for
        14. 6.1.14. Visualization of matrices
        15. 6.1.15. Vectorizing nested fors: Loan repayment tables
        16. 6.1.16. Multidimensional arrays
      2. 6.2. Matrix Operations
        1. 6.2.1. Multiplication
        2. 6.2.2. Exponentiation
      3. 6.3. Other Matrix Functions
      4. 6.4. *Strings
        1. 6.4.1. Input
        2. 6.4.2. Strings as arrays
        3. 6.4.3. String concatenation
        4. 6.4.4. ASCII codes: double and char
        5. 6.4.5. String display with fprintf
        6. 6.4.6. Comparing strings
        7. 6.4.7. Other string functions
      5. 6.5. *Two-Dimensional Strings
      6. 6.6. *eval and Text Macros
        1. 6.6.1. Error trapping with eval and lasterr
        2. 6.6.2. eval with try...catch
      7. Summary
      8. Chapter Exercises
    7. 7. Introduction to Graphics
      1. 7.1. Basic Two-Dimensional Graphs
        1. 7.1.1. Labels
        2. 7.1.2. Multiple plots on the same axes
        3. 7.1.3. Line styles, markers, and color
        4. 7.1.4. Axis limits
          1. Axes and axis?
        5. 7.1.5. Multiple plots in a figure: subplot
        6. 7.1.6. figure, clf, and cla
        7. 7.1.7. Graphical input
        8. 7.1.8. Logarithmic plots
        9. 7.1.9. Polar plots
        10. 7.1.10. Plotting rapidly changing mathematical functions: fplot
        11. 7.1.11. The Property Editor
      2. 7.2. Three-Dimensional Plots
        1. 7.2.1. The plot3 function
        2. 7.2.2. Animated 3D plots with the comet3 function
        3. 7.2.3. Mesh surfaces
        4. 7.2.4. Contour plots
        5. 7.2.5. Cropping a surface with NaNs
        6. 7.2.6. Visualizing vector fields
        7. 7.2.7. Matrix visualization
        8. 7.2.8. 3D graph rotation
        9. 7.2.9. Other graphics functions
      3. Summary
      4. Chapter Exercises
    8. 8. Loops
      1. 8.1. Determinate Repetition with for
        1. 8.1.1. Binomial coefficient
        2. 8.1.2. Update processes
        3. 8.1.3. Nested fors
      2. 8.2. Indeterminate Repetition with while
        1. 8.2.1. A guessing game
        2. 8.2.2. The while statement
        3. 8.2.3. Doubling time of an investment
        4. 8.2.4. Prime numbers
        5. 8.2.5. Projectile trajectory
        6. 8.2.6. break
        7. 8.2.7. Menus
      3. Summary
      4. Chapter Exercises
    9. 9. Errors and Pitfalls
      1. 9.1. Syntax Errors
        1. 9.1.1. Incompatible vector sizes
        2. 9.1.2. Name hiding
      2. 9.2. Logic Errors
      3. 9.3. Rounding Error
      4. Summary
      5. Chapter Exercises
    10. 10. Function M-files
      1. 10.1. Inline Objects: Harmonic Oscillators
      2. 10.2. Function M-Files: Newton’s Method Revisited
      3. 10.3. Basic Rules
        1. 10.3.1. Subfunctions
        2. 10.3.2. Private functions
        3. 10.3.3. P-code files
        4. 10.3.4. Improving M-file performance with the profiler
      4. 10.4. Function Handles
      5. 10.5. Command/Function Duality
      6. 10.6. Function Name Resolution
      7. 10.7. Debugging M-Files
        1. 10.7.1. Debugging a script
        2. 10.7.2. Debugging a function
      8. 10.8. Recursion
      9. Summary
      10. Chapter Exercises
    11. 11. Vectors as Arrays and *Advanced Data Structures
      1. 11.1. Update Processes
        1. 11.1.1. Unit time steps
        2. 11.1.2. Non–unit time steps
        3. 11.1.3. Using a function
        4. 11.1.4. Exact solution
      2. 11.2. Frequencies, Bar Charts, and Histograms
        1. 11.2.1. A random walk
        2. 11.2.2. Histograms
      3. 11.3. *Sorting
        1. 11.3.1. Bubble Sort
        2. 11.3.2. MATLAB’s sort
      4. 11.4. *Structures
      5. 11.5. *Cell Arrays
        1. 11.5.1. Assigning data to cell arrays
        2. 11.5.2. Accessing data in cell arrays
        3. 11.5.3. Using cell arrays
        4. 11.5.4. Displaying and visualizing cell arrays
      6. 11.6. *Classes and Objects
      7. Summary
    12. 12. *More Graphics
      1. 12.1. Handle Graphics
        1. 12.1.1. Getting handles
        2. 12.1.2. Changing graphics object properties
        3. 12.1.3. A vector of handles
        4. 12.1.4. Graphics object creation functions
        5. 12.1.5. Parenting
        6. 12.1.6. Positioning figures
      2. 12.2. Editing Plots
        1. 12.2.1. Plot edit mode
        2. 12.2.2. Property Editor
      3. 12.3. Animation
        1. 12.3.1. Animation with Handle Graphics
      4. 12.4. Colormaps
        1. 12.4.1. Surface plot color
        2. 12.4.2. Truecolor
      5. 12.5. Lighting and Camera
      6. 12.6. Saving, Printing, and Exporting Graphs
        1. 12.6.1. Saving and opening figure files
        2. 12.6.2. Printing a graph
        3. 12.6.3. Exporting a graph
      7. Summary
      8. Chapter Exercises
    13. 13. *Graphical User Interfaces (GUIs)
      1. 13.1. Basic Structure of a GUI
      2. 13.2. A First Example: Getting the Time
      3. 13.3. Newton’s Method Yet Again
      4. 13.4. Axes on a GUI
      5. 13.5. Adding Color to a Button
      6. Summary
  4. 2. Applications
    1. 14. Dynamical Systems
      1. 14.1. Cantilever Beam
      2. 14.2. Electric Current
      3. 14.3. Free Fall
      4. 14.4. Projectile with Friction
      5. Summary
      6. Chapter Exercises
    2. 15. Simulation
      1. 15.1. Random Number Generation
        1. 15.1.1 . Seeding rand
      2. 15.2. Flipping Coins
      3. 15.3. Rolling Dice
      4. 15.4. Bacterium Division
      5. 15.5. A Random Walk
      6. 15.6. Traffic Flow
      7. 15.7. Normal (Gaussian) Random Numbers
      8. Summary
      9. Chapter Exercises
    3. 16. *More Matrices
      1. 16.1. Leslie Matrices: Population Growth
      2. 16.2. Markov Processes
        1. 16.2.1. A random walk
      3. 16.3. Linear Equations
        1. 16.3.1. MATLAB’s solution
        2. 16.3.2. The residual
        3. 16.3.3. Overdetermined systems
        4. 16.3.4. Underdetermined systems
        5. 16.3.5. Ill-conditioned systems
        6. 16.3.6. Matrix division
      4. 16.4. Sparse Matrices
      5. Summary
      6. Chapter Exercises
    4. 17. *Introduction to Numerical Methods
      1. 17.1. Equations
        1. 17.1.1. Newton’s method
          1. Complex roots
        2. 17.1.2. The Bisection method
        3. 17.1.3. The fzero and roots functions
      2. 17.2. Integration
        1. 17.2.1. The Trapezoidal rule
        2. 17.2.2. Simpson’s rule
        3. 17.2.3. The quad function
      3. 17.3. Numerical Differentiation
        1. 17.3.1. The diff function
      4. 17.4. First-Order Differential Equations
        1. 17.4.1. Euler’s method
        2. 17.4.2. Example: Bacteria colony growth
        3. 17.4.3. Alternative subscript notation
        4. 17.4.4. A predictor-corrector method
      5. 17.5. Linear Ordinary Differential Equations
      6. 17.6. Runge-Kutta Methods
        1. 17.6.1. A single differential equation
        2. 17.6.2. Systems of differential equations: Chaos
        3. 17.6.3. Passing additional parameters to an ODE solver
      7. 17.7. A Partial Differential Equation
        1. 17.7.1. Heat conduction
      8. 17.8. Other Numerical Methods
      9. Summary
      10. Chapter Exercises
  5. A. Syntax: Quick Reference
    1. A.1. Expressions
    2. A.2. Function M-Files
    3. A.3. Graphics
    4. A.4. if AND switch
    5. A.5. for AND while
    6. A.6. Input/Output
    7. A.7. Load/Save
    8. A.8. Vectors and Matrices
  6. B. Operators
  7. C. Command and Function: Quick Reference
    1. C.1. General-Purpose Commands
      1. C.1.1. Managing variables and the workspace
      2. C.1.2. Files and the operating system
      3. C.1.3. Controlling the Command Window
      4. C.1.4. Starting and quitting MATLAB
    2. C.2. Logical Functions
    3. C.3. MATLAB Programming Tools
      1. C.3.1. Interactive input
    4. C.4. Matrices
      1. C.4.1. Special variables and constants
      2. C.4.2. Time and date
      3. C.4.3. Matrix manipulation
      4. C.4.4. Specialized matrices
    5. C.5. Mathematical Functions
    6. C.6. Matrix Functions
    7. C.7. Data Analysis
    8. C.8. Polynomial Functions
    9. C.9. Function Functions
    10. C.10. Sparse Matrix Functions
    11. C.11. Character String Functions
    12. C.12. File I/O Functions
    13. C.13. 2D Graphics
    14. C.14. 3D Graphics
    15. C.15. General
  8. D. ASCII Character Codes
  9. E. Solutions to Selected Exercises
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 7
    7. Chapter 8
    8. Chapter 9
    9. Chapter 10
    10. Chapter 15
    11. Chapter 16
    12. Chapter 17