An Introduction to MATLAB Programming

Similar documents
MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

AMATH 352: MATLAB Tutorial written by Peter Blossey Department of Applied Mathematics University of Washington Seattle, WA

Teaching Manual Math 2131

LAB 2 VECTORS AND MATRICES

MATLAB - Lecture # 4

Lecture 2: Variables, Vectors and Matrices in MATLAB

FF505 Computational Science. Miscellanea. Marco Chiarandini

An Object Oriented Finite Element Library

MATLAB Introduction To Engineering for ECE Topics Covered: 1. Creating Script Files (.m files) 2. Using the Real Time Debugger

1 Introductory Remarks. 2 Programming in Matlab. Math 4330 Sec. 1, Matlab Assignment # 3,

MATH 5520 Basics of MATLAB

MATH 3511 Basics of MATLAB

Fundamentals of MATLAB Usage

Tropical Orthogonal Representations

Introduction to MATLAB

Introduction. Matlab for Psychologists. Overview. Coding v. button clicking. Hello, nice to meet you. Variables

EE 216 Experiment 1. MATLAB Structure and Use

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

On JAM of Triangular Fuzzy Number Matrices

Matlab Introduction. Scalar Variables and Arithmetic Operators

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

Matlab course at. P. Ciuciu 1,2. 1: CEA/NeuroSpin/LNAO 2: IFR49

Scientific Computing: Lecture 1

CITS2401 Computer Analysis & Visualisation

MATLAB COURSE FALL 2004 SESSION 1 GETTING STARTED. Christian Daude 1

Introduction to Scientific Computing with Matlab

McTutorial: A MATLAB Tutorial

CSC 373: Algorithm Design and Analysis Lecture 8

CSE/Math 485 Matlab Tutorial and Demo

Introduction to MATLAB programming: Fundamentals

Session 3 Introduction to SIMULINK

LECTURE 1. What Is Matlab? Matlab Windows. Help

CM0340 Tutorial 2: More MATLAB

MATLAB. Miran H. S. Mohammed. Lecture 1

Introduction to MATLAB

MATLAB and Numerical Analysis

Introduction to MATLAB

3. Replace any row by the sum of that row and a constant multiple of any other row.

Array Accessing and Strings ENGR 1187 MATLAB 3

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

Programming 1. Script files. help cd Example:

TUTORIAL 1 Introduction to Matrix Calculation using MATLAB TUTORIAL 1 INTRODUCTION TO MATRIX CALCULATION USING MATLAB

MATLAB: The greatest thing ever. Why is MATLAB so great? Nobody s perfect, not even MATLAB. Prof. Dionne Aleman. Excellent matrix/vector handling

Computational Methods of Scientific Programming

Colorado State University Department of Mechanical Engineering. MECH Laboratory Exercise #1 Introduction to MATLAB

Chapter 2. MathScript

Matlab Programming MET 164 1/24

Math 2250 MATLAB TUTORIAL Fall 2005

INTRODUCTION TO MATLAB PROGRAMMING Lec 1.1: MATLAB Basics

HA 450. Application Development for SAP HANA COURSE OUTLINE. Course Version: 12 Course Duration:

Algebraic Graph Theory- Adjacency Matrix and Spectrum

3 Introduction to MATLAB

TUTORIAL MATLAB OPTIMIZATION TOOLBOX

MATLAB Vocabulary. Gerald Recktenwald. Version 0.965, 25 February 2017

Polymath 6. Overview

MATLAB NOTES. Matlab designed for numerical computing. Strongly oriented towards use of arrays, one and two dimensional.

Shell Script Programming

Dr Richard Greenaway

Introduction to Engineering gii

SECTION 2: PROGRAMMING WITH MATLAB. MAE 4020/5020 Numerical Methods with MATLAB

An Introduction to MATLAB and the Control Systems toolbox Aravind Parchuri, Darren Hon and Albert Honein

