Michael Paluszek and

Stephanie Thomas

MATLAB Recipes

A Problem-Solution Approach

1st ed. 2015

Michael Paluszek

Princeton, New Jersey, USA

Stephanie Thomas

Princeton Junction, New Jersey, USA

ISBN 978-1-4842-0560-0

e-ISBN 978-1-4842-0559-4

DOI 10.1007/978-1-4842-0559-4

Library of Congress Control Number: 2015955885

© Apress 2015

MATLAB Recipes: A Problem-Solution Approach

Managing Director: Welmoed Spahr

Lead Editor: Steve Anglin

Technical Reviewer: Jonah Lissner

Editorial Board: Steve Anglin, Louise Corrigan, Jonathan Gennick, Robert Hutchinson, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing, Steve Weiss

Coordinating Editor: Mark Powers

Copy Editor: Kim Burton-Weisman

Compositor: SPi Global

Indexer: SPi Global

Artist: SPi Global

For information on translations, please e-mail [email protected] , or visit www.apress.com .

Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales .

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected] , or visit www.springeronline.com . Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com/9781484205600 . For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/ . Readers can also access source code at SpringerLink in the Supplementary Material section for each chapter.

Standard Apress copyright included in front matter section of manuscript.

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

Printed on acid-free paper

For Marilyn and Matt

Introduction

Writing software has become part of the job description for nearly every professional engineer and engineering student. While there are many excellent prebuilt software applications for engineers, almost everyone can benefit from writing custom software for their own problems.

MATLAB® has origins for that very reason. Scientists that needed to do operations on matrices used numerical software written in FORTRAN. At the time, using computer languages required the user to go through the write-compile-link-execute process that was time-consuming and error-prone. MATLAB presented a scripting language that allowed the user to solve many problems with a few lines of a script that executed instantaneously. MATLAB had built-in visualization tools that helped the user better understand the results. Writing MATLAB was a lot more productive and fun than writing FORTRAN.

MATLAB has grown greatly since its origins. The power of the basic MATLAB software has grown dramatically, and hundreds of MATLAB libraries are now available, both commercially and as open source. MATLAB is so sophisticated that most new users only use a fraction of its power.

The goal of MATLAB Recipes is to help all users harness the power of MATLAB. This book has two parts. The first part, Chapters 1 through 5 , gives a framework that you can use to write high-quality MATLAB code that you, your colleagues, and possibly your customers, can utilize. We cover coding practices, graphics, debugging and other topics in a problem-solution format. You can read these sections from cover to cover or just look at the recipes that interest you and use them in your latest MATLAB code.

The second part of the book, Chapters 6 through 12 , shows complete MATLAB applications revolving around the control of dynamical systems and simulation. Each chapter provides the technical background for the topic, ideas on how you can write a simple control system, and an example of how you might simulate the system. Each system is implemented in a MATLAB script supported by a number of MATLAB functions. Each chapter also highlights a general MATLAB topic, like graphics or writing graphical user interfaces (GUIs). We have deliberately made the control systems simple so that the reader won’t need a course in control theory to get results. Control experts can easily take the script and implement their own ideas. We cover a number of areas, ranging from chemical processes to satellites—and we apologize if we didn’t write an example for your area of interest!

The book has something for everyone—from the MATLAB novice to the authors of commercial MATLAB packages. We learned new things writing this book! We hope that you enjoy the book and look forward to seeing your software that it inspires.

