September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

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.
Matrices. Special Matrices Matrix Addition and Subtraction Example.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
“No one can be told what the matrix is…
EGR 106 – Week 3 – More on Arrays Brief review of last week Additional ideas: – Special arrays – Changing an array – Some array operators – Character arrays.
Part 3 Chapter 8 Linear Algebraic Equations and Matrices PowerPoints organized by Dr. Michael R. Gustafson II, Duke University All images copyright © The.
Matrices The Basics Vocabulary and basic concepts.
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
4.2 Operations with Matrices Scalar multiplication.
259 Lecture 14 Elementary Matrix Theory. 2 Matrix Definition  A matrix is a rectangular array of elements (usually numbers) written in rows and columns.
Jeopardy Algebraic Expressions Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Real Numbers Matrix Usage Math Properties Right Triangles.
ECON 1150 Matrix Operations Special Matrices
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Matrix Algebra. Quick Review Quick Review Solutions.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
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,
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
Copyright © 2011 Pearson, Inc. 7.2 Matrix Algebra.
Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Unit 3: Matrices.
Algebra 3: Section 5.5 Objectives of this Section Find the Sum and Difference of Two Matrices Find Scalar Multiples of a Matrix Find the Product of Two.
Matrix Arithmetic. A matrix M is an array of cell entries (m row,column ) and it must have rectangular dimensions (Rows x Columns). Example: 3x x.
Operations with Matrices
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
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.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
MATLAB Lecture Two Tuesday 5 July Chapter 3.
Copyright © Cengage Learning. All rights reserved. 7 Linear Systems and Matrices.
ES 240: Scientific and Engineering Computation. Chapter 8 Chapter 8: Linear Algebraic Equations and Matrices Uchechukwu Ofoegbu Temple University.
Meeting 18 Matrix Operations. Matrix If A is an m x n matrix - that is, a matrix with m rows and n columns – then the scalar entry in the i th row and.
Slide Copyright © 2009 Pearson Education, Inc. 7.3 Matrices.
Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
MATRICES MATRIX OPERATIONS. About Matrices  A matrix is a rectangular arrangement of numbers in rows and columns. Rows run horizontally and columns run.
Sec 4.1 Matrices.
Copyright © Cengage Learning. All rights reserved. 2 SYSTEMS OF LINEAR EQUATIONS AND MATRICES.
Algebra Matrix Operations. Definition Matrix-A rectangular arrangement of numbers in rows and columns Dimensions- number of rows then columns Entries-
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Digital Image Processing. Converting between Data Classes The general syntax is B = data_class_name (A) Where data_class_name is one of the names defined.
Linear System of Simultaneous Equations Warm UP First precinct: 6 arrests last week equally divided between felonies and misdemeanors. Second precinct:
Unit 3: Matrices. Matrix: A rectangular arrangement of data into rows and columns, identified by capital letters. Matrix Dimensions: Number of rows, m,
Precalculus Section 14.1 Add and subtract matrices Often a set of data is arranged in a table form A matrix is a rectangular.
Array and Matrix Operations EE 201 Fall  Achieve Comprehension LOL of Array and Matrix Operations. Class Learning Objectives 2.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
A rectangular array of numeric or algebraic quantities subject to mathematical operations. The regular formation of elements into columns and rows.
Ch. 12 Vocabulary 1.) matrix 2.) element 3.) scalar 4.) scalar multiplication.
Vectors, Matrices and their Products Hung-yi Lee.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
MTH108 Business Math I Lecture 20.
Linear Algebraic Equations and Matrices
12-1 Organizing Data Using Matrices
응용 전산 및 실습 MATLAB – Chapter 3 행렬연산
Matrix Operations SpringSemester 2017.
7.3 Matrices.
Lecture 11 Matrices and Linear Algebra with MATLAB
Introduction to MATLAB [Vectors and Matrices] Lab 2
Unit 3: Matrices
3.5 Perform Basic Matrix Operations
Chapter 4 Matrices & Determinants
1.8 Matrices.
Matrix Operations SpringSemester 2017.
1.8 Matrices.
3.5 Perform Basic Matrix Operations Algebra II.
Presentation transcript:

September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005 Lecture 5 - By Paul Lin 2 Lecture 5: More On MATLAB Arrays 5.1 More MATLAB Functions 5.2 Array Operators 5.3 Array Arithmetic Operations 5.4 Sub-arrays

September 15, 2005 Lecture 5 - By Paul Lin More MATLAB Functions size(this_array) returns the two values specifying the number of rows and columns in this_arrayreturns the two values specifying the number of rows and columns in this_arraylength(this_array) returns the length of a vector, or the longest dimension of a 2-D arrayreturns the length of a vector, or the longest dimension of a 2-D arrayzeros(n) built-in MATLAB function for creating an n- by-n array with all elements are initialized to zerobuilt-in MATLAB function for creating an n- by-n array with all elements are initialized to zero zeros(n, m) for creating an n-by-m all-zero arrayfor creating an n-by-m all-zero array

