Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.

Slides:



Advertisements
Similar presentations
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 3.
Advertisements

EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Matlab Matlab is a powerful mathematical tool and this tutorial is intended to be an introduction to some of the functions that you might find useful.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Matrix Mathematics in MATLAB and Excel
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
1 Chapter 2 Matrices Matrices provide an orderly way of arranging values or functions to enhance the analysis of systems in a systematic manner. Their.
Matlab intro The Environment
MATLAB Fundamentals.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
ENGR-25_Arrays-2.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
Matlab tutorial course Lesson 2: Arrays and data types
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
Matlab Chapter 2: Array and Matrix Operations. What is a vector? In Matlab, it is a single row (horizontal) or column (vertical) of numbers or characters.
1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
Array Addition  Two arrays can be added if and only if both arrays have exactly the same dimensions.  Assuming the dimension requirement is satisfied,
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Chapter 2 Numeric, Cell, and Structure Arrays. Physics Connection - Specification of a position vector using Cartesian coordinates. Figure 2.1–1 2-2 The.
MATLAB An Introduction to MATLAB (Matrix Laboratory) 1.
Matrices Addition & Subtraction Scalar Multiplication & Multiplication Determinants Inverses Solving Systems – 2x2 & 3x3 Cramer’s Rule.
MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
ES 240: Scientific and Engineering Computation. Chapter 2 Chapter 2: MATLAB Fundamentals Uchechukwu Ofoegbu Temple University.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
A string is an array of characters Strings have many uses in MATLAB Display text output Specify formatting for plots Input arguments for some functions.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Prepared by Deluar Jahan Moloy Lecturer Northern University Bangladesh
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Array Creation ENGR 1181 MATLAB 2. Civil engineers store seismic data in arrays to analyze plate tectonics as well as fault patterns. These sets of data.
Array Operations ENGR 1181 MATLAB 4.
Matrices: Simplifying Algebraic Expressions Combining Like Terms & Distributive Property.
Array Creation ENGR 1187 MATLAB 2. Today’s Topics  Arrays: What are they?  Vectors  Matrices  Creating Arrays.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
1 An Introduction to R © 2009 Dan Nettleton. 2 Preliminaries Throughout these slides, red text indicates text that is typed at the R prompt or text that.
Matlab for Engineers Matlab Environment Chapter 2.
CMPS 1371 Introduction to Computing for Engineers VECTORS.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
An Introduction to Programming in Matlab Emily Blumenthal
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
A rectangular array of numeric or algebraic quantities subject to mathematical operations. The regular formation of elements into columns and rows.
ENG College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices.
ECE 1304 Introduction to Electrical and Computer Engineering
Matrix Operations.
Matrix Operations Monday, August 06, 2018.
Matrix Operations.
MATRICES MATRIX OPERATIONS.
2.2 Introduction to Matrices
Array Creation ENGR 1181 MATLAB 02.
Presentation transcript:

Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element Operations 3.Matrix Operations 4.Polynomial Operations Textbook Reading Assignments Practice Problems 1.Chapter 2, Problems:1, 2, 3, 8, 9, 13

Introduction to Matlab Module #2 Page 2 1) Numeric Arrays Arrays - An array is a collection of data that can be described with a single variable - Arrays are entered into Matlab using square brackets (i.e., y = [1, 2, 3]) Row Vectors - A horizontal arrangement of elements: - Entered in Matlab using comma delimiters within the square brackets: >> x = [5, 7 2] - Individual elements are addressed using indexes starting at 1:x(1) → 5 x(2) → 7 x(3) →

Introduction to Matlab Module #2 Page 3 1) Numeric Arrays Column Vectors - A vertical arrangement of elements - Column entries are entered in Matlab using the semicolon within the square brackets >> y = [8; -2; 4] - Individual elements are addressed using indexes:y(1) → 8 y(2) → -2 y(3) →

Introduction to Matlab Module #2 Page 4 1) Numeric Arrays Automatic Creation of Arrays - Equally space elements can be created using: >> t = [0:1:10] start value step size end value - the same thing can be accomplished using the linspace(a,b,n) command, which allows you to enter the start (a), the end (b), and the number of elements in the array (n). Matlab will create the array with regular spacing between elements. >> t = linspace[0,10,11] start value end size # of points in array - logarithmic spacing can be accomplished using the logspace(a,b,n) command

