Docsity

MATLAB for engineers HOLLY MOORE, Notas de estudo de Matlab

Pré-visualização parcial do texto

Baixe MATLAB for engineers HOLLY MOORE e outras Notas de estudo em PDF para Matlab, somente na Docsity!

This page intentionally left blank

This page intentionally left blank

MATLAB

® for Engineers

Third Edition

HOLLY M OORE Salt Lake Community College Salt Lake City, Utah

Boston • Columbus • Indianapolis • New York San Francisco • Upper Saddle River • Amsterdam Cape Town • Dubai • London • Madrid • Milan Munich • Paris • Montreal • Toronto • Delhi Mexico City • Sao Paulo • Sydney • Hong Kong Seoul • Singapore • Taipei • Tokyo

Contents

ABOUT THIS BOOK XI

  • 1 • ABOUT MATLAB® DEDICATION AND ACKNOWLEDGMENTS XV
    • 1.1 What Is MATLAB®?
    • 1.2 Student Edition of MATLAB®
    • 1.3 How Is MATLAB® Used in Industry?
    • 1.4 Problem Solving in Engineering and Science
  • 2 • MATLAB® ENVIRONMENT
    • 2.1 Getting Started
    • 2.2 MATLAB ® Windows
    • 2.3 Solving Problems with MATLAB®
    • 2.4 Saving Your Work
    • Summary
    • MATLAB ® Summary
    • Key Terms
    • Problems
  • 3 • BUILT-IN MATLAB® FUNCTIONS
    • Introduction
      • 3.1 Using Built-In Functions
      • 3.2 Using the Help Feature
      • 3.3 Elementary Math Functions
      • 3.4 Trigonometric Functions
      • 3.5 Data Analysis Functions
      • 3.6 Random Numbers
      • 3.7 Complex Numbers
      • 3.8 Computational Limitations
      • 3.9 Special Values and Miscellaneous Functions
    • 3.10 Summary vi Contents
    • MATLAB® Summary
    • Key Terms
    • Problems
  • 4 • MANIPULATING MATLAB® MATRICES
    • 4.1 Manipulating Matrices
    • 4.2 Problems with Two Variables
    • 4.3 Special Matrices
    • Summary
    • MATLAB® Summary
    • Key Terms
    • Problems
  • 5 • PLOTTING
    • Introduction
    • 5.1 Two-Dimensional Plots
    • 5.2 Subplots
    • 5.3 Other Types of Two-Dimensional Plots
    • 5.4 Three-Dimensional Plotting
    • 5.5 Editing Plots from the Menu Bar
    • 5.6 Creating Plots from the Workspace Window
    • 5.7 Saving Your Plots
    • Summary
    • MATLAB® Summary
    • Problems
  • 6 • USER-DEFINED FUNCTIONS
    • Introduction
    • 6.1 Creating Function M-Files
    • 6.2 Creating Your Own Toolbox of Functions
    • 6.3 Anonymous Functions and Function Handles
    • 6.4 Function Functions
    • 6.5 Subfunctions
    • Summary
    • MATLAB® Summary
    • Key Terms
    • Problems
  • 7 • USER-CONTROLLED INPUT AND OUTPUT
    • Introduction
    • 7.1 User-Defi ned Input
    • 7.2 Output Options
    • 7.3 Graphical Input
    • 7.4 More Cell Mode Features Contents vii
    • 7.5 Reading and Writing Data from Files
    • 7.6 Debugging Your Code
    • Summary
    • MATLAB ® Summary
    • Key Terms
    • Problems
  • 8 • LOGICAL FUNCTIONS AND SELECTION STRUCTURES
    • Introduction
    • 8.1 Relational and Logical Operators
    • 8.2 Flowcharts and Pseudocode
    • 8.3 Logical Functions
    • 8.4 Selection Structures
    • 8.5 Debugging
    • Summary
    • MATLAB ® Summary
    • Key Terms
    • Problems
  • 9 • REPETITION STRUCTURES
    • Introduction
    • 9.1 For Loops
    • 9.2 While Loops
    • 9.3 Break and Continue
    • 9.4 Midpoint Break Loops
    • 9.5 Nested Loops
    • 9.6 Improving the Effi ciency of Loops
    • Summary
    • Key Terms
    • Problems
  • 10 • MATRIX ALGEBRA
    • Introduction
    • 10.1 Matrix Operations and Functions
    • 10.2 Solutions of Systems of Linear Equations
    • 10.3 Special Matrices
    • Summary
    • MATLAB ® Summary
    • Key Terms
    • Problems
  • 11 • OTHER KINDS OF ARRAYS
    • Introduction
    • 11.1 Data Types
    • 11.2 Multidimensional Arrays
    • 11.3 Character Arrays viii Contents
    • 11.4 Cell Arrays
    • 11.5 Structure Arrays
    • Summary
    • MATLAB® Summary
    • Key Terms
    • Problems
  • 12 • SYMBOLIC MATHEMATICS
    • Introduction
    • 12.1 Symbolic Algebra
    • 12.2 Solving Expressions and Equations
    • 12.3 Symbolic Plotting
    • 12.4 Calculus
    • 12.5 Differential Equations
    • 12.6 Converting Symbolic Expressions to MATLAB® Functions
    • Summary
    • MATLAB® Summary
    • Problems
  • 13 • NUMERICAL TECHNIQUES
    • 13.1 Interpolation
    • 13.2 Curve Fitting
    • 13.3 Using the Interactive Fitting Tools
    • 13.4 Differences and Numerical Differentiation
    • 13.5 Numerical Integration
    • 13.6 Solving Differential Equations Numerically
    • Summary
    • MATLAB® Summary
    • Key Terms
    • Problems
  • 14 • ADVANCED GRAPHICS
    • Introduction
    • 14.1 Images
    • 14.2 Handle Graphics
    • 14.3 Animation
    • 14.4 Other Visualization Techniques
    • 14.5 Introduction to Volume Visualization
    • Summary
    • MATLAB® Summary
      • Key Terms
    • Problems
  • 15 • CREATING GRAPHICAL USER INTERFACES Contents ix
    • Introduction
    • 15.1 A Simple GUI with One User Interaction
      • Interactions—Ready_Aim_Fire 15.2 A Graphical User Interface with Multiple User
    • 15.3 An Improved Ready_Aim_Fire Program
    • 15.4 A Much Better Ready_Aim_Fire Program
    • 15.5 Built-In GUI Templates
    • Summary
    • Key Terms
    • Problems
  • 16 • SIMULINK ® —A BRIEF INTRODUCTION
    • Introduction
    • 16.1 Applications
    • 16.2 Getting Started
    • 16.3 Solving Differential Equations with Simulink®
    • Summary
    • Key Terms
    • Problems - FUNCTIONS APPENDIX A • SPECIAL CHARACTERS, COMMANDS, AND
  • APPENDIX B • SCALING TECHNIQUES
  • APPENDIX C • THE READY_AIM_FIRE GUI
  • INDEX