Contents

  1. Part I: Coding in MATLAB
    1. Chapter 1:​ Coding Handbook
      1. MATLAB Language Primer
        1. A Brief Introduction to MATLAB
        2. Everything Is a Matrix
        3. Strings Are Simple
        4. Use Strict Data Structures
        5. Cell Arrays Hold Anything and Everything
        6. Optimize Your Code with Logical Arrays
        7. Use Persistent and Global Scope to Minimize Data Passing
        8. Understanding Unique MATLAB Operators and Keywords
        9. Harnessing the Power of Multiple Inputs and Outputs
        10. Use Function Handles for Efficiency
        11. Advanced Data Types
      2. Primer Recipes
      3. 1-1.​ Creating Function Help
        1. Problem
        2. Solution
        3. How It Works
      4. 1-2.​ Locating Directories for Data Storage
        1. Problem
        2. Solution
        3. How It Works
      5. 1-3.​ Loading Binary Data from a File
        1. Problem
        2. Solution
        3. How It Works
      6. 1-4.​ Command-Line File Interaction
        1. Problem
        2. Solution
        3. How It Works
      7. 1-5.​ Using a MEX File to Link to an External Library
        1. Problem
        2. Solution
        3. How It Works
      8. 1-6.​ Protect Your IP with Parsed Files
        1. Problem
        2. Solution
        3. How It Works
      9. Writing to a Text File
        1. Problem
        2. Solution
        3. How It Works
      10. Summary
    2. Chapter 2:​ MATLAB Style
      1. 2-1.​ Developing Your Own MATLAB Style Guidelines
        1. Problem
        2. Solution
        3. How It Works
      2. 2-2.​ Writing Good Function Help
        1. Problem
        2. Solution
        3. How It Works
      3. 2-3.​ Overloading Functions and Utilizing varargin
        1. Problem
        2. Solution
        3. How It Works
      4. 2-4.​ Adding Built-in Inputs and Outputs to Functions
        1. Problem
        2. Solution
        3. How It Works
      5. 2-5.​ Smart Structuring of Scripts
        1. Problem
        2. Solution
        3. How It Works
      6. 2-6.​ Implementing MATLAB Command-Line Help for Folders
        1. Problem
        2. Solution
        3. How It Works
      7. 2-7.​ Publishing Code into Technical Reports
        1. Problem
        2. Solution
        3. How It Works
      8. 2-8.​ Integrating Toolbox Documentation into the MATLAB Help System
        1. Problem
        2. Solution
        3. How It Works
      9. 2-9.​ Structuring a Toolbox
        1. Problem
        2. Solution
        3. How It Works
      10. Summary
    3. Chapter 3:​ Visualization
      1. 3-1.​ Plotting Data Interactively from the MATLAB Desktop
        1. Problem
        2. Solution
        3. How It Works
      2. 3-2.​ Incrementally Annotate a Plot
        1. Problem
        2. Solution
        3. How It Works
      3. 3-3.​ Create a Custom Plot Page with Subplot
        1. Problem
        2. Solution
        3. How It Works
      4. 3-4.​ Create a Plot Page with Custom-Sized Axes
        1. Problem
        2. Solution
        3. How It Works
      5. 3-5.​ Plotting with Dates
        1. Problem
        2. Solution
        3. How It Works
      6. 3-6.​ Generating a Color Distribution
        1. Problem
        2. Solution
        3. How It Works
      7. 3-7.​ Visualizing Data over 2D or 3D Grids
        1. Problem
        2. Solution
        3. How It Works
      8. 3-8.​ Generate 3D Objects Using Patch
        1. Problem
        2. Solution
        3. How It Works
      9. 3-9.​ Working with Light Objects
        1. Problem
        2. Solution
        3. How It Works
      10. 3-10.​ Programmatically​ Setting the Camera Properties
        1. Problem
        2. Solution
        3. How It Works
      11. 3-11.​ Display an Image
        1. Problem
        2. Solution
        3. How It Works
      12. 3-12.​ Adding a Watermark
        1. Problem
        2. Solution
        3. How It Works
      13. Summary
    4. Chapter 4:​ Interactive Graphics
      1. 4-1.​ Creating a Simple Animation
        1. Problem
        2. Solution
        3. How It Works
      2. 4-2.​ Playing Back an Animation
        1. Problem
        2. Solution
        3. How It Works
      3. 4-3.​ Animate Line Objects
        1. Problem
        2. Solution
        3. How It Works
      4. 4-4.​ Implementation of a uicontrol Button
        1. Problem
        2. Solution
        3. How It Works
      5. 4-5.​ Display Status of a Running Simulation or Loop
        1. Problem
        2. Solution
        3. How It Works
      6. 4-6.​ Create a Custom GUI with GUIDE
        1. Problem
        2. Solution
        3. How It Works
      7. 4-7.​ Build a MATLAB App from Your GUI
        1. Problem
        2. Solution
        3. How It Works
      8. Summary
    5. Chapter 5:​ Testing and Debugging
      1. 5-1.​ Creating a Unit Test
        1. Problem
        2. Solution
        3. How It Works
      2. 5-2.​ Running a Test Suite
        1. Problem
        2. Solution
        3. How It Works
      3. 5-3.​ Setting Verbosity Levels in Tests
        1. Problem
        2. Solution
        3. How It Works
      4. 5-4.​ Create a Logging Function to Display Data
        1. Problem
        2. Solution
        3. How It Works
      5. 5-5.​ Generating and Tracing MATLAB Errors and Warnings
        1. Problem
        2. Solution
        3. How It Works
      6. 5-6.​ Testing Custom Errors and Warnings
        1. Problem
        2. Solution
        3. How It Works
      7. 5-7.​ Testing Generation of Figures
        1. Problem
        2. Solution
        3. How It Works
      8. Summary
  2. Part II: Applications
    1. Chapter 6:​ The Double Integrator
      1. 6-1.​ Writing Equations for the Double Integrator Model
        1. Problem
        2. Solution
        3. How It Works
      2. 6-2.​ Creating a Fixed-Step Numerical Integrator
        1. Problem
        2. Solution
        3. How It Works
      3. 6-3.​ Implement a Discrete Proportional-Derivative Controller
        1. Problem
        2. Solution
        3. How It Works
      4. 6-4.​ Simulate the Double Integrator with Digital Control
        1. Problem
        2. Solution
        3. How It Works
      5. 6-5.​ Create Time Axes with Reasonable Time Units
        1. Problem
        2. Solution
        3. How It Works
      6. 6-6.​ Create Figures with Multiple Subplots
        1. Problem
        2. Solution
        3. How It Works
      7. Summary
    2. Chapter 7:​ Robotics
      1. 7-1.​ Creating a Dynamic Model of the SCARA Robot
        1. Problem
        2. Solution
        3. How It Works
      2. 7-2.​ Customize a Visualization Function for the Robot
        1. Problem
        2. Solution
        3. How It Works
      3. 7-3.​ Using Numerical Search for Robot Inverse Kinematics
        1. Problem
        2. Solution
        3. How It Works
      4. 7-4.​ Developing a Control System for the Robot
        1. Problem
        2. Solution
        3. How It Works
      5. 7-5.​ Simulating the Controlled Robot
        1. Problem
        2. Solution
        3. How It Works
      6. Summary
    3. Chapter 8:​ Electric Motors
      1. 8-1.​ Modeling a Three-Phase Brushless Permanent Magnet Motor
        1. Problem
        2. Solution
        3. How It Works
      2. 8-2.​ Controlling the Motor
        1. Problem
        2. Solution
        3. How It Works
      3. 8-3.​ PulseWidth Modulation of the Switches
        1. Problem
        2. Solution
        3. How It Works
      4. 8-4.​ Simulating the Controlled Motor
        1. Problem
        2. Solution
        3. How It Works
      5. Summary
    4. Chapter 9:​ Fault Detection
      1. Introduction
      2. 9-1.​ Modeling an Air Turbine
        1. Problem
        2. Solution
        3. How It Works
      3. 9-2.​ Building a Detection Filter
        1. Problem
        2. Solution
        3. How It Works
      4. 9-3.​ Simulating the Fault Detection System
        1. Problem
        2. Solution
        3. How It Works
      5. 9-4.​ Building a GUI for the Detection Filter Simulation
        1. Problem
        2. Solution
        3. How It Works
      6. Summary
    5. Chapter 10:​ Chemical Processes
      1. 10-1.​ Modeling the Chemical Mixing Process
        1. Problem
        2. Solution
        3. How It Works
      2. 10-2.​ Sensing the pH of the Chemical Process
        1. Problem
        2. Solution
        3. How It Works
      3. 10-3.​ Controlling the Effl uent pH
        1. Problem
        2. Solution
        3. How It Works
      4. 10-4.​ Simulating the Controlled pH Process
        1. Problem
        2. Solution
        3. How It Works
      5. Summary
    6. Chapter 11:​ Aircraft
      1. 11-1.​ Creating a Dynamic Model of an Aircraft
        1. Problem
        2. Solution
        3. How It Works
      2. 11-2.​ Finding the Equilibrium Controls for an Aircraft Using Numerical Search
        1. Problem
        2. Solution
        3. How It Works
      3. 11-3.​ Designing a Control System for an Aircraft
        1. Problem
        2. Solution
        3. How It Works
      4. 11-4.​ Plotting a 3D Trajectory for an Aircraft
        1. Problem
        2. Solution
        3. How It Works
      5. 11-5.​ Simulating the Controlled Aircraft
        1. Problem
        2. Solution
        3. How It Works
      6. Summary
    7. Chapter 12:​ Spacecraft
      1. 12-1.​ Creating a Dynamic Model of the Spacecraft
        1. Problem
        2. Solution
        3. How It Works
      2. 12-2.​ Computing Angle Errors from Quaternions
        1. Problem
        2. Solution
        3. How It Works
      3. 12-3.​ Simulating the Controlled Spacecraft
        1. Problem
        2. Solution
        3. How It Works
      4. 12-4.​ Performing Batch Runs of a Simulation
        1. Problem
        2. Solution
        3. How It Works
      5. Summary
  3. Index

