Unformatted text preview:

IntroductionThe fifty-cent tourHelpBasic commands and syntaxSaving workExercisesArrays and matricesBuilding arraysReferencing elementsMatrix operationsArray operationsExercisesScripts and functionsUsing scripts effectivelyFunctionsDebugging and profilingInline functionsFunction functionsExercisesMore on functionsLoops and conditionalsErrors and warningsScoping exceptionsExercisesGraphics2-D plots3-D plotsAnnotationQuick function plotsHandles and propertiesColorSaving figuresGraphics for publicationExercisesOptimizing performanceUse functions, not scriptsPreallocate memoryVectorizeUse maskingExercisesAdvanced data structuresStringsFormatted outputCell arraysStructuresGraphical user interfacesGUI programming styleUsing guide for layoutCreating callbacksAn exampleObject-oriented programmingOOP in MATLABAn OOP exampleExercisesLinking to Fortran or CHow MEX files workExamples and outlineCrash course in MATLABc Tobin A. Driscoll∗June 2, 2003The purpose of this document is to give a medium-length introduction to the essentials ofMATLAB and how to use it well. I’m aiming for a document that’s somewhere between a two-page introduction for a numerical methods course and the excellent but longer Getting Started andother user guides found in the online docs. I use it in a week-long “boot camp” for graduatestudents at the University of Delaware in the summer after their first year of study.I assume no knowledge of MATLAB at the start, though a working familiarity with basic linearalgebra is pretty important. The first four sections cover the basics needed to solve even simpleexercises. (There is also a little information about using MATLAB to make graphics for a technicalpaper.) The remaining sections go more deeply into issues and capabilities that should at least bein the consciousness of a person trying to implement a project of more than a hundred lines or so.The version of MATLAB at this writing is 6.5 (Release 13). Simulink and the optional toolboxesare not covered.Please don’t redistribute or alter this document without my permission. I’m not stingy withsuch permission, but I like to have an idea of where my work goes.1 IntroductionMATLAB is a software package for computation in engineering, science, and applied mathemat-ics. It offers a powerful programming language, excellent graphics, and a wide range of expertknowledge. MATLAB is published by and a trademark of The MathWorks, Inc.The focus in MATLAB is on computation, not mathematics. Hence symbolic expressions andmanipulations are not possible (except through a clever interface to Maple). All results are notonly numerical but inexact, thanks to the rounding errors inherent in computer arithmetic. Thelimitation to numerical computation can be seen as a drawback, but it is a source of strengthtoo: MATLAB generally runs circles around Maple, Mathematica, and the like when it comes tonumerics.On the other hand, compared to other numerically oriented languages like C++ and FOR-TRAN, MATLAB is much easier to use and comes with a huge standard library. The only majorunfavorable comparison here is a gap in execution speed. This gap is maybe not as dramatic as∗Department of Mathematical Sciences, Ewing Hall, University of Delaware, Newark, DE 19716;[email protected] INTRODUCTION 2popular lore has it, and it can often be narrowed or closed with good MATLAB programming (seesection 6), but MATLAB is not the best tool for high-performance computing.The MATLAB niche is numerical computation on workstations for nonexperts in computation.This is a huge niche—one way to tell is to look at the number of MATLAB-related books on math-works.com. Even for hard-core supercomputer users, MATLAB can be a valuable environmentin which to explore and fine-tune algorithms before more laborious coding in C.1.1 The fifty-cent tourWhen you start MATLAB, you get a multipaneled desktop and perhaps a few other new windowsas well. The layout and behavior of the desktop and its components are highly customizable.The component that is the heart of MATLAB is called the Command Window. Here you cangive MATLAB commands typed at the prompt, >>. Unlike FORTRAN, and other compiled com-puter languages, MATLAB is an interpreted environment—you give a command, and MATLABtries to follow it right away before asking for another.In the d efault desktop you can also see the Launch Pad. The Launch Pad is a window intothe impressive breadth of MATLAB. Individual toolboxes add capability in specific methods orspecialties. Often these represent a great deal of expert knowledge. Most have friendly demon-strations that hint at their capabilities, and it’s easy to waste a day on these. You may notice thatmany toolboxes are related to electrical engineering, which is a large share of MATLAB’s clientele.Another major item, not exactly a toolbox, is Simulink, which is a control- and system-orientedinterface to MATLAB’s dynamic simulation facilities.Notice at the top of the desktop that MATLAB has a notion of current directory, just likeUNIX does. In general MATLAB can only “see” files in the current directory and on its own path.Commands for working with the directory and path include cd, what, addpath, and pathedit(in addition to widgets and menu items). We will return to this subject in section 3.You can also see a tab for the workspace next to the Launch Pad. The workspace shows youwhat variables are currently defined and some information about their contents. At startup it is,naturally, empty.In this document I will often give the names of commands that can be used at the prompt. Inmany (maybe most) cases these have equivalents among the menus, buttons, and other graphicalwidgets. Take some time to explore these widgets. This is one way to become familiar with thepossibilities in MATLAB.1.2 HelpMATLAB is a huge package. You can’t learn everything about it at once, or always remember howyou have done things before. It is essential that you learn how to teach yourself more using theonline help.There are two levels of help:• If you need quick help on the syntax of a command, use help. For example, help plottells you all the ways in which you can use the plot command. Typing help by itself givesyou a list of categories that themselves yield lists of commands.1 INTRODUCTION 3• Use helpdesk or the menu/graphical equivalent to get into the Help Browser. This in-cludes HTML and PDF forms of all MATLAB manuals and guides, including toolbox


View Full Document

UD ELEG 212 - Crash Course in MATLAB

Download Crash Course in MATLAB
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Crash Course in MATLAB and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Crash Course in MATLAB 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?