xi

About This Book

This book grew out of my experience teaching MATLAB ®^ and other computing languages to freshmen engineering students at Salt Lake Community College. I was frustrated by the lack of a text that “started at the beginning.” Although there were many comprehensive reference books, they assumed a level of both mathem- atical and computer sophistication that my students did not possess. Also, because MATLAB ®^ was originally adopted by practitioners in the fields of signal processing and electrical engineering, most of these texts provided examples primarily from those areas, an approach that didn’t fit with a general engineering curriculum. This text starts with basic algebra and shows how MATLAB ®^ can be used to solve engineering problems from a wide range of disciplines. The examples are drawn from concepts introduced in early chemistry and physics classes and freshman and sophomore engineering classes. A standard problem-solving methodology is used consistently. The text assumes that the student has a basic understanding of college algebra and has been introduced to trigonometric concepts; students who are mathematically more advanced generally progress through the material more rapidly. Although the text is not intended to teach subjects such as statistics or matrix algebra, when the MATLAB®^ techniques related to these subjects are introduced, a brief background is included. In addition, sections describing MATLAB®^ techniques for solving problems by means of calculus and differential equations are introduced near the end of appro- priate chapters. These sections can be assigned for additional study to students with a more advanced mathematics background, or they may be useful as reference material as students progress through an engineering curriculum. The book is intended to be a “hands-on” manual. My students have been most successful when they read the book while sitting beside a computer and typing in the examples as they go. Numerous examples are embedded in the text, with more com- plicated numbered examples included in each chapter to reinforce the concepts introduced. Practice exercises are included in each chapter to give students an immediate opportunity to use their new skills, and complete solutions are available online at: www.pearsonhighered.com/moore. The material is grouped into three sections. The first, An Introduction to Basic MATLAB ®^ Skills , gets the student started and contains the following chapters:

  • Chapter 1 shows how MATLAB®^ is used in engineering and introduces a stand- ard problem-solving methodology.
  • Chapter 2 introduces the MATLAB ®^ environment and the skills required to perform basic computations. This chapter also introduces M-files, and the con- cept of organizing code into cells. Doing so early in the text makes it easier for students to save their work and develop a consistent programming strategy.
  • Chapter 3 details the wide variety of problems that can be solved with built-in MATLAB®^ functions. Background material on many of the functions is provided to help the student understand how they might be used. For example, the differ- ence between Gaussian random numbers and uniform random numbers is described, and examples of each are presented.