Introduction to Matlab Module #2 Page 5 1) Numeric Arrays 2D Arrays or Matrices - Matrices are 2D arrays that are m x n in size where: m = # of rows n = # of columns - Matrices can be directly entered in Matlab using a combination of commas and semicolons: >> A = [2,5; -3,4; -7,1] - When talking about Matrices, the row always comes first i.e., (row, column) - We say that A is a 3 x 2 matrix (i.e., it has 3 rows and 2 columns) n m

Introduction to Matlab Module #2 Page 6 1) Numeric Arrays Addressing Matrices - Individual entries can be addressed using the (row, column) location:A(1,1) → 2 A(1,2) → 5 A(2,1) → -3 A(2,2) → 4 A(3,1) → -7 A(3,2) → 1 - The colon (:) represents all element addresses i.e.,>> B = A(:,1) would yield: i.e., All rows, 1 st column >> C = A(2,:) would yield i.e., 2 nd row, all columns

Introduction to Matlab Module #2 Page 7 1) Numeric Arrays Addressing Matrices - A range of entries can be addressed using the semicolon: i.e.,>> D = A(1:2,1:2) would yield: i.e., rows 1 & 2, columns 1 &

Introduction to Matlab Module #2 Page 8 1) Numeric Arrays Creating 2D Arrays with the Matrix Editor - A 2D array variable can be created using the empty or null operator [] D = []this creates an empty matrix variable D - This variable can now be double clicked on in the Workspace to launch the Array editor

Introduction to Matlab Module #2 Page 9 2) Array Operations (element-by-element) A variety of built-in functions exist to help analyze a Matrix - length(M)% if a vector, returns the number of elements in the array % if a matrix, returns the largest number of elements in the array (either row or column) >> length(X)→4 >> length(Y)→4 >> length(Z)→3

Introduction to Matlab Module #2 Page 10 2) Array Operations (element-by-element) A variety of built-in functions exist to help analyze a Matrix - size(M)% returns the matrix size in row, column (m x n) format >> size(X)→[1 4] >> size(Y)→[4 1] >> size(Z)→[3 2]

Introduction to Matlab Module #2 Page 11 2) Array Operations (element-by-element) - max(M)% if a vector, returns the algebraically largest value % if a matrix, returns the row vector containing the largest value >> max(X)→9 >> max(Y)→8 >> max(Z)→[2 5] - min(M)% if a vector, returns the algebraically smallest value % if a matrix, returns the row vector containing the smallest value >> min(X)→-5 >> min(Y)→-6 >> min(Z)→[-7 1]

Introduction to Matlab Module #2 Page 12 2) Array Operations (element-by-element) - sort(M) % if a row vector, returns a row vector of the same size elements in ascending order % if a column vector, returns a column vector of the same size elements in ascending order % if a matrix, returns a matrix of the same size with columns sorted in ascending order >> sort(X)→ >> sort(Y)→ >> sort(Z)→

Introduction to Matlab Module #2 Page 13 - sum(M) % if a vector, returns a scalar with the sum of all elements % if a matrix, returns a row vector with the sum of each columns >> sum(X)→6 >> sum(Y)→5 >> sum(Z)→[-8 10] 2) Array Operations (element-by-element)

Introduction to Matlab Module #2 Page 14 2) Array Operations (element-by-element) Matrices can also be “Transposed” to interchange rows and columns The transpose command in Matlab is the tick (‘) >> X’ >> Y’ >> Z’ The transpose command is often used to make the sort/sum commands work more efficiently

Introduction to Matlab Module #2 Page 15 2) Array Operations (element-by-element) Scalar-Array Operations - mathematical operations between a scalar and matrix are performed on each element within the matrix (element-by-element). - addition (+), subtraction (-), and multiplication (*) are performed on each element ex)>> A+1 → >> 1+A→ >> A-1 → >> A*2 → >> 2*A→

Introduction to Matlab Module #2 Page 16 2) Array Operations (element-by-element) Scalar-Array Operations - division requires that the Array be the numerator - both left and right scalar division works ex)>> A/2 → >> 2\A→

