1. Trang chủ
  2. » Công Nghệ Thông Tin

learning to program with matlab

322 2,7K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 322
Dung lượng 8,66 MB

Nội dung

www.it-ebooks.info LENT ffirs.tex 28/11/2012 17: 13 Page iv www.it-ebooks.info LENT ffirs.tex 4/12/2012 14: 28 Page i LEARNING TO PROGRAM WITH MATLAB Building GUI Tools Craig S. Lent Department of Electrical Engineering University of Notre Dame www.it-ebooks.info LENT ffirs.tex 4/12/2012 14: 28 Page ii VP & PUBLISHER: Don Fowley ASSOCIATE PUBLISHER: Dan Sayre EDITORIAL ASSISTANT: Jessica Knecht MARKETING MANAGER: Christopher Ruel DESIGNER: Kenji Ngieng SENIOR PRODUCTION MANAGER: Janis Soo ASSOCIATE PRODUCTION MANAGER: Joyce Poh This book was set by MPS Limited, Chennai. Cover and text printed and bound by Courier Westford. This book is printed on acid free paper. Founded in 1807, John Wiley & Sons, Inc. has been a valued source of knowledge and understanding for more than 200 years, helping people around the world meet their needs and fulfill their aspirations. Our company is built on a foundation of principles that include responsibility to the communities we serve and where we live and work. In 2008, we launched a Corporate Citizenship Initiative, a global effort to address the environmental, social, economic, and ethical challenges we face in our business. Among the issues we are addressing are carbon impact, paper specifications and procurement, ethical conduct within our business and among our vendors, and community and charitable support. For more information, please visit our website: www.wiley.com/go/citizenship. Copyright © 2013 John Wiley & Sons, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc. 222 Rosewood Drive, Danvers, MA 01923, website www.copyright.com. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, (201)748-6011, fax (201)748-6008, website http://www.wiley.com/go/permissions. Evaluation copies are provided to qualified academics and professionals for review purposes only, for use in their courses during the next academic year. These copies are licensed and may not be sold or transferred to a third party. Upon completion of the review period, please return the evaluation copy to Wiley. Return instructions and a free of charge return mailing label are available at www.wiley.com/go/returnlabel. If you have chosen to adopt this textbook for use in your course, please accept this book as your complimentary desk copy. Outside of the United States, please contact your local sales representative. Library of Congress Cataloging-in-Publication Data Lent, Craig S., 1956– Learning to program with MATLAB : building GUI tools / Craig S. Lent, Department of Electrical Engineering, University of Notre Dame. pages cm Includes index. ISBN 978-0-470-93644-3 (pbk. : acid-free paper) 1. Computer programming. 2. Visual programming (Computer science) 3. MATLAB. 4. Graphical user interfaces (Computer systems) I. Title. QA76.6.L45 2013 005.4'37—dc23 2012041638 Printed in the United States of America 10987654321 www.it-ebooks.info LENT ffirs.tex 4/12/2012 14: 28 Page iii To Tom Finke, Pat Malone, the late Katy McShane, and all the other amazing teachers at the Trinity School campuses in South Bend, IN, Eagan, MN, and Falls Church, VA. www.it-ebooks.info LENT ffirs.tex 4/12/2012 14: 28 Page iv www.it-ebooks.info LENT ftoc.tex 6/11/2012 15: 48 Page v Contents Preface ix I MATLAB Programming 1 1 Getting Started 3 1.1 Running the MATLAB IDE 4 Manipulating windows 4 1.2 MATLAB variables 5 Variable assignment statements 7 Variable names 8 Variable workspace 9 1.3 Numbers and functions 9 1.4 Documentation 11 1.5 Writing simple MATLAB scripts 11 1.6 A few words about errors and debugging 14 1.7 Using the debugger 14 2 Strings and Vectors 20 2.1 String basics 21 2.2 Using the disp command to print a variable’s value 22 2.3 Getting information from the user 22 2.4 Vectors 23 2.5 Operations on vectors 24 2.6 Special vector functions 27 Statistical functions 28 2.7 Using rand and randi 29 3 Plotting 34 3.1 The plot command 35 3.2 Tabulating and plotting a simple function 39 3.3 Bar graphs and histograms 43 3.4 Drawing several plots on one graph 46 Multiple plots with a single plot command 46 Combining multiple plots with a hold command 48 3.5 Adding lines and text 51 v www.it-ebooks.info LENT ftoc.tex 6/11/2012 15: 48 Page vi vi Contents 4 Matrices 56 4.1 Entering and manipulating matrices 57 4.2 Operations on matrices 60 4.3 Solving linear systems: The backslash operator 65 Extended example: Solving circuit problems 66 4.4 Special matrix functions 72 5 Control Flow Commands 75 5.1 Conditional execution: The if statement 76 5.2 Logical expressions 79 5.3 Logical variables 81 5.4 for loops 82 5.5 while loops 85 5.6 Other control flow commands 87 Switch-case statement 87 Break statement (not recommended) 88 6 Animation 94 6.1 Basic animation 95 6.2 Animating function plots 99 6.3 Kinematics of motion 103 One-dimensional motion: Constant speed 103 Motion with constant acceleration 106 Time-marching dynamics: Nonconstant force 109 7 Writing Your Own MATLAB Functions 117 7.1 MATLAB function files 118 Declaring MATLAB functions 119 7.2 Function inputs and outputs 120 7.3 Local workspaces 120 7.4 Multiple outputs 121 7.5 Function files 121 7.6 Other functional forms 121 Subfunctions 122 Nested functions 127 Anonymous functions 128 8 More MATLAB Data Classes and Structures 137 8.1 Cell arrays 138 8.2 Structures 139 8.3 Complex numbers 140 8.4 Function handles 141 8.5 Other data classes and data structures 141 www.it-ebooks.info LENT ftoc.tex 6/11/2012 15: 48 Page vii Contents vii II Building GUI Tools 145 9 Building a Graphical User Interface 147 9.1 Getting started with GUIDE 147 Saving the GUI to a file 150 9.2 Starting an action with a GUI element 151 9.3 Communicating with GUI elements 154 Building SliderTool 154 Communicating with GUI elements from the command line 157 9.4 Synchronizing information with a GUI element 161 9.5 Key points from this chapter 163 10 Transforming a MATLAB Program into a GUI Tool 165 10.1 Creating a GUI tool step by step 166 10.2 Further GUI design considerations 177 11 GUI Components 189 III Advanced Topics 207 12 More GUI Techniques 209 12.1 Waitbars 210 12.2 File dialogs 211 Saving and loading data in .mat files 211 A GUI interface to file names using uiputfile and uigetfile 212 12.3 Reading and writing formatted text files 215 12.4 The input dialog 219 12.5 The question dialog 220 12.6 Sharing application data between functions 221 12.7 Responding to keyboard input 222 12.8 Making graphic objects interactive 223 Mouse-click response 223 Mouse events and object dragging 225 12.9 Creating menus in GUIDE 228 13 More Graphics 232 13.1 Logarithmic plots 233 13.2 Plotting functions on two axes 236 13.3 Plotting surfaces 237 13.4 Plotting vector fields 243 13.5 Working with images 245 Importing and manipulating bit-mapped images 245 Placing images on surface objects 253 13.6 Rotating composite objects in three dimensions 254 www.it-ebooks.info LENT ftoc.tex 6/11/2012 15: 48 Page viii viii Contents 14 More Mathematics 260 14.1 Derivatives 261 Derivatives of mathematical functions expressed as MATLAB functions 261 Derivatives of tabulated functions 263 14.2 Integration 265 Integrating tabulated functions 265 Integrating mathematical functions expressed as MATLAB functions . 270 14.3 Zeros of a function of one variable 273 14.4 Function minimization 275 Finding a minimum of a function of one variable 275 Multidimensional minimization 277 Fitting to an arbitrary function by multidimensional minimization 278 Solving simultaneous nonlinear equations by multidimensional minimization 281 14.5 Solving ordinary differential equations 284 14.6 Eigenvalues and eigenvectors 289 AppendixA: Hierarchy of Handle Graphics Objects 293 Appendix B:Using L A T E X Commands 295 Index 301 www.it-ebooks.info [...]... mathematical operations on numbers The MATLAB integrated development environment is the program that runs when you launch MATLAB You will use it to operate MATLAB interactively, and to develop and run MATLAB programs The concept of a MATLAB variable is important to grasp It is not identical with the familiar mathematical notion of a variable, though the two are related MATLAB variables should be thought... files in the current directory The browser displays files and other directories (folders) that reside in the current directory Icons at the top of the browser allow the user to move up a directory (folder) level or to create a new folder Double-clicking on a displayed folder makes it the current folder Editor window The MATLAB editor is where programs are written Launch the Editor window by typing “edit”... “EDITOR|Save|Save As ” and enter calcParticle Energy in the File Save dialog box The dialog box will automatically add the “.m” extension to the filename; this indicates the file is a MATLAB program You can save any changes and run the program by pressing the green “Run” button at the top of the Editor tab Let’s look at some important features of this simple program Block structure Virtually all programs...LENT fpref.tex 6/11/2012 15: 53 Page ix Preface To learn how to program a computer in a modern language with serious graphical capabilities, is to take hold of a tool of remarkable flexibility that has the power to provide profound insight This text is primarily aimed at being a first course in programming, and is oriented toward integration with science, mathematics, and engineering It is also... technique for transforming a functioning MATLAB program into a GUI tool Readers already familiar with MATLAB, but unfamiliar with using GUIDE, can likely work through these two chapter in a couple hours and be in short order making GUI tools Part III covers more advanced techniques in GUI building, graphics, and mathematics It is not meant to be comprehensive; the online MATLAB help documentation is excellent... for programming Lego Mindstorms® robots The high school curriculum at Trinity introduces students to programming with a four-week course on the Alice language, developed by Carnegie Mellon University These “ramp languages” allow students to become accustomed to programming as creating a sequence of instructions in a way that is insulated from syntax errors A note on formatting: Numerous examples, programs,... and continue 4 Run the program by pressing the green “Run” button on the EDITOR tab or by invoking the program name in the Command window 5 Press the “Step” button on the EDITOR tab to step through the program line by line, executing one statement at a time Look in the Workspace browser to see the current values of all variables In the Command window the “>>” prompt becomes “K>>” to indicate that any... triangle 4 Running to the Sun Write a program, sunrun.m, to calculate how many hours it would take to run to the Sun if averaging a five-minute mile Display the answer in seconds, hours, and years (The distance from the Earth to the Sun is 93 million miles.) 5 Gravitational force Write a program, gforce.m, to find the gravitational force (in Newtons) between any two people using F = Gm1 m2 /r 2 with gravitational... Write a program, LightTime.m, to calculate the time it takes light to travel (a) from New York to San Francisco, (b) from the Sun to the Earth, (c) from Earth to Mars (minimum and maximum), (d) from the Sun to Pluto www.it-ebooks.info 17 LENT 18 c01.tex 28/11/2012 CHAPTER 1 15: 32 Page 18 Getting Started 13 Finite difference Consider the function f (x) = cos(x) Write a program, FiniteDiff.m, to calculate... advanced students and researchers who want to rapidly acquire the ability to easily build useful graphical tools for exploring computational models The MATLAB programming language provides an excellent introductory language, with built-in graphical, mathematical, and user-interface capabilities The goal is that the student learns to build computational models with graphical user interfaces (GUIs) that . on numbers. The MATLAB integrated development environment is the program that runs when you launch MATLAB. You will use it to operate MATLAB interactively, and to develop and run MATLAB programs. The. tutorial approach to GUI building. Chapter 10 details a simple, but widely useful, technique for transforming a functioning MATLAB program into a GUI tool. Readers already familiar with MATLAB, but. 163 10 Transforming a MATLAB Program into a GUI Tool 165 10.1 Creating a GUI tool step by step 166 10.2 Further GUI design considerations 177 11 GUI Components 189 III Advanced Topics 207 12 More

Ngày đăng: 28/04/2014, 16:19

TỪ KHÓA LIÊN QUAN