About the Authors and About the Technical Reviewer

About the Authors

A335353_1_En_BookFrontmatter_Fig2_HTML.gif

Michael Paluszek is president of Princeton Satellite Systems, Inc. (PSS), in Plainsboro, New Jersey. Mr. Paluszek founded PSS in 1992 to provide aerospace consulting services. He used MATLAB to develop the control system and simulation for the Indostar-1 geosynchronous communications satellite, resulting in the launch of PSS’s first commercial MATLAB toolbox, the Spacecraft Control Toolbox, in 1995. Since then he has developed toolboxes and software packages for aircraft, submarines, robotics, and fusion propulsion, resulting in PSS’s current extensive product line. He is currently leading a US Army research contract for precision attitude control of small satellites and IS working with the Princeton Plasma Physics Laboratory on a compact nuclear fusion reactor for energy generation and propulsion.

Prior to founding PSS, Mr. Paluszek was an engineer at GE Astro Space in East Windsor, NJ. At GE, he designed the Global Geospace Science Polar despun platform control system and led the design of the GPS IIR attitude control system, the Inmarsat-3 attitude control systems, and the Mars Observer delta-V control system, leveraging MATLAB for control design. Mr. Paluszek also worked on the attitude determination system for the DMSP meteorological satellites. Mr. Paluszek flew communication satellites on more than 12 satellite launches, including the GSTAR III recovery, the first transfer of a satellite to an operational orbit using electric thrusters. At Draper Laboratory, Mr. Paluszek worked on the Space Shuttle program, the international space station, and submarine navigation. His space station work included the design of the control moment gyroscope, based control systems for attitude control.

