Download Learning to Program with MATLAB PDF

Learning to Program with MATLAB Building GUI Tools pdf

Learning to Program with MATLAB – The text is for instructors who want to use MATLAB to teach introductory programming concepts. Since many students struggle with applying the concepts that underlie good programming practice,  Learning to Program with MATLAB: Building GUI Tools was designed upon the observation that student learning is enhanced if the students themselves build the GUI (graphical user interface) tool, construct the computational model, implement the visualization of results, and design the GUI.

This text teaches the core concepts of computer programming―arrays, loops, functions, and basic data structures―using MATLAB. The chapter sequence covers text-based programs, then programs that produce graphics, building up to an emphasis on GUI tools. This progression unleashes the real power of MATLAB―creating visual expressions of the underlying mathematics of a problem or design.

Contents – Learning to Program with MATLAB


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

Learning to Program with MATLAB

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

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

Learning to Program with MATLAB

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

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 LATEX Commands 295
Index

Preface – Learning to Program with MATLAB

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 useful for more 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 enable exploration of model behavior. This GUI tool-building approach has been used at multiple educational levels: graduate courses, intermediate undergraduate courses, an introductory engineering course for first-year college students, and high school junior and senior-level courses. The MATLAB programming language, descended from FORTRAN, has evolved to include many powerful and convenient graphical and analysis tools.

It has become an important platform for engineering and science education, as well as research. MATLAB is a very valuable first programming language, and for many will be the preferred language for most, if not all, of the computational work they do. Of course, C++, Java, Python, and many other languages play crucial roles in other domains. Several language features make the MATLAB language easier for beginners than many alternatives: it is interpreted rather than compiled; variable types and array sizes need not be declared in advance; it is not strongly typed; vector, matrix, multidimensional array, and complex numbers are basic data types; there is a sophisticated integrated development and debugging environment; and a rich set of mathematical and graphics functions is provided.

While computer programs can be used in many ways, the emphasis here is on building computational models, primarily of physical phenomena (though the techniques can be easily extended to other systems). A physical system is modeled first conceptually, using ideas such as momentum, force, energy, reactions, fields, etc. These concepts are expressed mathematically and applied to a particular class of problem. Such a class might be, for example, projectile motion, fluid flow, quantum evolution, electromagnetic fields, circuit equations, or Newton’s laws. Typically, the model involves a set of parameters that describe the physical system and a set of mathematical relations (systems of equations, integrals, differential equations, eigensystems, etc.). The mathematical solution process must be realized through a computational algorithm—a step-by-step procedure for calculating the desired quantities from the input parameters. The behavior of the model is then usually visualized graphically, e.g., one or more plots, bar graphs, or animations.

A GUI tool consists of a computational model and a graphical user interface that lets the user easily and naturally adjust the parameters of the model, rerun the computation, and see the new results. The experience that led to this text was the observation that student learning is enhanced if the students themselves build the GUI tool: construct the computational model, implement the visualization of results, and design the GUI. The GUI is valuable for several reasons. The most important is that exploring model behavior, by manipulating sliders, buttons, checkboxes, and the like, encourages a focus on developing an intuitive insight into the model behavior. Insight is the primary goal. Running the model many times with differing inputs, the user can start to see the characteristic behavior of physical system represented by the model. Learning to Program with MATLAB

Additionally, it must be recognized that graphically driven tools are what students are accustomed to when dealing with computers. A command line interface seems crude and retrograde. Moreover, particularly for engineering students, the discipline of wrapping the model in a form that someone else could use encourages a design-oriented mentality. Finally, building and delivering a sophisticated mathematical model that is operated through a GUI interface is simply more rewarding and fun. The GUI tool orientation guides the structure of the text. Part I (Chapters 1 through 8) covers the fundamentals of MATLAB programming and basic graphics. It is designed to be what one needs to know prior to actual GUI building. The goal is to get the student ready for GUI building as quickly as possible (but not quicker). In this context, Chapter 4 (matrices) and Chapter 6 (animation) warrant comment.

Because arrays are a basic MATLAB data class and solving linear systems a frequent application, this material is included in Part I. An instructor could choose to cover it later without disrupting the flow of the course. Similarly, the animation techniques covered in Chapter 6 could be deferred. The animation process does, however, provide very helpful and enjoyable practice at programming FOR loops. Many GUI tools are enhanced by having an animation component; among other advantages, animation provides a first check of model behavior against experience. The end of Chapter 6 also includes a detailed discussion of the velocity Verlet algorithm as an improvement on the Euler method for solving systems governed by Newton’s second law.While this could be considered a more advanced topic, without it, models as simple as harmonic motion or bouncing balls fail badly because of nonconservation of energy. Learning to Program with MATLAB

Part II covers GUI tool creation with the GUIDE (graphical user interface development environment) program, which is part of MATLAB. Chapters 9 and 10 are the heart of the text and take a very 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 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 and will be the main source for many details. The text covers what, in many cases, is the

About the Author – Learning to Program with MATLAB

Craig S. Lent is the author of Learning to Program with MATLAB: Building GUI Tools, published by Wiley.

DOWNLOAD

You May Also Like

About the Author: arthur peace

A little bio won't be bad but i don't have any.. Lol.

Leave a Reply

Your email address will not be published.