CME 192: Introduction to Matlab

5. Special Purpose Languages

MATLAB Tutorial. Primary Author: Shoumik Chatterjee Secondary Author: Dr. Chuan Li

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

1 Introduction. 2 Useful linear algebra (reprise) Introduction to MATLAB Reading. Spencer and Ware (2008), secs. 1-7, 9-9.3,

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

Matlab Advanced Programming. Matt Wyant University of Washington

Using MATLAB, SIMULINK and Control System Toolbox

Matlab Tutorial, CDS

OVERVIEW DISPLAYING NUMBERS IN SCIENTIFIC NOTATION ENTERING NUMBERS IN SCIENTIFIC NOTATION

MATLAB for Experimental Research. Fall 2018 Vectors, Matrices, Matrix Operations

Linear Algebra in LabVIEW

Sand Pit Utilization

Introduction to MATLAB. Simon O Keefe Non-Standard Computation Group

A Tour of Matlab for Math 496, Section 6

A Brief Introduction to MATLAB

Some elements for Matlab programming

Programming in Mathematics. Mili I. Shah

hp calculators hp 39g+ & hp 39g/40g Using Matrices How are matrices stored? How do I solve a system of equations? Quick and easy roots of a polynomial

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

Part VI. Scientific Computing in Python. Alfredo Parra : Scripting with Python Compact Max-PlanckMarch 6-10,

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

