CME 192: Introduction to Matlab

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

Teaching Manual Math 2131

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

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

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

Dr Richard Greenaway

Introduction to MATLAB

EGR 111 Introduction to MATLAB

MATLAB GUIDE UMD PHYS401 SPRING 2012

Introduction to MATLAB Programming

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

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

Vectors and Matrices. Chapter 2. Linguaggio Programmazione Matlab-Simulink (2017/2018)

Introduction to MATLAB Programming

Finding, Starting and Using Matlab

Lecture 2: Variables, Vectors and Matrices in MATLAB

Control Structures. March 1, Dr. Mihail. (Dr. Mihail) Control March 1, / 28

MATLAB: Introduction Part 1

Programming in Mathematics. Mili I. Shah

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

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

EGR 102 Introduction to Engineering Modeling. Lab 05A Managing Data

2.0 MATLAB Fundamentals

One-dimensional Array

Introduction to MATLAB

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

Chapter 2. MATLAB Fundamentals

SMS 3515: Scientific Computing Lecture 1: Introduction to Matlab 2014

PHYS 210: Introduction to Computational Physics MATLAB Exercises 2

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

Interactive Computing with Matlab. Gerald W. Recktenwald Department of Mechanical Engineering Portland State University

Chapter 1 MATLAB Preliminaries

Introduction to MATLAB for Engineers, Third Edition

Introduction to Matlab

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

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

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

Chapter 2. MATLAB Basis

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

An Introduction to MATLAB

Scientific Computing with MATLAB

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

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

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

AN INTRODUCTION TO MATLAB

Introduction to MATLAB

Introduction to MATLAB. Computational Probability and Statistics CIS 2033 Section 003

PART 1 PROGRAMMING WITH MATHLAB

Mathematical Operations with Arrays and Matrices

McTutorial: A MATLAB Tutorial

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

STAT/MATH 395 A - PROBABILITY II UW Winter Quarter Matlab Tutorial

Lecture 1: Hello, MATLAB!

MATLAB BASICS. M Files. Objectives

CITS2401 Computer Analysis & Visualisation

MBI REU Matlab Tutorial

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

Introduction to MATLAB 7 for Engineers

Worksheet 1. Hello Matlab

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix.

MATLAB for beginners. KiJung Yoon, 1. 1 Center for Learning and Memory, University of Texas at Austin, Austin, TX 78712, USA

Identity Matrix: >> eye(3) ans = Matrix of Ones: >> ones(2,3) ans =

A Quick Tutorial on MATLAB. Zeeshan Ali

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

6.094 Introduction to MATLAB January (IAP) 2009

ENGR 1181 MATLAB 02: Array Creation

MATLAB. Devon Cormack and James Staley

PROGRAMMING AND ENGINEERING COMPUTING WITH MATLAB Huei-Huang Lee SDC. Better Textbooks. Lower Prices.

Introduction to Matlab

Introduction to MATLAB

A Quick Introduction to MATLAB/Octave. Kenny Marino, Nupur Chatterji

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

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

Chapter 1 Introduction to MATLAB

MATLAB GUIDE UMD PHYS401 SPRING 2011

Introduction to MATLAB Programming. Chapter 3. Linguaggio Programmazione Matlab-Simulink (2017/2018)

MATLAB Introduction to MATLAB Programming

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

Arrays and Matrix Operations

Introduction to Matlab. By: Hossein Hamooni Fall 2014

Introduction to Engineering gii

Matrix Manipula;on with MatLab

1 Introduction to MATLAB

Introduction to Scientific Computing with Matlab

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

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

Introduction to MATLAB for Numerical Analysis and Mathematical Modeling. Selis Önel, PhD

Matlab- Command Window Operations, Scalars and Arrays

MATLAB Lecture 1. Introduction to MATLAB

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Introduction to MATLAB LAB 1

INTRODUCTION TO NUMERICAL ANALYSIS

A General Introduction to Matlab

Introduction to MATLAB programming: Fundamentals

1 Introduction to MATLAB

QUICK INTRODUCTION TO MATLAB PART I

Some elements for Matlab programming

Dr Richard Greenaway

Transcription:

CME 192: Introduction to Matlab Matlab Basics Brett Naul January 15, 2015

Recap Using the command window interactively Variables: Assignment, Identifier rules, Workspace, command who and whos Setting the output format using the command format Basic computations: help elfun gives the list of elementary math functions

Vectors and Matrices Matlab is short for Matrix Laboratory. 5 scalar 3 7 4 3 1 column vector 5 88 3 11 1 4 row vector 9 6 3 5 7 2 4 33 8 3 3 matrix Vectors and Matrices in Matlab store values of the same type only.

