Matlab course lecture notes

what is matlab for engineers and how to learn matlab quickly and how matlab integrated design workflow pdf free download
NancyWest Profile Pic
NancyWest,Germany,Professional
Published Date:12-07-2017
Your Website URL(Optional)
Comment
Introduction to Matlab Course notes Mark Herbster and Jason Kastanis c Copyright 2006 M. Herbster and J.Kastanis January 2006CONTENTS i Contents I Interface Guide 1 1 Overview 1 2 Style of the guide 2 3 A brief history of Matlab 2 4 Basic elements of programming 3 5 Matlab main desktop 4 5.1 Title bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 5.2 Menu bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 5.3 Desktop toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . 6 5.4 Command window . . . . . . . . . . . . . . . . . . . . . . . . 6 5.5 Command history . . . . . . . . . . . . . . . . . . . . . . . . . 8 5.6 Current directory (window) . . . . . . . . . . . . . . . . . . . 8 5.7 Launch pad . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 5.8 Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 6 Opening and editing files 10 6.1 Opening files . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 6.2 Editing files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 7 Getting help 15 7.1 Text based help . . . . . . . . . . . . . . . . . . . . . . . . . . 15 7.2 Graphical interface help . . . . . . . . . . . . . . . . . . . . . 17 7.3 Web-based help . . . . . . . . . . . . . . . . . . . . . . . . . . 23 8 Setting the desktop layout 24 II Lecture 1 25 9 Overview of Lecture 1 25 10 Style of notes 26CONTENTS ii 11 Recommended reading 26 12 Introduction to Matlab 27 13 Building matrices 27 14 Addressing and assigning elements 31 15 Building special matrices 34 16 Matrix operations 39 17 Equation solving 43 18 User defined functions 45 19 Plotting 47 20 Utility commands 49 21 Summary table of functions 50 22 Lab exercises 1 51 III Lecture 2 60 23 Overview of Lecture 2 60 24 Relational operators 61 25 Logical operators 63 26 Control flow 65 26.1 for loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 26.2 while loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 26.3 if-else-end . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 26.4 switch-case-otherwise-end . . . . . . . . . . . . . . . . . . . . 74 27 Precision issues 76CONTENTS iii 28 Additional data types 81 28.1 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 28.2 Cell arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 28.3 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 29 Input/Output (I/O) 88 30 Formatted Input/Output 89 31 Summary table of functions 108 32 Lab exercises 2 109 IV Lecture 3 121 33 Overview of Lecture 3 121 34 Matlab performance tuning 122 35 Set functions 128 36 User defined functions 2 130 37 Plotting 2 137 38 Summary table of functions 148 39 Lab exercises 3 1491 Part I Interface Guide 1 Overview • Style of the guide • A brief history of Matlab • Basic elements of programming • Matlab main desktop Title bar Menu bar Desktop toolbar Command window Command history Current directory (window) Launch pad Workspace • Opening and editing files Opening files Editing files • Getting help Text based help Graphical help interface Web based help • Setting the desktop layout2 STYLE OF THE GUIDE 2 2 Style of the guide This guide has been designed to offer a short introduction to programming and the Matlab environment. The main functionality of the graphical user interface is described using example images. These images were produced on a PC running Windows and Matlab version 6.5. They might differ slightly from the version of Matlab that you are running. Bold is used for all the icons, tools, menu items and other parts of the Matlabinterface. Theitalic fontisusedfortheintroductionofbasicelements of programming. Elements, such as commands, that belong in the Matlab programming language were written using the verbatim font. 3 A brief history of Matlab MATrix LABoratory was originally developed by Cleve Moler in the 1970’s, then chairman of the computer science department of the University of New Mexico. It was an interface for theLINPACK and EISPACKlibraries, which were written in FORTRAN with the participation of Moler. Matlab was originally intended for a linear algebra course. Its aim was to simplify the use of these subroutine libraries by avoiding the complexities of FORTRAN. Matlabbegangainingpopularitywithintheappliedmathematicscommunity. Theearlyversionswerebasedonthecommandpromptandhadnographical interface. In 1983 Moler, Jack Little and Steve Bangert rewrote Matlab in C and the following year founded Mathworks to market it further. From version 6 Matlab was based on the LAPACK library which has superseded both LINPACK and EISPACK.4 BASIC ELEMENTS OF PROGRAMMING 3 4 Basic elements of programming A program is a collection of instructions for the computer to execute. It is essentially an algorithm, in that sense it has to be deterministic. Each instruction should be unambiguous. A programming language just like any language has a set of rules. These rules describe the syntax and semantics of the language. The basic elements of a programming language are described below. Variables are places to store values on the computer memory. The name of the variable serves as the address in the memory, where the value of this variable is held. e.g. x=5, x is the variable, it represents a particular loca- tion in the memory, where the value 5 is stored. Variables can have types. Types describe the kind of values a variable can accept and they are di- vided in primitive and composite. Primitive types are the ones provided by theprogramminglanguageandtheytypicallycontainintegers, floating-point numbers and characters. Composite types are made from the combination of primitive types with other primitive types or other composite types. For example an integer variable is of primitive type and it can only store inte- ger values. In some languages the declaration of the type and dimensions of a variable is not required. Variables can be local or global, local ones are only accessible in a particular part of the program, while global ones can be accessed anywhere. An array is the most basic data structure. It is a list of elements of the same type. Individual elements of an array can be accessed using a consecutive range of integers. This is referred to as the index. The index denotes the position of an element in the list. One dimensional arrays are called vectors and two dimensional are called matrices. Assignment is the taskofstoringavalueinavariable. Itiscommonlydoneusingtheequalsign (=). Keep in mind that the equal sign in Mathematics stands for equality, in programming it stands for assignment. Expressions compute new values from old ones. The expression 5 + 3 will calculate the sum of the values 5 and 3. In the previous expression the plus sign is an operator, which operates on the values 5 and 3. Statements, or instructions, describe what the program will do. They can contain assign- ments, expressions and control flow operations. Control flow operations are divided in two main categories, conditionals and loops. Conditionals, as the name suggests, condition the flow of the program, if a variable has a partic- ular value (or belongs in a particular range of values), then a particular set5 MATLAB MAIN DESKTOP 4 of statements will be executed, if not another set will. Loops are used for repetition, their construction contains a rule determining how many times a set of statements will be repeated. An entire set of statements grouped together is called a function. A function takes variables, in this case called arguments, and can return values. Calling a function transfers the control over to the function, the group of statements which form it will be executed. When this finishes it returns (with or without a result) to the original flow of the program. Having specified these basic elements of programming, a program can be redefined as an ordered collection of statements, functions and variables. 5 Matlab main desktop TheMatlabmaindesktop,presentedinfig.1,hasmanyareasandwindows. These are discussed in the following sections. Figure 1: The default Matlab main desktop5 MATLAB MAIN DESKTOP 5 5.1 Title bar The title bar (fig. 2) contains the program’s name and logo as well as the windowcontrolbuttons. ItissituatedonthetopofthemainMatlabwindow. Figure 2: The title bar 5.2 Menu bar The menu bar (fig. 3) is underneath the title bar. It has commands for opening, closing files, preferences, etc. Many of these commands have keyboardshortcuts, butthesevarybetweendifferentoperatingsystems. The shortcutkeysarecommonlydisplayednexttothecommand,forexamplelook at fig. 4 Figure 3: The menu bar Figure 4: Example of a menu item5 MATLAB MAIN DESKTOP 6 5.3 Desktop toolbar The desktop toolbar (fig. 5) is placed underneath the menu bar. It con- tains many items found on the menus, new file, open, copy, paste, ... . To find out what each icon does, leave the mouse above it for a few seconds and a small box with a tool tip will appear, e.g. fig. 6. Figure 5: The desktop toolbar Figure 6: A tooltip Ontheleftsideofthedesktoptoolbarthereisbox,whichcanbeedited, called the current directory. This defines the location, where Matlab is working. It is the folder at which the user is looking at. If Matlab (or the user) can’t find a particular file, it means that the file is not in the current directory. Commonly used or referenced directories can be setup from the menu bar using File → Set Path... . The small arrow on the right of the boxwillshowpastcurrentdirectories(fig.7). Nexttothatthebrowsetool, the icon with the three dots, is used for finding a folder. 5.4 Command window The command window (fig. 8) is the most important part of the Matlab main desktop. It is the window where input and output appears. In this window the user can enter commands and obtain results. Each new line on the command prompt starts with the symbol . This defines where new input can be entered. Input can be apart from Matlab commands, various DOS or Linux prompt type of commands, e.g dir, ls.5 MATLAB MAIN DESKTOP 7 Figure 7: Past directories Figure 8: Command window5 MATLAB MAIN DESKTOP 8 By pressing the up arrow on the keyboard the user can scroll through all the previously entered commands. To scroll back the down arrow can be used. If a letter (or more) is typed, then the up and down arrows can be usedtoscrollthroughallthecommandsthathavebeenpreviouslytypedand begin with these letters. The tab button can be used to complete the name of commands or functions. If a command does not exist or if more than one commands with the same starting letters exist, then pressing the tab will make a sound. 5.5 Command history The command history window (fig. 9) contains the history of the com- mands entered in the command window. It begins on each new session with the starting date and time. Thus each session history is separated by dates. Commands from the history window can be copied and pasted, dragged and dropped. Figure 9: Command history window 5.6 Current directory (window) The current directory is also visible as a window inside the Matlab main desktop (fig. 10). On the top of this window there is box, which contains the location of the current directory, same as the one in the desktop toolbar. File names appear on the left column, file types on the middle and last date of modification on the right column. On the same part of the Matlab main desktop, the next two windows (launch pad, workspace) appear as tabs. All of these windows are sepa- rable from the main desktop window.5 MATLAB MAIN DESKTOP 9 Figure 10: Current directory window 5.7 Launch pad The launch pad (fig. 11) is a way of accessing various Matlab resources, such as the import wizard, the profiler, the Graphical User Interface (GUI) builder, etc. Figure 11: Launch pad ThesewindowsappearasseparatewindowsfromtheMatlabmaindesktop, someofwhichcanbedocked,thatistheycanbeapartofthemaindesktop, and some which cannot be docked, they are called undockable windows. The launch pad can also be used to launch the help and demo files of the toolboxes. As it can be seen on fig. 11 it lives on the same window as the6 OPENING AND EDITING FILES 10 current directory. Those two can be switched using the tabs on the lowest part of the window, by pressing the one with the corresponding name. 5.8 Workspace Another window that cohabits the same space as the launch pad and the current directory is the workspace window (fig. 12). This window dis- plays the loaded variables of the current Matlab session, these are variables you have created and are currently loaded on the memory. It displays their name, their size, that is their dimensions, the number of bytes they take on memory and their class, that is the type of variable. Figure 12: Workspace On the top part of the workspace window there is toolbar with tools associatedtothevariables. Forexampletheopeniconwilllaunchaseparate window, the array editor (fig. 13), for viewing and editing the contents of a variable. 6 Opening and editing files 6.1 Opening files There are a few ways the user can open a file. From the menu bar, File → Open, from the desktop toolbar by clicking on the open file icon, by typing the name of the file on the command prompt, selecting it, right- clicking and choosing open selection. Matlab can deal with a variety of file6 OPENING AND EDITING FILES 11 Figure 13: Array editor formats, but only a few will be mentioned here. Workspace files store loaded variables in to a .mat file, figure files (.fig) are graphic files, and M-files contain code and finish with .m. Variables contained in a mat file will be loaded on the workspace as soon as the mat file is opened. Figure files will open on a separate window (fig. 14), this offers certain tools for editing and manipulating the figure. Figure 14: Figure window6 OPENING AND EDITING FILES 12 6.2 Editing files As soon as an M-file is opened the editor window fig. 15 will appear as a separate window. Figure 15: Editor window6 OPENING AND EDITING FILES 13 This window can be docked in to the Matlabmain desktop. Every file that is opened will appear in the same window. Each file can be in its own editor window, in most cases it is practical to keep them in one. Files are chosen by the tabs on the lower part of the editor window (fig. 16). Figure 16: Editor tabs Ofcourseonecoulduseanyeditor,butMatlab’seditorofferscolorcoding, runninganddebuggingfacilities. Similartothecommandprompt,theuser can select the name of a function file, right-click on it and open it. On the bottom right of the editor window (fig. 17), information about the file is displayed as well as the line and the column where the cursor is placed, this can be very useful when debugging. On the left side of the editing area the lines are numbered. Figure 17: Information on the lower part of the editor A toolbar (fig. 18) is displayed on the top of the editor window. The standard buttons, New file, Open file, Save, Copy, Cut, Paste, Print are placed here. Next to them the binocular icon represents the find tool. This is used for searching the file for a particular keyword and replacing it if required. Further to the right are the debugging tools for setting and clearing breakpoints. The Run button is also situated on the right of the debugging tools. This will execute the code contained in the active file. Figure 18: The editor toolbar Files can also be executed using the menu bar (fig. 19) on the upper part of theeditor window inDebug→Run. Themenu bar contains all of the toolbar commands and many others.6 OPENING AND EDITING FILES 14 Figure 19: The editor menu bar The name of the file is displayed on the top of the editor window on the title bar (fig. 20). Figure 20: The editor title bar7 GETTING HELP 15 7 Getting help It is an essential part of programming to be able to find out information about syntax and functionality as well as to see working examples. There arethreewaystheusercangethelpinMatlab,textbasedhelp,graphical help interface and web based help. 7.1 Text based help Text based help can be obtained from the command prompt by typing help. The help topics then appear as in fig. 21. Figure 21: Text based help topics To see the subtopics of one of the topics of fig. 21 type the name of the topic in the command prompt. For example:7 GETTING HELP 16 help matlab\general The command help can also be used to find out information about a specific function. For example: help sin SIN Sine. SIN(X) is the sine of the elements of X. Overloaded methods help sym/sin.m In the case the function is unknown or the user wants to search for a specific keyword, the command lookfor can be used. lookfor infinity INF Infinity. CEIL Round towards plus infinity. FLOOR Round towards minus infinity. CHOLINC Sparse Incomplete Cholesky and Cholesky-Infinity factorizations. ACTDEMO Demo of digital H-infinity hydraulic actuator design. DHINF Discrete H-Infinity control synthesis (bilinear transform version). DHINFOPT Discrete H-Infinity control synthesis via Gamma iteration. DINTDEMO Demo of H-Infinity design of double integrator plant. ... The command lookfor will search in all help entries. To find out the details in one of the search results help can be used as previously: help INF INF Infinity. INF returns the IEEE arithmetic representation for positive infinity. Infinity is also produced by operations like dividing by zero, eg. 1.0/0.0, or from overflow, eg. exp(1000). See also NaN, ISFINITE, ISINF.

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.