xii About This Book

  • Chapter 4 demonstrates the power of formulating problems by using matrices in MATLAB ®^ and expanding on the techniques employed to define those matrices. The meshgrid function is introduced in this chapter and is used to solve problems with two variables. The difficult concept of meshing variables is revisited in Chapter 5 when surface plots are introduced.
  • Chapter 5 describes the wide variety of both two-dimensional and three- dimensional plotting techniques available in MATLAB ®^. Creating plots via MATLAB ®^ commands, either from the command window or from within an M-file, is emphasized. However, the extremely valuable techniques of interac- tively editing plots and creating plots directly from the workspace window are also introduced. MATLAB ®^ is a powerful programming language that includes the basic constructs common to most programming languages. Because it is a scripting language, creating programs and debugging them in MATLAB®^ is often easier than in traditional programming languages such as C++. This makes MATLAB® a valuable tool for introductory programming classes. The second section of the text, Programming in MATLAB® , introduces students to programming and consists of the following chapters:
  • Chapter 6 describes how to create and use user-defined functions. This chapter also teaches students how to create a “toolbox” of functions to use in their own programming projects.
  • Chapter 7 introduces functions that interact with the program user, including user-defined input, formatted output, and graphical input techniques. The use of MATLAB®’s debugging tools is also introduced.
  • Chapter 8 describes logical functions such as find and demonstrates how they vary from the if and if/else structures. The switch case structure is also intro- duced. The use of logical functions over control structures is emphasized, partly because students (and teachers) who have previous programming experience often overlook the advantages of using MATLAB ®^ ’s built-in mat- rix functionality.
  • Chapter 9 introduces repetition structures, including for loops, while loops, and midpoint break loops which utilize the break command. Numerous examples are included because students find these concepts particularly challenging.

Chapters 1 through 9 should be taught sequentially, but the chapters in Section 3, Advanced MATLAB ®^ Concepts , do not depend upon each other. Any or all of these chapters could be used in an introductory course or could serve as ref- erence material for self-study. Most of the material is appropriate for freshmen. A two-credit course might include Chapters 1 through 9 plus Chapter 10, while a three-credit course might include Chapters 1 through 14, but eliminate Sections 12.4, 12.5, 13.4, 13.5, and 13.6, which describe differentiation techniques, integration techniques, and solution techniques for differential equations. Chapters 15 and 16 will be interesting to more advanced students, and might be included in a course delivered to sophomore or junior students instead of to freshmen. The skills developed in these will be especially useful as students become more involved in solving engineering problems:

  • Chapter 10 discusses problem solving with matrix algebra, including dot prod- ucts, cross products, and the solution of linear systems of equations. Although matrix algebra is widely used in all engineering fi elds, it finds early application in the statics and dynamics classes taken by most engineering majors.

xiv About This Book

  • Information on debugging features was added to Chapters 7 and 8.
  • Based on student and instructor feedback, Chapter 8 was significantly revised and split into two chapters. ❍ The new Chapter 8 introduces MATLAB ®’s logical functions such as find , and the more traditional selection structures if , if/else , and switch/case. ❍ The new Chapter 9 deals exclusively with repetition structures.
  • The symbolic toolbox was changed significantly in the 2007b edition, which required changes to the symbolic algebra materials in Chapter 12.
  • Two additional chapters were added in an attempt to make the text useful to a wider audience. ❍ Chapter 15 describes graphical user interfaces. ❍ Chapter 16 is an introduction to Simulink®.
  • Problems were added at the end of each chapter.
  • Additional example problems were added.
  • A number of new functions are introduced throughout the book, suggested to us by adopters of the text.

xv

Dedication and

Acknowledgments

This project would not have been possible without the support of my family, which endured reading multiple drafts of the text and ate a lot of frozen pizza while I con- centrated on writing. Thanks to Mike, Heidi, Meagan, and David, and to my hus- band, Dr. Steven Purcell. I also benefited greatly from the suggestions for problems related to electricity from Lee Brinton and Gene Riggs of the SLCC Electrical Engineering Department. Their cheerful efforts to educate me on the mysteries of electricity are much appreciated. I’d also like to thank Dr. Ghassan Hamarneh for his careful review of the second edition, which helped tremendously as I prepared this latest manuscript. This book is dedicated to my father, Professor George Moore, who taught in the Department of Electrical Engineering at the South Dakota School of Mines and Technology for almost 20 years. Professor Moore earned his college degree at the age of 54 after a successful career as a pilot in the United States Air Force and was a living reminder that you are never too old to learn. My mother, Jean Moore, encouraged both him and her two daughters to explore outside the box. Her loving support made it possible for both my sister and I to enjoy careers in engineering—something few women attempted in the early 1970s. I hope that readers of this text will take a minute to thank those people in their lives who’ve helped them make their dreams come true. Thanks Mom and Dad.

