Unformatted text preview:

An Introduction to MATLAB ProgrammingCenter for Interdisciplinary Research and ConsultingDepartment of Mathematics and StatisticsUniversity of Maryland, Baltimore Countywww.umbc.edu/circWinter 2008Mission and Goals: The Center for Interdisciplinary Research and Consulting(CIRC) is a consulting service on mathematics and statistics provided by the Depart-ment of Mathematics and Statistics at UMBC. Established in 2003, CIRC is dedicatedto support interdisciplinary research for the UMBC campus community and the pub-lic at large. We provide a full range of consulting services from free initial consultingto long term support for research programs.CIRC offers mathematical and statistical expertise in broad areas of applications,including biological sciences, engineering, and the social sciences. On the mathematicsside, particular strengths include techniques of parallel computing and assistance withsoftware packages such as MATLAB and COMSOL Multiphysics (formerly known asFEMLAB). On the statistics side, areas of particular strength include Toxicology,Industrial Hygiene, Bioequivalence, Biomechanical Engineering, Environmental Sci-ence, Finance, Information Theory, and packages such as SAS, SPSS, and S-Plus.Copyrightc 2003–2008 by the Center for Interdisciplinary Research and Consult-ing, Department of Mathematics and Statistics, University of Maryland, BaltimoreCounty. All Rights Reserved.This tutorial is provided as a service of CIRC to the community for personal usesonly. Any use beyond this is only acceptable with prior permission from CIRC.This document is under constant development and will periodically be updated. Stan-dard disclaimers apply.Acknowledgements: We gratefully acknowledge the efforts of the CIRC researchassistants and students in Math/Stat 750 Introduction to Interdisciplinary Consultingin developing this tutorial series.MATLAB is a registered trademark of The MathWorks, Inc., www.mathworks.com.31 IntroductionIn this tutorial we introduce the basics of programming in Matlab. We will start byreviewing the elements of structured programming; next, we will begin our discussionof Matlab programming. The flow of control and the related Matlab keywords will bementioned along with several examples for the purpose of illustration. Another topicwhich we will discuss is the use of Matlab functions. At the end, we will point theaudience to the appropriate sections of Matlab’s documentations for more informationon Matlab programming. The outline of the major objectives of this workshop is thefollowing:• Basics of Structured Programming• Basic Input and Output• Flow of Control in a Program• Matlab Functions• Further Help2 Structured Programming in MatlabMatlab provides extensive math and graphics functions along with a powerful high-level programming language. Programmers can choose to program using the classi-cal structured programming approach, but it is also possible to do objec t-orientedprogramming in Matlab. Our discussion, however, will be limited to structured pro-gramming in Matlab.2.1 Structured ProgrammingStructured programming (modular programming) is a software development paradigmin which problems are solved in a top-down fashion. As a program gets larger, it issub-divided into sub-programs to provide code that is easier to read, debug, and main-tain; also this approach facilitates reuse of commonly used procedures (functions). Anatural starting point in our discussion of structured programming in Matlab will bea quick review of the elements of structured programming.2.2 Basic Constructs of Structur ed Programmi ngRecall that an algorithm is a finite set of instructions for accomplishing a given taskwhich given an initial state will terminate in a defined end state. And a computerprogram is an implementation of a given algorithm in a programming language. In4 2 STRUCTURED PROGRAMMING IN MATLABthe discussion that follows, we will use Matlab programming language as our imple-mentation tool.There are three main programming constructs:1. Sequence2. Branch (Decision)3. LoopThe Sequence construct refers to writing a group of programming statements in asequence. The Branch construct enables us to change the flow of control if a givencondition is satisfied, and finally the Loop construct enables the program to run astatement (or a group of statements) a number of times.53 Some Technical DetailsBefore we go deeper in our discussion of Matlab programming, we take care of sometechnical details in this section. We will first mention naming conventions for Matlabprogram files; next, we will talk about Matlab’s program development environment,and then have a brief discussion of input and output.3.1 File Names in MatlabMatlab programming codes are saved in files with extension .m. This gives rise tothe so-called Matlab M-files. For example, a program that computes the roots of aquadratic polynomial may be saved in a file named quadRoots.m. An M-file maycontain a Matlab script or a Matlab function; the distinction between the two typeswill become clear in the course of our discussion.3.2 Development EnvironmentMatlab programs can be created using any text editor. In a Unix environment, onecan use editors such as vi, emacs, nedit, etc. In Windows environment, notepadand alike can be used. In addition, Matlab provides a user-friendly editor whereprogrammers can create their programs (and also debug them). To access Matlab’seditor, type the following in Matlab command prompt,editthis opens Matlab’s editor (Figure 1).3.3 A Word on Input and OutputBefore doing any programming we recall that a typical computer program (in struc-tured programming) receives data as input, does s ome pro cessing on the given dataand provides output. Henc e, input and output constitute an important part of anycomputer program. The programming examples in this tutorial do not involve inter-active (or batch) data input. However, in every example, we will create some output.The commands used for providing output are disp and fprintf. We will discussthese commands at appropriate places throughout this tutorial. Although our dis-cussion of input and output will be limited in this tutorial, we point out that Matlabprovides flexible file I/O options as well; for more information, the reader can referto Matlab’s documentations.6 3 SOME TECHNICAL DETAILSFigure 1: Matlab’s program development e nvironment.74 Flow of Control: Branch and Loop StructuresIn this section, we discuss the two


View Full Document

UMBC MATH 426 - An Introduction to MATLAB Program

Download An Introduction to MATLAB Program
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 An Introduction to MATLAB Program 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 An Introduction to MATLAB Program 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?