September 15, 2005 Lecture 5 - By Paul Lin More MATLAB Functions (continue) ones(n) built-in MATLAB function for creating an n- by-n array with all elements are initialized to onebuilt-in MATLAB function for creating an n- by-n array with all elements are initialized to one ones(n, m) for creating an n-by-m all-one arrayfor creating an n-by-m all-one arrayeye(n) for creating an n-by-n identity matrix in which all elements in the main diagonal are onesfor creating an n-by-n identity matrix in which all elements in the main diagonal are ones eye(n, m) for creating an n-by-m identity matrixfor creating an n-by-m identity matrix

September 15, 2005 Lecture 5 - By Paul Lin More MATLAB Functions (continue) >> help rand RAND Uniformly distributed random numbers. RAND(N) is an N-by-N matrix with random entries, chosen from a uniform distribution on the interval (0.0,1.0). RAND(N) is an N-by-N matrix with random entries, chosen from a uniform distribution on the interval (0.0,1.0). RAND(M,N) and RAND([M,N]) are M-by-N matrices with random entries. RAND(M,N,P,...) or RAND([M,N,P,...]) generate random arrays. RAND with no arguments is a scalar whose value changes each time it is referenced. RAND(SIZE(A)) is the same size as A.

September 15, 2005 Lecture 5 - By Paul Lin More MATLAB Functions (continue) >> help fix FIX Round towards zero. FIX Round towards zero. FIX(X) rounds the elements of X to the nearest integers towards zero. See also FLOOR, ROUND, CEIL. See also FLOOR, ROUND, CEIL.

September 15, 2005 Lecture 5 - By Paul Lin More MATLAB Functions (continue) Example 5.1: Creating arrays using array functions n = 3; m = 2; % an 3 x 3 all-zero array A0 = zeros(3) % an 3 x 2 all zero array B0 = zeros(3, 2) % an 3 x 3 all-one array A1 = ones(3) % an 3 x 2 all-zero array B1 = ones(3,2) %An identify array A_eye = eye(n) A1 = B1 = 1 1 A_eye = A0 = B0 = 0 0