1.1 WHAT IS MATLAB®?

MATLAB®^ is one of a number of commercially available, sophisticated mathematical computation tools, which also include Maple, Mathematica, and MathCad. Despite what proponents may claim, no single one of these tools is “the best.” Each has strengths and weaknesses. Each allows you to perform basic mathematical computations. They differ in the way they handle symbolic calculations and more complicated mathemati- cal processes, such as matrix manipulation. For example, MATLAB®^ (short for Mat rix Lab oratory) excels at computations involving matrices, whereas Maple excels at sym- bolic calculations. At a fundamental level, you can think of these programs as sophisti- cated computer-based calculators. They can perform the same functions as your scientific calculator—and many more. If you have a computer on your desk, you may find yourself using MATLAB®^ instead of your calculator for even the simplest mathe- matical applications—for example, balancing your checkbook. In many engineering classes, the use of programs such as MATLAB®^ to perform computations is replacing more traditional computer programming. Although programs such as MATLAB®^ have become a standard tool for engineers and scientists, this doesn’t mean that you shouldn’t learn a high-level language such as C++, JAVA, or FORTRAN. Because MATLAB®^ is so easy to use, you can perform many programming tasks with it, but it isn’t always the best tool for a programming task. It excels at numerical calculations—especially matrix calculations—and graphics, but you wouldn’t want to

After reading this chapter, you should be able to:

  • Understand what MATLAB®^ is and why it is widely used in engineering and science - Understand the advantages and limitations of the stu- dent edition of MATLAB® - Formulate problems by using a structured prob- lem-solving approach

Objectives

About MATLAB

®

C H A P T E R

2 Chapter 1 About MATLAB®

use it to write a word-processing program. For large applications, such as operating systems or design software, C++, JAVA, or FORTRAN would be the programs of choice. (In fact, MATLAB ®^ , which is a large application program, was originally written in FORTRAN and later rewritten in C, a precursor of C++.) Usually, high- level programs do not offer easy access to graphing—an application at which MATLAB®^ excels. The primary area of overlap between MATLAB®^ and high-level programs is “number crunching”—repetitive calculations or the processing of large quantities of data. Both MATLAB®^ and high-level programs are good at processing numbers. A “number-crunching” program is generally easier to write in MATLAB®, but usually it will execute faster in C++ or FORTRAN. The one exception to this rule is calculations involving matrices. MATLAB®^ is optimized for matrices. Thus, if a problem can be formulated with a matrix solution, MATLAB®^ executes substan- tially faster than a similar program in a high-level language. MATLAB®^ is available in both a professional and a student version. The profes- sional version is probably installed in your college or university computer laboratory, but you may enjoy having the student version at home. MATLAB®^ is updated regu- larly; this textbook is based on MATLAB®^ 7.12. If you are using earlier versions such as MATLAB®^ 6, you may notice some minor differences between it and MATLAB® 7.12. There are substantial differences in versions that predate MATLAB®^ 5.5. The standard installation of the professional version of MATLAB®^ is capable of solving a wide variety of technical problems. Additional capability is available in the form of function toolboxes. These toolboxes are purchased separately, and they may or may not be available to you. You can find a complete list of the MATLAB® product family at The MathWorks web site, www.mathworks.com.

1.2 STUDENT EDITION OF MATLAB®

The professional and student editions of MATLAB®^ are very similar. Beginning stu- dents probably won’t be able to tell the difference. Student editions are available for Microsoft Windows, Mac OSX, and Linux operating systems and can be purchased from college bookstores or online from The MathWorks at www.mathworks.com. The MathWorks packages its software in groups called releases , and MATLAB®^ 7. is featured, along with other products, such as Simulink®^ 7.7, in Release R2011a. New versions are released every 6 months. The release number is the same for both the stu- dent and professional edition, but the student version may lag the professional version by several months. The student edition of R2011a includes the following features:

  • Full MATLAB®
  • Simulink®, with the ability to build models with up to 1000 blocks (the profes- sional version allows an unlimited number of blocks)
  • Symbolic Math Toolbox
  • Control System Toolbox
  • Signal Processing Toolbox
  • DSP System Toolbox
  • Statistics Toolbox
  • Optimization Toolbox
  • Image Processing Toolbox
  • Software manuals for both MATLAB®^ 7 and Simulink®
  • A CD containing the full electronic documentation
  • A single-user license, limited to students for use in their classwork (the profes- sional version is licensed either singly or to a group)

KEY IDEA MATLAB ®^ is optimized for matrix calculations

KEY IDEA MATLAB ®^ is regularly updated