3 An Introductory Demonstration Execute the following command to view a quick introduction to Matlab. >> intro (Use your mouse to position windows on

Arrays and Matrix Operations

Game Mathematics. (12 Week Lesson Plan)

Finite Fields can be represented in various ways. Generally, they are most

LabVIEW MathScript Quick Reference

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

Object Oriented Methods : Deeper Look Lecture Three

1 INTRODUCTION. Purpose. Audience. Figure 1-0. Table 1-0. Listing 1-0.

Matlab Basics. Paul Schrimpf. January 14, Paul Schrimpf () Matlab Basics January 14, / 24

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

Dynamic Programming. Outline and Reading. Computing Fibonacci

Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA

Mathworks (company that releases Matlab ) documentation website is:

Chapter 4 Branching Statements & Program Design

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

Introduction to MATLAB

Transcription:

An Introduction to MATLAB Programming Center for Interdisciplinary Research and Consulting Department of Mathematics and Statistics University of Maryland, Baltimore County www.umbc.edu/circ Winter 2008

2 2 MATRIX MANIPULATION 1 Introduction In this tutorial we introduce the basics of programming in Matlab. We will start by reviewing the elements of structured programming; next, we will begin our discussion of Matlab programming. The flow of control and the related Matlab keywords will be mentioned along with several examples for the purpose of illustration. Another topic which we will discuss is the use of Matlab functions. At the end, we will point the audience to the appropriate sections of Matlab s documentations for more information on Matlab programming. The outline of the major objectives of this workshop is the following: Matrix Manipulation Data Analysis Basics of Structured Programming Basic Input and Output Flow of Control in a Program 2 Matrix Manipulation In addition to performing algebraic operations with matrices, Matlab provides many useful commands for manipulating matrices in other ways. The reshape command is used to change the shape of a matrix: For instance, reshape(a, m, n) produces an m-by-n matrix whose elements are taken columnwise from A: >> A = [0 1 2; 3 6 9] A = 0 1 2 3 6 9 >> reshape(a,3,2) 0 6 3 2 1 9

The diag, tril, and triu commands can be used to extract diagonal, lower- or upper-triangular portions of a matrix. Examples: 3 >> A = [1 2 3; 4 5 6; 7 8 9] A = 1 2 3 4 5 6 7 8 9 >> diag(a) 1 5 9 >> diag(a,-1) 4 8 >> tril(a) 1 0 0 4 5 0 7 8 9 >> triu(a) 1 2 3

4 3 DATA ANALYSIS 0 5 6 0 0 9 >> triu(a,1) 0 2 3 0 0 6 0 0 0 3 Data Analysis Matlab also includes a number of useful functions for data analysis of vectors and matrices. The min and max commands return the minimum and maximum elements of a vector, as well as the index for that element, as in this example: >> x = [1 5-2 3 4] x = 1 5-2 3 4 >> [min(x) max(x)] -2 5 >> [m,i] = min(x) m = -2 i = 3

5 >> [m,j] = max(x) m = j = 5 2 When operating on matrices, min and max will return a vector containing the minimum (or maximum) element in each column of the matrix, and an index specifying which element in the column is the minimum (or maximum), while sum will return a vector containing the sums of each column: >> A = [0-1 2; 1 2-4; 5-3 -4] A = 0-1 2 1 2-4 5-3 -4 >> max(a) 5 2 2 >> [m,i] = min(a) m = 0-3 -4 i = 1 3 2 >> sum(a)

6 4 STRUCTURED PROGRAMMING IN MATLAB 6-2 6 The examples included here cover only a small subset of Matlab commands and their functionality. The user is encouraged to experiment on his own, and consult the Matlab documentation for more information. 4 Structured Programming in Matlab Matlab provides extensive math and graphics functions along with a powerful highlevel programming language. Programmers can choose to program using the classical structured programming approach, but it is also possible to do object-oriented programming in Matlab. Our discussion, however, will be limited to structured programming in Matlab. 4.1 Structured Programming Structured programming (modular programming) is a software development paradigm in which problems are solved in a top-down fashion. As a program gets larger, it is sub-divided into sub-programs to provide code that is easier to read, debug, and maintain; also this approach facilitates reuse of commonly used procedures (functions). A natural starting point in our discussion of structured programming in Matlab will be a quick review of the elements of structured programming. 4.2 Basic Constructs of Structured Programming Recall that an algorithm is a finite set of instructions for accomplishing a given task which given an initial state will terminate in a defined end state. And a computer program is an implementation of a given algorithm in a programming language. In the discussion that follows, we will use Matlab programming language as our implementation tool. There are three main programming constructs: 1. Sequence 2. Branch (Decision) 3. Loop The Sequence construct refers to writing a group of programming statements in a sequence. The Branch construct enables us to change the flow of control if a given condition is satisfied, and finally the Loop construct enables the program to run a statement (or a group of statements) a number of times.

5 Some Technical Details 7 Before we go deeper in our discussion of Matlab programming, we take care of some technical details in this section. We will first mention naming conventions for Matlab program files; next, we will talk about Matlab s program development environment, and then have a brief discussion of input and output. 5.1 File Names in Matlab Matlab programming codes are saved in files with extension.m. This gives rise to the so-called Matlab M-files. For example, a program that computes the roots of a quadratic polynomial may be saved in a file named quadroots.m. An M-file may contain a Matlab script or a Matlab function; the distinction between the two types will become clear in the course of our discussion. 5.2 Development Environment Matlab programs can be created using any text editor. In a Unix environment, one can use editors such as vi, emacs, nedit, etc. In Windows environment, notepad and alike can be used. In addition, Matlab provides a user-friendly editor where programmers can create their programs (and also debug them). To access Matlab s editor, type the following in Matlab command prompt, edit this opens Matlab s editor (Figure 1). 5.3 A Word on Input and Output Before doing any programming we recall that a typical computer program (in structured programming) receives data as input, does some processing on the given data and provides output. Hence, input and output constitute an important part of any computer program. The programming examples in this tutorial do not involve interactive (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 discuss these commands at appropriate places throughout this tutorial. Although our discussion of input and output will be limited in this tutorial, we point out that Matlab provides flexible file I/O options as well; for more information, the reader can refer to Matlab s documentations.

8 5 SOME TECHNICAL DETAILS Figure 1: Matlab s program development environment.