September 15, 2005 Lecture 5 - By Paul Lin Array Operators Transpose operator ( ' ) swap the rows and columns of an array swap the rows and columns of an array Colon operator (: ) one of the most useful operator in MATLAB. We can use it to create regularly spaced vectors, subscript matrices, and specify for iterationsone of the most useful operator in MATLAB. We can use it to create regularly spaced vectors, subscript matrices, and specify for iterations

September 15, 2005 Lecture 5 - By Paul Lin Array Operators Example 5.2: Vector Transpose Example rand() – random number generation function rand() – random number generation function >> % Generate an array using rand() function array_r1 = rand(1, 4) array_r2 = rand(1, 4) % Convert row vector to column vector array_c1 = array_r1' array_c2 = array_r2' array_r1 = array_r2 = array_c1 = array_c2 =

September 15, 2005 Lecture 5 - By Paul Lin Array Operators (continue) Example 5.3: Using rand(), fix() functions and array transpose operator >> A = rand(1,4) *10 A = >> A = fix(A) A = >> B = fix(rand(1,4)*10) B = >> C = [A' B'] C =

September 15, 2005 Lecture 5 - By Paul Lin Array Operators (continue) Example 5.4: Using colon operator to pick-up selected rows or columns Example 5.4: Using colon operator to pick-up selected rows or columns A(:, j) – extracts j-th column of A A(:, j) – extracts j-th column of A A(i, :) – extracts the i-th row of A A(i, :) – extracts the i-th row of A C = C_1 = C(:,1) C_2 = C(:, 2) R_1 = C(1,:) R_2 = C(2, :) R_3 = C(3, :) R_4 = C(4, :) R_1 = R_2 = R_3 = R_4 = C_1 = C_2 =

September 15, 2005 Lecture 5 - By Paul Lin Array Operators (continue) Example 5.5: This problem solving example uses the colon operator with integers to generate a regularly spaced temperature vector from 0 to 100 in degree C. We will also print all data to show that C to F Temperature Conversion holding a linear relationship. Example 5.5: This problem solving example uses the colon operator with integers to generate a regularly spaced temperature vector from 0 to 100 in degree C. We will also print all data to show that C to F Temperature Conversion holding a linear relationship. Analysis (identified equations and MATLAB equations, and using plot function to show the linear relationship) Analysis (identified equations and MATLAB equations, and using plot function to show the linear relationship) F = 9/5 C + 32 C = 0:10:100; F = 9/5 * C + 32; plot(C, F)

September 15, 2005 Lecture 5 - By Paul Lin Array Operators (continue) Example 5.5: Solution Example 5.5: Solution % CtoF_plot.m % Author: M. Lin % Date: 9/6/04 % Description: C = 0:10:100; F = (9/5)* C + 32; plot(C, F), grid on xlabel('Degree C'), ylabel('Degree F') title(' C vs F')

September 15, 2005 Lecture 5 - By Paul Lin Array Operators (continue) Example 5.6: Reconstruct the sine 60Hz signal and its time vector as a 2-D array and save it as sine60hz.mat file. Verify result by reloading the program and plot the sine wave using colon operator. Example 5.6: Reconstruct the sine 60Hz signal and its time vector as a 2-D array and save it as sine60hz.mat file. Verify result by reloading the program and plot the sine wave using colon operator. Solution: Solution: f = 60 Hz, T = 1/f f = 60 Hz, T = 1/f Vm = 10 volts, Vm = 10 volts, dt = 0.001*T, dt = 0.001*T, t = 0:dt: 5*T t = 0:dt: 5*T e = Vm*sin(2*pi*f*t) e = Vm*sin(2*pi*f*t) % sine60hz_2d.m % Author: M. Lin % Date: 9/6/04 % Description: f = 60; T = 1/f; Vm = 10; dt = 0.001*T; t = 0:dt: 5*T; e = Vm*sin(2*pi*f*t); sine60 = [t' e']; save sine60.mat sine60 clear load sine60.mat plot(sine60(:,1), sine60(:,2))

September 15, 2005 Lecture 5 - By Paul Lin Array Operators (continue) Example 5.6: MATLAB Solution Example 5.6: MATLAB Solution

September 15, 2005 Lecture 5 - By Paul Lin Array Arithmetic Operations + Addition A + B adds A and B arrays of the same dimension, unless one is a scalar. A scalar can be added to a matrix of any dimension.A + B adds A and B arrays of the same dimension, unless one is a scalar. A scalar can be added to a matrix of any dimension. An Example: both A and B are 2-by-2 array, A + B isAn Example: both A and B are 2-by-2 array, A + B is | (a11 + b11) (a12 + b12) | | (a21 + b21) (a22 + b22) |

September 15, 2005 Lecture 5 - By Paul Lin Array Arithmetic Operations (continue) - Subtraction. - Subtraction. A - B subtracts B from A. A and B arrays must have the same dimension, unless one is a scalar. A scalar can be subtracted from a matrix of any dimension.A - B subtracts B from A. A and B arrays must have the same dimension, unless one is a scalar. A scalar can be subtracted from a matrix of any dimension. An Example: both A and B are 2-by-2 array, A - B isAn Example: both A and B are 2-by-2 array, A - B is | (a11 - b11) (a12 - b12) | | (a21 - b21) (a22 - b22) |

September 15, 2005 Lecture 5 - By Paul Lin Array Arithmetic Operations (continue).* Array Multiplication A.* B is the element-by-element product of the arrays A and B. A and B must have the same dimension, unless one is a scalar. A scalar can be multiplied to a matrix of any dimension.A.* B is the element-by-element product of the arrays A and B. A and B must have the same dimension, unless one is a scalar. A scalar can be multiplied to a matrix of any dimension. An Example: both A and B are 2-by-2 array, A.* B isAn Example: both A and B are 2-by-2 array, A.* B is | (a11 * b11) (a12 * b12) | | (a21 * b21) (a22 * b22) |

September 15, 2005 Lecture 5 - By Paul Lin Array Arithmetic Operations (continue)./ Array Right Division A./B is the element-by-element division of the arrays A and B. A and B must have the same dimension, unless one is a scalar. A scalar can be divided by a matrix of any dimension.A./B is the element-by-element division of the arrays A and B. A and B must have the same dimension, unless one is a scalar. A scalar can be divided by a matrix of any dimension. An Example: both A and B are 2-by-2 array, A./ B isAn Example: both A and B are 2-by-2 array, A./ B is | (a11 / b11) (a12 / b12) | | (a21 / b21) (a22 / b22) |

September 15, 2005 Lecture 5 - By Paul Lin Array Arithmetic Operations (continue) Example 5.7: Element-by- element array arithmetic operations A = fix(rand(2)*10) B = fix(rand(2)*10) W = A - B X = A + B Y = A.* B Z = A./ B >> A = fix(rand(2)*10) B = fix(rand(2)*10) W = A - B X = A + B Y = A.* B Z = A./ B A = B =

September 15, 2005 Lecture 5 - By Paul Lin Array Arithmetic Operations (continue) Example 5.7: continue W = A - B X = A + B Y = A.* B Z = A./ B A = B = >> A - B W = >> A + B X = >> A.* B Y = >> A./B Warning: Divide by zero. (Type "warning off MATLAB: divideByZero" to suppress this warning.) Z = Inf Inf

September 15, 2005 Lecture 5 - By Paul Lin SubArrays Subarrays can be formed by using colon (: ) operator to select a portion of an array Example 5.9: Subarray Examples array4 = [ ; ; ] array5 = array4(1, : ) % [ ] array6 = array4(:,1: 2: 3) % [first column third column] array6 = array1 = [ ]; array7 = array1(3: end) array 7 = array1(end) ans = 5

September 15, 2005 Lecture 5 - By Paul Lin 23 Summary 5.1 More MATLAB Functions 5.2 Array Operators 5.3 Array Arithmetic Operations 5.4 Subarrays