A GUIDE FOR USING MATLAB IN COMPUTER SCIENCE AND COMPUTER ENGINEERING TABLE OF CONTENTS

Similar documents
Computer Programming in MATLAB

Introduction to Engineering gii

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

A General Introduction to Matlab

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

Dr Richard Greenaway

Part #1. A0B17MTB Matlab. Miloslav Čapek Filip Kozák, Viktor Adler, Pavel Valtr

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

A Guide to Using Some Basic MATLAB Functions

Introduction to MATLAB Programming

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Introduction to MATLAB

Finding, Starting and Using Matlab

LAB 1 General MATLAB Information 1

MATH 3511 Basics of MATLAB

McTutorial: A MATLAB Tutorial

Starting MATLAB To logon onto a Temple workstation at the Tech Center, follow the directions below.

Matlab Tutorial, CDS

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

2.0 MATLAB Fundamentals

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Chapter 1 MATLAB Preliminaries

PIV Programming. Today s Contents: 1. Matlab Programming 2. An example of PIV in Matlab code 3. EDPIV 4. PIV plugin for ImageJ 5.

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

MATH 5520 Basics of MATLAB

Ebooks Chemical Engineering

Lecturer: Keyvan Dehmamy

Introduction to MATLAB

General MATLAB Information 1

Introduction to MATLAB

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

Some elements for Matlab programming

Dr Richard Greenaway

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

MATLAB Premier. Asst. Prof. Dr. Melik DÖLEN. Middle East Technical University Department of Mechanical Engineering 10/30/04 ME 304 1

MATLAB Premier. Middle East Technical University Department of Mechanical Engineering ME 304 1/50

Lab 1 Intro to MATLAB and FreeMat

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Arithmetic and Logic Blocks

MATLAB Constants, Variables & Expression. 9/12/2015 By: Nafees Ahmed

Introduction to Scientific and Engineering Computing, BIL108E. Karaman

Part V Appendices c Copyright, Todd Young and Martin Mohlenkamp, Department of Mathematics, Ohio University, 2017

Lecture 2: Variables, Vectors and Matrices in MATLAB

Introduction to Matlab

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

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

CSE/Math 456 and CSE/Math 550 Matlab Tutorial and Demo

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

MBI REU Matlab Tutorial

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

Digital Image Analysis and Processing CPE

Matlab and Octave: Quick Introduction and Examples 1 Basics

Introduction to MATLAB Practical 1

Chapter 2. MATLAB Fundamentals

AN INTRODUCTION TO MATLAB

A Brief Introduction to MATLAB

Math 2250 MATLAB TUTORIAL Fall 2005

Introduction to MATLAB

Matlab Tutorial. Get familiar with MATLAB by using tutorials and demos found in MATLAB. You can click Start MATLAB Demos to start the help screen.

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

To start using Matlab, you only need be concerned with the command window for now.

FreeMat Tutorial. 3x + 4y 2z = 5 2x 5y + z = 8 x x + 3y = -1 xx

MATLAB Lecture 1. Introduction to MATLAB

PART 1 PROGRAMMING WITH MATHLAB

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

A = [1, 6; 78, 9] Note: everything is case-sensitive, so a and A are different. One enters the above matrix as

Introduction to MATLAB

MATLAB Fundamentals. Berlin Chen Department of Computer Science & Information Engineering National Taiwan Normal University

ELEMENTARY MATLAB PROGRAMMING

Matlab Programming Introduction 1 2

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

A very brief Matlab introduction

Julia Calculator ( Introduction)

EP375 Computational Physics

1 Introduction to Matlab

Matlab Introduction. Scalar Variables and Arithmetic Operators

Appendix A: MATLAB Basics

General Information. There are certain MATLAB features you should be aware of before you begin working with MATLAB.

Course Layout. Go to follow instr. Accessible within campus (only for the first download)

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

A/D Converter. Sampling. Figure 1.1: Block Diagram of a DSP System

Laboratory 1 Octave Tutorial

University of Alberta

Introduction to MatLab. Introduction to MatLab K. Craig 1

Programming in Mathematics. Mili I. Shah

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations

MATLAB SUMMARY FOR MATH2070/2970

Scientific Computing Lecture Series Introduction to MATLAB Programming

Stokes Modelling Workshop

Creates a 1 X 1 matrix (scalar) with a value of 1 in the column 1, row 1 position and prints the matrix aaa in the command window.

Fundamentals of MATLAB Usage

Matrices. A Matrix (This one has 2 Rows and 3 Columns) To add two matrices: add the numbers in the matching positions:

Introduction to GNU-Octave

Transcription:

A GUIDE FOR USING MATLAB IN COMPUTER SCIENCE AND COMPUTER ENGINEERING MARC THOMAS AND CHRISTOPHER PASCUA TABLE OF CONTENTS 1. Language Usage and Matlab Interface 1 2. Matlab Global Syntax and Semantic Structure 2 3. Matlab Numerical Mathematics 3 4. Matlab Symbolic Mathematics 5 Table I - General Operators and Constructs 2 Table II - Constants and Special Variables 3 Table III - General Commands and Functions 3 Table IV - Matlab Matrix Functions 4 Table V - Matlab Scalar and Element-wise Functions 4 Table VI - Symbolic Math Functions 5 Appendix I: Sample m-file Functions 7 Appendix II: Sample m-file Scripts 9 1. Language Usage and Matlab Interface American grammar is unusual in that punctuation, even if it is not part of the quote, is absorbed into the quote; for example, He said oh, damn, paused, then added assessment fails me again. In this document I will use proper English grammar so that punctuation not intrinsic to the quote will be used as a delimeter, and appear outside the quote; for example, He said oh, damn, paused, then added assessment fails me again. Commands to be entered into Matlab on the command line will be set in TEX fixed, nonproportional teletype font; for example, asking Matlab to compute the expression 4.56 cos(2.5π/4 + π/8) + exp( π) 1

2 MARC THOMAS AND CHRISTOPHER PASCUA can be done by typing 4.56*cos(2.5*pi/4 + pi/8) + exp(-pi); ans at the Matlab command prompt. All of the following refers to usage under Matlab version 7.13 (R2011b). Like any software package there are some situations which require additional advice. 1. Sometimes the Desktop gets corrupted with overlapping windows, etc.; do the following to restore it: click Desktop -> Desktop Layout -> Default 2. To tile a graph on the Desktop, click (on the graph) Desktop -> Dock Figure 3. You may need to change the Current Folder (probably workspace) if your m-files are in another directory; otherwise you will get a file (or function) not found error. If you start Matlab from a cmd.exe prompt, then this will be the current working directory. 4. If you want to know about, say matlab widgit you can type doc matlab widgit and help pages will come up. An on-line reference can be found at http://www.mathworks.com/help/techdoc/matlab product page.html 2. Matlab Global Syntax and Semantic Structure Matlab, like any programming language has standard operators for assignment and arithmetic and logical operations and most of these apply to the toolkits as well. One fine point is that some operations (such as multiplication) can be done either entry-wise or operator-wise, so the dot -operator is used as a prefix in order to distinguish the two operations. Table I - General Operators and Constructs = assignment operator ( ) parentheses for expressions + - * scalar/matrix addition subtraction and multiplication ˆ scalar/matrix exponentialtion / \ right and left matrix division.+.-.* entry-wise matrix addition subtraction and multiplication.^ entry-wise matrix exponentiation./ entry-wise matrix division matrix transpose ; complete command and supress output... continue on next line remark: one needs both right and left division for matrices because matrix multiplication is not commutative and we can form both inv(a)*b = (A\B) and B*inv(A) = (B/A). The following constants and special variables are built in.

3 Table II - Constants and Special Variables i or j the square root of 1 pi π = 3.1415926... ans variable holding the answer to the last computation eps the machine epsilon, i.e. the smallest floating point number h such that 1+h > 1 inf infinity Note that you can do a calculation without assignment and then use ans to recall the result; for example (3.45 * 2.18)/2.07; ans The are some very useful general commands and functions. Table III - General Commands and Functions clear clear all non-permanent variables clear all clear variables, functions, etc. clf clear figures who list all variables whos list all variables together with their sizes ver list all installed toolboxes 3. Matlab Numerical Mathematics The Matlab core contains fundamental numerical operations to handle both real and complex scalars and matrices with double-precision floating point accuracy. Data Types. There is only one data type and that is a matrix with (possibly complex) doubleprecision entries. Row vectors are 1 x n matrices v where n = length(v). Column vectors are transposes of row vectors. Scalars are 1 x 1 matrices. The size function can be used on the command line or in function calls to find out the number of rows and columns; for example z = 3.5; [m n] = size(z) will print out m = 1 and n = 1, whereas z = [3.4-5.6]; [m n] = size(z) will print out m = 1 and n = 2. Identifiers. Variable are case sensitive and must start with a letter; following that, letters, digits, and underscores can be added (but only the first 19 characters are significant). Do not use the hyphen in either variable or function names.

4 MARC THOMAS AND CHRISTOPHER PASCUA Table IV - Matlab Matrix Functions Function Operation size [m n] = size(a) returns rows and columns of A length returns the length of a vector det returns the determinant of a square matrix inv returns the inverse of the matrix norm returns the l 2 -norm of the matrix cond returns the condition number of the matrix A, i.e. norm(a)*norm(inv(a)) rank returns the rank of the matrix, i.e. the maximum number of linearly independent rows eig [V D] = eig(a) returns eigenvalues and eigenvectors of the matrix A Certain functions operate element-wise on matrices; some important ones are: Table V - Matlab Scalar and Element-wise Functions Type Function Operation Trigonometric cos, sin, tan, cosine, sine, tangent acos, asin, atan, inverse cosine, sine, tangent General abs absolute value log (natural) logarithm exp exponential function sqrt square root sign sign (positive, negative, etc.) Complex real, imag real and imaginary parts cong complex conjugate Some Linear Algebra. Scalars are entered into Matlab the same as in (almost) any programming language with the assignment operator, for example z = 3.5 Note that if you do not end an assignment with a semicolon, Matlab will confirm the value(s) of the assignment. You may want this interactively, but in most m-file scripts you would want the assignment to be silent, so you would terminate the assignment with a semicolon. z = 3.5; Row vectors (1 x n matrics) are entered with the values separated by white space between square brackets, for example x = [2.3-3.4 0.3 1.7]; You may enter complex vectors as well but be careful with whitespace, for example, try y = [2.3-3.4*j 0.3+1.7*j]; whos Note that you can check assignment sizes with the operator whos. The transpose operator (single quote ) can be used to make a column vector from a row vector (or vice-versa), for example z = x ;

5 4. Matlab Symbolic Mathematics The Matlab Symbolic Math Toolbox contains functions designed to do mathematics symbolically, obtaining closed-form solutions whenever possible. Obviously, solving certain types of, for example, differential equations may not yield closed-form solutions so these operations may fail, and it is the responsibility of the user to understand this. Table VI - Symbolic Math Functions Function Operation Calculus diff differentiate int integrate

6 MARC THOMAS AND CHRISTOPHER PASCUA References [1] ABET, Criteria For Accrediting Computing Programs, 2010 11, online see http://www.abet.org/forms.shtml#for Computing Programs Only -> 2010-2011 Criteria (pdf) -> Proposed Changes to the Accreditation Criteria (pdf) [2] ABET, Criteria For Accrediting Engineering Programs, 2010 11, online see http://www.abet.org/forms.shtml#for Engineering Programs Only -> 2010-2011 Criteria (pdf) -> Proposed Changes to the Accreditation Criteria (pdf) [3] ACM, Computer Science Body of Knowledge, 2001, 2008, online see http://www.acm.org/education/education/education/curric vols/cc2001.pdf and the Interim Revision at http://www.acm.org//education/curricula/computerscience2008.pdf [4] ACM, Curriculum Guidelines for Undergraduate Degree Programs in Computer Engineering, online see http://www.acm.org/education/education/curric vols/ce-final-report.pdf [5] Estes, Allen and Ressler, Stephen, Surviving ABET Accreditation: Satisfying the Demands of Criterion 3, online see http://digitalcommons.calpoly.edu/cgi/viewcontent.cgi?article=1000&context=aen fac

7 Appendix I - Sample m-file Functions The unit-step function: ustep.m function y = ustep(s) the heaviside (or unit step) function example (using both ramp() and ustep()): t = -5:0.01:5; plot(t, 3*ramp(t+3) - 6*ramp(t+1) + 3*ramp(t) - 3*ustep(t - 3); important: matlab seems to "assume" that user-defined functions are automatically vector (not scalar) functions. So we first have to get the size: [m n] = size(s); case: scalar if n == 1 && s < 0 y = 0; elseif n == 1 && s == 0 y = 0.5; elseif n == 1 && s > 0 y = 1; elseif n == 1 error( invalid real value: s=f\n, s); case: vector (important: semicolon before "end;" keeps it silent) else for i = 1:n, y(i) = ustep(s(i)); end; end

8 MARC THOMAS AND CHRISTOPHER PASCUA The ramp function: ramp.m function y = ramp(s) ramp function resulting from integrating the heaviside (or unit step function) example (using both ramp() and ustep()): t = -5:0.01:5; plot(t, 3*ramp(t+3) - 6*ramp(t+1) + 3*ramp(t) - 3*ustep(t - 3); important: matlab seems to "assume" that user-defined functions are automatically vector (not scalar) functions. So we first have to do get the size: [m n] = size(s); case: scalar if n == 1 && s < 0 y = 0; elseif n == 1 && s == 0 y = 0; elseif n == 1 && s > 0 y = s; elseif n == 1 error( invalid real value: s=f\n, s); case: vector (important: semicolon before "end;" keeps it silent) else for i = 1:n, y(i) = ramp(s(i)); end; end

Appendix I - Sample m-file Scripts 9

10 MARC THOMAS AND CHRISTOPHER PASCUA Department of Computer and Electrical Engineering, and Computer Science, California State University at Bakersfield, 9001 Stockdale Hwy, Bakersfield, CA, 93311-1022, USA E-mail address: marc@cs.csubak.edu