Introduction to Matlab Module #2 Page 17 2) Array Operations (element-by-element) Array Addition (or element-by-element addition) - when performing mathematical operations with two matrix inputs, care needs to be taken to follow the rules of matrix algebra. - element-by-element operations are NOT always the same as traditional Matrix operations. - addition of two matrices requires that the two inputs be of the same size. The output is a matrix of the same size where each element is the sum of the two corresponding locations in the inputs. ex)>> C = A + B →

Introduction to Matlab Module #2 Page 18 2) Array Operations (element-by-element) Array Subtraction (or element-by-element subtraction) - subtraction of two matrices requires that the two inputs be of the same size. The output is a matrix of the same size where each element is the difference of the two corresponding locations in the inputs. ex)>> C = A - B →

Introduction to Matlab Module #2 Page 19 2) Array Operations (element-by-element) Array Multiplication (or element-by-element multiplication) - The rules of traditional Matrix multiplication are different than element-by-element multiplication. - If you wish to perform element-by-element multiplication on two matrices of the same size, you use the operator (.*) >> F = D.* E →

Introduction to Matlab Module #2 Page 20 2) Array Operations (element-by-element) Array Division (or element-by-element multiplication) - The rules of traditional Matrix division are different than element-by-element division. - If you wish to perform element-by-element division on two matrices of the same size, you use the operator (./) or (.\) >> F = D./ E → >> F = E.\ D →

Introduction to Matlab Module #2 Page 21 2) Array Operations (element-by-element) Array Exponentiation (or element-by-element exponentiation) - The rules of traditional Matrix exponentiation are different than element-by-element exponentiation. - If you wish to perform element-by-element exponentiation on a matrix you use the operator (.^) >> F = A.^ 3 →

Introduction to Matlab Module #2 Page 22 3) Matrix Operations Matrix Multiplication (formal definition) - The rules of Matrix multiplication is that for C=AB: - that the number of columns in A is equal to the number or rows in B. - the product will be a matrix with the same # of rows in A and same # of columns in B i.e., - If the inputs to matrix-matrix multiplication follow these size rules, the multiplication operator is simply (*) >> A*y →

Introduction to Matlab Module #2 Page 23 3) Matrix Operations Matrix Division - The rules of Matrix division are more complicated and covered in Chapter 6 of the text. - The operators for Matrix division are (/) and (\) - There are a variety of conditions that must be met in order for this division to work properly (more later)

Introduction to Matlab Module #2 Page 24 3) Matrix Operations Matrix Exponentiation - Matrix exponentiation is defined as repeatedly multiplying the matrix by itself. i.e., - This requires that the Matrix be a square (i.e., m = n) >> S^2 → Notice this is NOT simply raising each element to a power of 2

Introduction to Matlab Module #2 Page 25 4) Polynomial Operations Polynomials - Polynomials are entered into Matlab using a Row Vector - Each of the entries in the row vector represents the coefficients of the terms in the polynomial - The coefficients are entered with the highest order on the left and the lowest on the right. Ex) would be entered as:>> [ ] - polynomial terms that don’t exists are entered with a coefficient of 0. Ex) would be entered as:>> [ ]

Introduction to Matlab Module #2 Page 26 4) Polynomial Operations Polynomial Operations - Matlab has built in operations to evaluate polynomials: roots(a)% returns the roots of a polynomial poly(x)% computes the coefficients of a polynomial give the roots polyval(a,x)% evaluates the polynomial (a) at specified values of the % independent variable (x)

Introduction to Matlab Module #2 Page 27 Lab 2 Exercise Problems -For each of these exercises, you will create a script file. Your script file will perform the calculations and then display the answers to the workspace. -Create a directory on your Z drive called “Z:\Matlab_Course\Lab02” -Change your pwd to “Z:\Matlab_Course\Lab02” (>> cd Z:\Matlab_Course\Lab02) -Perform the following exercises: 2.1- Create a script file called Lab02_2d1a.m - Print a comment to the screen for each solution using the command disp Create a script file called Lab02_2d2.m Create a script file called Lab02_2d3.m 2.8- Create a script file called Lab02_2d8.m 2.9- Create a script file called Lab02_2d9.m Create a script file called Lab02_2d13.m