Creating Row Vectors Comma or space separated values between brackets 1 EDU>> v = [ 1 2 3 4] 2 v = 3 1 2 3 4 4 EDU>> v = [1, 2, 3, 4] 5 v = 6 1 2 3 4 The colon operator 1 EDU>> v = 1:5 2 v = 3 1 2 3 4 5 4 EDU>> v = 1:2:5 % step size of 2 5 v = 6 1 3 5

Creating Row Vectors linspace function 1 EDU>> linspace(1,5,3) 2 ans = 3 1 3 5 Concatenating row vectors 1 EDU>> v1 = 1:2:5; 2 EDU>> v2 = linspace(10, 14, 5); 3 EDU>> v = [v1 v2] 4 v = 5 1 3 5 10 11 12... 13 14

Creating Column Vectors Semicolon-separated values between brackets 1 EDU>> v = [ 1; 2; 3; 4] 2 v = 3 1 4 2 5 3 6 4 Colon/linspace create row vectors, need to transpose: 1 EDU>> v = 1:2:5; 2 EDU>> v = v' 3 v = 4 1 5 3 6 5

Referring to and Modifying Elements in Vectors 1 2 3 4 5 6 7 8 9 10 1 3 5 7 9 3 6 9 12 15 Accessing a single element 1 EDU>> v(5) 2 ans = 3 9 Accessing a subset of a vector using the colon operator 1 EDU>> v(4:6) 2 ans = 3 7 9 3

Referring to and Modifying Elements in Vectors 1 2 3 4 5 6 7 8 9 10 1 3 5 7 9 3 6 9 12 15 Accessing a subset of a vector using an index vector 1 EDU>> v([1 5 10]) 2 ans = 3 1 9 15 4 % Here [ 1 5 10] is the index vector Logical indexing 1 EDU>> v(v<10) 2 ans = 3 1 3 5 7 9 3... 6 9

Creating Matrices Explicitly typing values 1 EDU>> mat = [ 4 3 1; 2 5 6] 2 mat = 3 4 3 1 4 2 5 6 5 EDU>> mat2 = [2:4 ; 1:2:5] 6 mat2 = 7 2 3 4 8 1 3 5 1 EDU>> mat = [ 4 3 1; 2 5 ] 2??? Error using ==> vertcat 3 CAT arguments dimensions are not consistent. There must always be the same number of values in each row.

Creating Matrices Special functions : zeros, ones, eye, rand 1 EDU>> zeros(2,3) 2 ans = 3 0 0 0 4 0 0 0 5 EDU>> zeros() 6 ans = 7 0 0 8 0 0 9 EDU>> ones(2,3) 10 ans = 11 1 1 1 12 1 1 1 13 EDU>> eye(2) 14 ans = 15 1 0 16 0 1

Creating Matrices Constructing matrices in block form 1 EDU>> B = [ 1 2 ; 3 4]; 2 EDU>> C = [B zeros(2); ones(2) eye(2)] 3 C = 4 1 2 0 0 5 3 4 0 0 6 1 1 1 0 7 1 1 0 1 tiled block matrices using repmat. 1 EDU>> A = repmat(eye(2), 2, 1) 2 A = 3 1 0 4 0 1 5 1 0 6 0 1

Creating Matrices Creating diagonal matrices: diag, blkdiag 1 EDU>> diag([2,3,4]) 2 ans = 3 2 0 0 4 0 3 0 5 0 0 4 6 EDU>> A = blkdiag(2*eye(2), ones(2)) 7 A = 8 2 0 0 0 9 0 2 0 0 10 0 0 1 1 11 0 0 1 1 Matlab provides a number of special matrices (magic, hadamard,...).

Referring to and Modifying Elements in Matrices Accessing an element 1 2 3 1 11 12 13 2 21 22 23 3 31 32 33 1 EDU>> A = [11 12 13; 21 22 23; 31 32 33] 2 EDU>> A(2,3) 3 ans = 4 23

Referring to and Modifying Elements in Matrices Accessing a row/column 1 2 3 1 11 12 13 2 21 22 23 3 31 32 33 1 EDU>> A(2, :) 2 ans = 3 21 22 23 4 EDU>> A(:,2) 5 ans = 6 12 7 22 8 32

Referring to and Modifying Elements in Matrices Accessing a block 1 2 3 1 11 12 13 2 21 22 23 3 31 32 33 1 EDU>> A(1:2, 2:3) 2 ans = 3 12 13 4 22 23 5 EDU>> A(:, 2:3) 6 ans = 7 12 13 8 22 23 9 32 33

Exercises Using the colon operator, create the following vectors 1 3 4 5 6 2 1.0000 1.5000 2.0000 2.5000... 3.0000 3 5 4 3 2 Using the linspace function, create the following vectors: 1 4 6 8 2 3 6 9 12 15 3 9 7 5

Exercises Create a 4 2 matrix of all zeros and store it in a variable. Then replace the second row in the matrix with a 3 and a 6. Create a 3 5 matrix of random integers, each in the range from -5 to 5; store it in a variable. Create another matrix that stores for each element the absolute value of the corresponding element in the original matrix. Delete the the third row. Create a vector x = [ 1 2 3]. Expand the vector x to have length 6 and assign x(6) = 4. Create a random 4 1 vector and get the index of the largest element.

Data Types Strings and Cell Arrays Strings are vectors of characters Denoted by Ex: str = hello Cell arrays generalize matrices to allow for arbitrary entries, not just numbers Denoted by {} Ex: cll = { hello, 123, 456, goodbye }

Elementary Matrix and Array Operations Operation Matrix sense Element wise Addition + + Subtraction - - Multiplication *.* Left Division \.\ Right Division /./ Exponentiation ˆ.ˆ Elementary matrix and array operations Matlab notation Right division: a/b Left division: a\b Mathematical Equivalent a b a

Matrix and Array Operations dot, cross product of two vectors 1 EDU>> i = [ 1 0 0 ]; j = [ 0 1 0 ]; 2 EDU>> dot(i,j) 3 ans = 4 0 5 EDU>> cross(i,j) 6 ans = 7 0 0 1

Matrix and Array Operations Transpose of a matrix/vector 1 EDU>> A = [ 1 2 3; 4 5 6] 2 A = 3 1 2 3 4 4 5 6 5 EDU>> A' 6 ans = 7 1 4 8 2 5 9 3 6 10 EDU>> x = [1 2] 11 x = 12 1 2 13 EDU>> x' 14 ans = 15 1 16 2

Matrix and Array Operations Most functions which act on a scalar can be given a matrix argument, in which case the functions are computed element wise. 1 EDU>> A = [ 2 2 ; 0 2] 2 A = 3 2 2 4 0 2 5 EDU>> sqrt(a) 6 ans = 7 1.4142 1.4142 8 0 1.4142

Matrix and Array Operations Functions of a matrix in linear algebra sense are signified by names ending in m: expm, funm, logm, sqrtm 1 EDU>> B = sqrtm(a) 2 B = 3 1.4142 0.7071 4 0 1.4142 5 EDU>> B*B 6 ans = 7 2.0000 2.0000 8 0 2.0000

Introduction to Matlab programming Simple I/O input function : used as an assignment statement. 1 EDU>> length = input('enter the length: ') 2 Enter the length: 3 3 length = 4 3 If a character or string input is desired, s must be added after the prompt. 1 EDU>> name = input('enter your name: ') 2 Enter your name: Brett 3??? Error using ==> input 4 Undefined function or variable 'Brett'.

Introduction to Matlab programming Simple I/O 1 EDU>> name = input('enter your name: ', 's') 2 Enter your name: Brett 3 name = 4 Brett Output Statements: disp and fprintf disp: Displays the result of an expression or a string. 1 EDU>> disp('hello') 2 Hello 3 EDU>> disp(3*7) 4 21 5 EDU>> x = 5; y = 2; 6 EDU>> disp(x*y) 7 10

Introduction to Matlab programming Simple I/O fprintf: Prints formatted output to the screen. 1 EDU>> fprintf('the value is %d, for... sure! \n', 3^2); 2 The value is 9, for sure! The string passed to the fprintf function is called the fomat string.

Introduction to Matlab programming Simple I/O 1 EDU>> fprintf('the value is %d, for sure!... \n', 3^2); 2 The value is 9, for sure! In the above block, %d is an example of a place holder. The character in the placeholder is called the conversion character. %d decimal integers %f floats %e exponential notation %c single characters %s strings List of simple place holders

Introduction to Matlab programming Simple I/O 1 EDU>> fprintf('%6.3f\n', pi) 2 3.142 3 EDU>> fprintf('%6.3e\n', pi) 4 3.142e+00 In the above example, % character denotes the start of a format specifier requesting a field of width 6 with 3 digits after the decimal point and \n denotes a new line. For a negative number, a minus sign occupies one position of the field width.

Introduction to Matlab programming Simple I/O A minus sign just after the % character causes the field to be left justified. 1 EDU>> fprintf('%5.0f\n%5.0f\n', 9, 103) 2 9 3 103 4 EDU>> fprintf('% 5.0f\n% 5.0f\n', 9, 103) 5 9 6 103

Introduction to Matlab programming Simple I/O What if more numbers are supplied to be printed than there are format specifiers? 1 EDU>> fprintf('% 5.0f\n% 5.0f\n', 9,... 103, 11) 2 9 3 103 4 11 The format specifiers are simply reused. If a matrix is provided, elements are taken down the first column, then the second column and so on. This feature can be used to avoid loops.

Exercises Create the following output using not more than a single fprintf statement: 1 30 miles/hour = 48 kilometers/hour 2 40 miles/hour = 64 kilometers/hour 3 60 miles/hour = 96 kilometers/hour 4 70 miles/hour = 112 kilometers/hour