Mr. Paluszek received his bachelor’s degree in electrical engineering, and a master’s and engineer’s degree in aeronautics and astronautics from the Massachusetts Institute of Technology. He is author of numerous papers and has over a dozen US patents.

A335353_1_En_BookFrontmatter_Fig1_HTML.gif

Stephanie Thomas is vice president of Princeton Satellite Systems, Inc., in Plainsboro, New Jersey. She received her bachelor’s and master’s degrees in aeronautics and astronautics from the Massachusetts Institute of Technology in 1999 and 2001. Ms. Thomas was introduced to PSS’s Spacecraft Control Toolbox for MATLAB during a summer internship in 1996 and has been using MATLAB for aerospace analysis ever since. She built a simulation of a lunar transfer vehicle in C++, LunarPilot, during the same internship. In her nearly 20 years of MATLAB experience, she has developed many software tools, including the Solar Sail Module for the Spacecraft Control Toolbox; a proximity satellite operations toolbox for the Air Force; collision monitoring Simulink blocks for the Prisma satellite mission; and launch vehicle analysis tools in MATLAB and Java, to name a few. She has developed novel methods for space situation assessment, such as a numeric approach to assessing the general rendezvous problem between any two satellites implemented in both MATLAB and C++.

Ms. Thomas has contributed to PSS’s Attitude and Orbit Control textbook, featuring examples using the Spacecraft Control Toolbox, and written many software users’ guides. She has conducted SCT training for engineers from diverse locales, such as Australia, Canada, Brazil, and Thailand, and has performed MATLAB consulting for NASA, the US Air Force, and the European Space Agency.

About the Technical Reviewer

Jonah Lissner is a research scientist advancing PhD and DSc programs, scholarships, applied projects, and academic journal publications in theoretical physics, power engineering, complex systems, metamaterials, geophysics, and computation theory. He has strong cognitive ability in empiricism and scientific reason for hypothesis building, theory learning, mathematical and axiomatic modeling, and testing for abstract problem solving. His dissertations, research publications and projects, CV, journals, blog, novels, and system are listed at http://Lissnerresearch.weebly.com .

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset