Chapter 2 Numeric, Cell, and Structure Arrays. Physics Connection - Specification of a position vector using Cartesian coordinates. Figure 2.1–1 2-2 The.

Slides:



Advertisements
Similar presentations
Lecture 4.
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.
CMPS 1371 Introduction to Computing for Engineers STRUCTURE ARRAYS.
Chapter 4.1 Mathematical Concepts
Chapter 4.1 Mathematical Concepts. 2 Applied Trigonometry Trigonometric functions Defined using right triangle  x y h.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
CSCE 590E Spring 2007 Basic Math By Jijun Tang. Applied Trigonometry Trigonometric functions  Defined using right triangle  x y h.
Review of Matrix Algebra
Ch 7.2: Review of Matrices For theoretical and computation reasons, we review results of matrix theory in this section and the next. A matrix A is an m.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
MOHAMMAD IMRAN DEPARTMENT OF APPLIED SCIENCES JAHANGIRABAD EDUCATIONAL GROUP OF INSTITUTES.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
EGR 106 – Week 3 – More on Arrays Brief review of last week Additional ideas: – Special arrays – Changing an array – Some array operators – Character arrays.
Chapter 1: Matrices Definition 1: A matrix is a rectangular array of numbers arranged in horizontal rows and vertical columns. EXAMPLE:
Matlab intro The Environment
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
Chapter 10 Review: Matrix Algebra
Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4.
Compiled By Raj G. Tiwari
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
ENGR-25_Arrays-2.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
1 February 24 Matrices 3.2 Matrices; Row reduction Standard form of a set of linear equations: Chapter 3 Linear Algebra Matrix of coefficients: Augmented.
MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson.
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 Review Exam 2 – Oct 28, Exam Reminders Open book. Only Matlab book, no notes or loose papers in book. Open book. Only Matlab book, no notes.
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
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.
Unit 2: Expressions Section 1: Algebraic Expressions Numerical expressions are those which contain only numbers and operation symbols Algebraic expressions.
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,
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.
Statistics and Linear Algebra (the real thing). Vector A vector is a rectangular arrangement of number in several rows and one column. A vector is denoted.
Chapter 2 Numeric, Cell, and Structure Arrays. Physics Connection - Specification of a position vector using Cartesian coordinates. Figure 2.1–1 2-2 The.
ENGR-25_Arrays-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
CMPS 1371 Introduction to Computing for Engineers MATRICES.
Matrices. Definitions  A matrix is an m x n array of scalars, arranged conceptually as m rows and n columns.  m is referred to as the row dimension.
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.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
Linear algebra: matrix Eigen-value Problems Eng. Hassan S. Migdadi Part 1.
Working with Arrays in MATLAB
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.
>> x = [ ]; y = 2*x y = Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions.
Eigenvalues The eigenvalue problem is to determine the nontrivial solutions of the equation Ax= x where A is an n-by-n matrix, x is a length n column.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
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 MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
ENG College of Engineering Engineering Education Innovation Center 1 Array Operations in MATLAB 1.Types of Matrix arithmetic 2.Dot operators Mathematical.
Computer Graphics Mathematical Fundamentals Lecture 10 Taqdees A. Siddiqi
Array and Matrix Operations EE 201 Fall  Achieve Comprehension LOL of Array and Matrix Operations. Class Learning Objectives 2.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
Introduction to MATLAB for Engineers, Third Edition
Introduction to MATLAB 7
Chapter 3 Arrays and Vectors
Dr Huw Owens Room B44 Sackville Street Building Telephone Number 65891
Array and Matrix Functions
Arrays and Matrices in MATLAB
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Introduction to MATLAB 7
Math review - scalars, vectors, and matrices
Presentation transcript:

Chapter 2 Numeric, Cell, and Structure Arrays

Physics Connection - Specification of a position vector using Cartesian coordinates. Figure 2.1–1 2-2 The vector p can be specified by three components: p =x i + y j + z k = [x, y, z]. Generally, MATLAB vector is any list of ordered scalars T = [ ….. 69] - 24 temperatures throughout the day, measured every hour

To create a row vector, separate the elements by commas. For example, >>p = [3,7,9] % or just [ ] p = You can create a column vector by using the transpose notation ('). >>p = [3,7,9]' p =

You can also create a column vector by separating the elements by semicolons. For example, >>g = [3;7;9] g = Another way is to just type in rows: g = [ ] 2-4

2-5 You can create vectors by ''appending'' one vector to another. For example, to create the row vector u whose first three columns contain the values of r = [2,4,20] and whose fourth, fifth, and sixth columns contain the values of w = [9,-6,3], you type u = [r, w] The result is the vector u = [2,4,20,9,-6,3]. NO need for special merge operations etc…

The colon operator (:) easily generates a large vector of regularly spaced elements. Typing >>x = [m:q:n] creates a vector x of values with a spacing q. The first value is m. The last value is n if m - n is an integer multiple of q. If not, the last value is < n. 2-6

For example, typing x = [0:2:8] creates the vector: x = [0,2,4,6,8], whereas typing x = [0:2:7] creates the vector x = [0,2,4,6]. Can also try x = 10: -3: 1 produces x = [ ] To create row vector z consisting of the values from 5 to 8 in steps of 0.1, type z = 5:0.1:8 If the increment q is omitted, it is presumed to be 1. Thus typing y = [-3:2] produces the vector y = [-3,-2,-1,0,1,2]. In class: Generate a vector from -1.5 to 3.7 with 0.2 spacing 2-7

The linspace command also creates a linearly spaced row vector, but instead you specify the number of values rather than the increment. The syntax is linspace(x1,x2,n), where x1 and x2 are the lower and upper limits and n is the number of points. For example, linspace(5,8,31) % note not 30, but 31 is equivalent to [5:0.1:8]. If n is omitted, the spacing is 1. In class: generate vector from -10 to 100 with 14 elements 2-8

The logspace command creates an array of logarithmically spaced elements. Its syntax is logspace(a,b,n), where n is the number of points between 10 a and 10 b. For example, x = logspace(-1,1,4) produces the vector x = [0.1000, , , ]. If n is omitted, the number of points defaults to More? See pages

Magnitude, Length, and Absolute Value of a Vector The length command gives the number of elements in the vector. The magnitude of a vector x having elements x 1, x 2, …, x n is a scalar, given by  x x … + x n 2 ), and is the same as the vector's geometric length. For 2 variables x and y in plane this is just Pythagorean Theorem The absolute value of a vector x is a vector whose elements are the absolute values of the elements of x. [ |x1|, |x2|, |x3|, ….. |xn| ] 2-10

For example, if x = [2,-4,5],  its length is 3; (computed from length(x) )  its magnitude is  [2 2 + (–4) ] = ; (computed from sqrt(x’*x) or norm(x) )  its absolute value is [2,4,5] (computed from abs(x) ) - absolute values of elements In class: y = [ ], find length, magnitude and absolute value 2-11 More? See page 79.

Matrices A matrix has multiple rows and columns. For example, the matrix has four rows and three columns. Elements are accessed by M(row, col) Vectors are special cases of matrices having one row or one column. M = More? See page 73.

Creating Matrices If the matrix is small you can type it row by row, separating the elements in a given row with spaces or commas and separating the rows with semicolons. For example, typing >>A = [2,4,10;16,3,7]; creates the following matrix: Remember: spaces or commas separate elements in different columns semicolons separate elements in different rows. A = 2-13

Creating Matrices from Vectors Suppose a = [1,3,5] and b = [7,9,11] (row vectors). Note the difference between the results given by [a b] and [a;b] in the following session: >>c = [a b]; c = % single row >>D = [a;b] D = % forms a matrix

Adjoining a column to a matrix: A = [ ] newcol = [ ] Anewcol = [A newcol]

Matrix Transpose A = [ ] Atransposed = A' Atransposed = If A' = A – symmetric matrix (only square matrix may be symmetric For complex matrices, it produces complex conjugate transpose.

Array Addressing Array indices are the row and column numbers in that order. v(3) – 3 rd element of array v. A(3,4) 3 rd row 4 th column element in matrix A The colon operator selects individual elements, rows, columns, or ''sub- arrays'' of arrays. Here are some examples: v(:) represents all the row or column elements of the vector v. v(2:5) represents the second through fifth elements; that is v(2), v(3), v(4), v(5). v(2:end) -- 2 nd to the end A(:,3) denotes all the elements in the third column of matrix A. A(:,2:5) denotes all the elements in the second through fifth columns of A. A(2:3,1:3) denotes all the elements in the second and third rows that are also in the first through third columns.

You can use array indices to extract a smaller array from another array. For example, if you first create the array B B = 2-17 C =C = then type C = B(2:3,1:3), you can produce the following array: More? See pages B(2, :)=[ ] B(:, 3) = [ ]

M = [ ] creates empty (null) matrix Rows and columns can be deleted by assigning [ ] For example: A(:,4) = [ ] – delete entire 4 th column A(3:5,:) = [ ] – delete 3 rd, 4 th, and 5 th rows of A A([3 5],:) = [ ] – delete 3 rd and 5 th rows of A

>> A = [ ] 3 4 ] A = >> A(1,4) = 5 % expands matrix automatically A = Matrix does not accept negative or zero indices, but can use negative increments in colon operator. A(:, 4:-1:1) – reverse the columns Always use clear all in the beginning of a script to avoid reusing array with wrong dimention or some other previous information

Additional Array Functions (Table 2.1–1) [u,v,w] = find(A) length(A) Computes the arrays u and v, containing the row and column indices of the nonzero elements of the matrix A, and the array w, containing the values of the nonzero elements. The array w may be omitted. -- useful for sparse matrices Computes either the number of elements of A if A is a vector or the largest value of m or n if A is an m × n matrix A = [ ] u = % nonzero rows v = % nonzero cols w = % nonzero elements A = [ ] u = % nonzero rows v = % nonzero cols w = % nonzero elements

Additional Array Functions (Table 2.1–1) max(A) Returns the algebraically largest element in A if A is a vector. Returns a row vector containing the largest elements in each column if A is a matrix. If any of the elements are complex, max(A) returns the elements that have the largest magnitudes If A=[ ] - vector max(A) = 7 If A = [ ] – matrix max(A) = [ ]

Additional Array Functions (Table 2.1–1) [x,k] = max(A) min(A) and [x,k] = min(A) Similar to max(A) but stores the maximum values in the row vector x and their indices in the row vector k. Like max but returns minimum values. 2-20

The function size(A) returns a row vector [m n] containing the sizes of the m × n array A. The length(A) function computes either the number of elements of A if A is a vector or the largest value of m or n if A is an m × n matrix. For example, if then size(A) returns [3, 2]; length(A) returns 3, max(A) returns the vector [6,2] – largest elements in each column; min(A) returns the vector [-10, -5] - smallest elements in each column In class: Ch2 #5, also find A(1:3,2:4), A(:,3:4) A = 6 2 –10 – More? See pages

sum – sums elements in an array and column by column in a matrix. sort – sorts elements in ascending order and column by column in a matrix A = sumA = sortA =

The Workspace Browser. Figure 2.1– More? See pages

The Array Editor. Figure 2.1–3 double-click on the array variable to get to Array Editor 2-24 More? See pages

Multidimensional Arrays - optional Consist of two-dimensional matrices “layered” to produce a third dimension. Each “layer” is called a page. Example of use – say temperature in a room with indices giving x, y, and z coordinates. cat(n,A,B,C,...) - creates a new array by concatenating the arrays A, B, C, and so on along the dimension n More? See pages >> A = [ ] A = >> A(:,:,2) = [ ] A(:,:,1) = A(:,:,2) = >> A = [ ] A = >> B = [ ] B = >> cat(3,A,B) ans(:,:,1) = ans(:,:,2) = >> cat(1,A,B) ans = >> cat(2,A,B) ans =

2-26 Vector addition by geometry. (a)The parallelogram law. (b)Addition of vectors in three dimensions.

Array Addition and Subtraction 6 – –12 14 = 15 6 –2 17 Array subtraction is performed in a similar way. The addition shown in equation 2.3–1 is performed in MATLAB as follows: >>A = [6,-2;10,3]; >>B = [9,8;-12,14] >>A+B ans = Arrays are added and subtracted element by element, for example: 2-27 (2.3-1) More? See page 85.

2-28 Geometric interpretation of scalar multiplication of a vector. Figure 2.3–1 If r = [x, y, z], then v = 2r = 2[x, y, z] = [2x, 2y, 2z]. element by element scalar multiplication

Multiplying a matrix A by a scalar w produces a matrix whose elements are the elements of A multiplied by w – again element by element. For example: –7 = –21 This multiplication is performed in MATLAB as follows: >>A = [2, 9; 5,-7]; >>3*A ans =

Multiplication of an array by a scalar is easily defined and easily carried out. However, multiplication of two arrays is not so straightforward. MATLAB uses two very different definitions of multiplication: (1)array multiplication (element-by-element multiplication) (2)matrix multiplication. 2-30

Division and exponentiation must also be carefully defined when you are dealing with operations between two arrays. MATLAB has two forms of arithmetic operations on arrays. Next we introduce one form, called array operations, which are also called element-by-element operations. Then we will introduce matrix operations. Each form has its own applications. 2-31

Element-by-element operations: Table 2.3–1 Symbol *./.\.^ Examples [6,3]+2=[8,5] [8,3]-5=[3,-2] [6,5]+[4,8]=[10,13] [6,5]-[4,8]=[2,-3] [3,5].*[4,8]=[12,40] [2,5]./[4,8]=[2/4,5/8] [2,5].\[4,8]=[2\4,5\8] [3,5].^2=[3^2,5^2] 2.^[3,5]=[2^3,2^5] [3,5].^[2,4]=[3^2,5^4] Operation Scalar-array addition Scalar-array subtraction Array addition Array subtraction Array multiplication Array right division Array left division Array exponentiation Form A + b A – b A + B A – B A.*B A./B A.\B A.^B 2-32

Array or Element-by-element multiplication is defined only for arrays having the same size. The definition of the product x.*y, where x and y each have n elements, is x.*y = [x(1)y(1), x(2)y(2),..., x(n)y(n)] if x and y are row vectors. For example, if x = [2, 4, – 5], y = [– 7, 3, – 8] (2.3–4) then z = x.*y gives z = [2(– 7), 4 (3), –5(–8)] = [–14, 12, 40] 2-33

If x and y are column vectors, the result of x.*y is a column vector. For example z = (x’).*(y’) gives Note that x’ is a column vector with size 3 × 1 and thus does not have the same size as y, whose size is 1 × 3. Thus for the vectors x and y the operations x’.*y and y.*x’ are not defined in MATLAB and will generate an error message. 2(–7) 4(3) –5(–8) – = z = 2-34

The array operations are performed between the elements in corresponding locations in the arrays. For example, the array multiplication operation A.*B results in a matrix C that has the same size as A and B and has the elements c i j = a i j b i j. For example, if then C = A.*B gives this result: A = 11 5 –9 4 B = – C = 11(–7) 5(8) –9(6) 4(2) = –77 40 – More? See pages NOT Matrix Multiplication

The built-in MATLAB functions such as sqrt(x) and exp(x) automatically operate on array arguments to produce an array result the same size as the array argument x - again element by element. Thus these functions are said to be vectorized functions. For example, in the following session the result y has the same size as the argument x. >>x = [4, 16, 25]; >>y = sqrt(x) % element by element y =

However, when multiplying or dividing these functions, or when raising them to a power, you must use element-by- element operations if the arguments are arrays. For example, to compute z = (e y sin x) cos 2 x, you must type z = exp(y).*sin(x).*(cos(x)).^2. You will get an error message if the size of x is not the same as the size of y. The result z will have the same size as x and y More? See pages

Array Division The definition of array division is similar to the definition of array multiplication except that the elements of one array are divided by the elements of the other array (again element by element). Both arrays must have the same size. The symbol for array right division is./ For example, if x = [8, 12, 15] y = [–2, 6, 5] then z = x./y gives z = [8/(–2), 12/6, 15/5] = [–4, 2, 3] 2-38

A = 2420 – 9 4 B = – Also, if then C = A./B gives C = 24/(–4) 20/5 –9/3 4/2 = –6 4 – More? See pages

Array Exponentiation MATLAB enables us not only to raise arrays to powers but also to raise scalars and arrays to array powers. To perform exponentiation on an element-by-element basis, we must use the.^ symbol. For example, if x = [3, 5, 8], then typing x.^3 produces the array [3 3, 5 3, 8 3 ] = [27, 125, 512]. 2-40

We can raise a scalar to an array power. For example, if p = [2, 4, 5], then typing 3.^p produces the array [3 2, 3 4, 3 5 ] = [9, 81, 243]. Remember that.^ is a single symbol. The dot in 3.^p is not a decimal point associated with the number 3. The following operations, with the value of p given here, are equivalent and give the correct answer: 3.^p 3.0.^p 3..^p % correct, but confusing => avoid it (3).^p 3.^[2,4,5] In class: Ch2 T2.3-1 p More? See pages

Matrix Operations - see notes on dot product and matrix vector multiplication - see notes on dot product and matrix vector multiplication

Matrix-Matrix Multiplication In the product of two matrices A*B, the number of columns in A must equal the number of rows in B. [C - m by p matrix] = [A - m by n matrix] times [B -n by p matrix] The row-column multiplications form column vectors, and these column vectors form the matrix result. The product AB has the same number of rows as A and the same number of columns as B. For ex: 6 – –5 12 = (6)(9) + (– 2)(– 5) (6)(8) + (– 2)(12) (10)(9) + (3)(– 5) (10)(8) + (3)(12) (4)(9) + (7)(– 5) (4)(8) + (7)(12) = (2.4–4) 2-42 Product of 3 by 2 matrix and 2 by 2 matrix => get 3 by 2 matrix

Use the operator * to perform matrix multiplication in MATLAB. NOT.* The following MATLAB session shows how to perform the matrix multiplication shown in (2.4–4). >>A = [6,-2;10,3;4,7]; >>B = [9,8;-5,12]; >>A*B ans =

Matrix multiplication does not have the commutative property; that is, in general, AB  BA. A simple example will demonstrate this fact: AB = 6 – –12 14 = (2.4–6) BA = 9 8 – – = (2.4–7) whereas 2-44 More? See pages

More multipications >> x = [ 1 2 3] % column vector 3] % column vector x = >> y = [ 4 5 6] % row vector y = >> x*y % 3 by 1 times 1 by 3 get 3 by 3 ans = % outer product % outer product >> y*x % 1 by 3 times 3 by 1 get 1 by 1 ans = 32 % inner product 32 % inner product See Manufacturing Cost Example in the notes

Special Matrices ( type help specmat ) Two exceptions to the non-commutative property are the null or zero matrix, denoted by 0 and the identity, or unity, matrix, denoted by I. The null matrix contains all zeros and is not the same as the empty matrix [ ], which has no elements. These matrices have the following properties: 0A = A0 = 0 – just get all zeros IA = AI = A 2-45

The identity matrix is a square matrix whose diagonal elements are all equal to one, with the remaining elements equal to zero. For example, the 2 × 2 identity matrix is I = The functions eye(n) and eye(size(A)) create an n × n identity matrix and an identity matrix the same size as the matrix A More? See page 105.

Sometimes we want to initialize a matrix to have all zero elements. The zeros command creates a matrix of all zeros. Typing zeros(n) creates an n × n matrix of zeros, whereas typing zeros(m,n) creates an m × n matrix of zeros. Typing zeros(size(A)) creates a matrix of all zeros having the same dimension as the matrix A. This type of matrix can be useful for applications in which we do not know the required dimension ahead of time, but need to initialize the matrix to all zeros. The syntax of the ones command is the same, except that it creates arrays filled with ones. In class: T2.4-1&2, p More? See pages

Matrix Exponentiation A^n = A*A*A … *A - n times (not.^) Matrix A has to be square. >> A = [ ] 3 4 ] A = >> A^7 ans =

Dot and Cross Products >> x = [ 1 2 3] x = >> y = [ ] y = >> cross(x,y) % cross product -- vector orthogonal to x and y ans = >> dot(x,y) % dot product -- the answer is a scalar ans = 16 16>>

Polynomial Multiplication and Division - optional The polynomial above is entered into Matlab as: >a = [ a1 a2 a(n+1)], if any power is missing have to put in 0 To add or subtract two polynomials, just add or subtract their arrays, but have to pad it with 0’s: 2-48

Multiplying and Dividing Polynomials Multiplying and Dividing Polynomials - optional The function conv(a,b) computes the product of the two polynomials described by the coefficient arrays a and b. The two polynomials need not be the same degree. The result is the coefficient array of the product polynomial. The function [q,r] = deconv(num,den) computes the result of dividing a numerator polynomial, whose coefficient array is num, by a denominator polynomial represented by the coefficient array den. The quotient polynomial is given by the coefficient array q, and the remainder polynomial is given by the coefficient array r. Note that we do NOT need adding extra zeros like in summation

Polynomial Multiplication and Division: Examples - optional >>a = [9,-5,3,7]; % corresponds to 9x^3 - 5x^2 + 3x + 7 >>b = [6,-1,2]; % corresponds to 6x^2 – x + 2 >>product = conv(a,b) product = % 54x^5 – 39x^4 + 41x^3 + 29x^2 – x + 14 >>[quotient, remainder] = deconv(a,b) quotient = % 1.5x remainder = % x More? See pages

Polynomial Roots The function The function roots(a) computes the roots of a polynomial specified by the coefficient array a. The result is a column vector that contains the polynomial’s roots. For example, >>r = roots([2, 14, 20]) % 2x^2+14x+20=0 r = More? See page 107.

Polynomial Coefficients The function The function poly(r) computes the coefficients of the polynomial whose roots are specified by the vector r. The result is a row vector that contains the polynomial’s coefficients arranged in descending order of power. For example, >>c = poly([-2, -5]) c = % corresponds to x^2 + 7x More? See page 107.

Plotting Polynomials The function polyval(a,x) evaluates a polynomial at specified values of its independent variable x, which can be a matrix or a vector. The polynomial’s coefficients of descending powers are stored in the array a. The result is the same size as x. 2-52

Example of Plotting a Polynomial To plot the polynomial f (x) = 9x 3 – 5x 2 + 3x + 7 for – 2 ≤ x ≤ 5, you type >>a = [9,-5,3,7]; % - polynomial coefficients >>x = [-2:0.01:5]; % - densely spaced x values >>f = polyval(a,x); >>plot(x,f),xlabel(’x’),ylabel(’f(x)’) In class: T2.5-1,2,3,& 5. p More? See pages

Simple vibration model of a building subjected to ground motion - skip. Figure 2.5–1 Buildings designed to withstand earthquakes must have natural frequencies of vibration that are NOT close to the natural frequency of the ground motions to prevent deadly resonances. Building’s natural frequencies are determined by masses of floors and by lateral stiffness of the supporting columns. They are roots of the characteristic polynomial. >> k = 7e6; m = 1000; >> alpha = k/(4*pi^2*m); >> p1 = [ -1 0 alpha]; >> p2 = [ *alpha]; >> p3 = [ alpha^2 0 -2*alpha^3]; >> p4 = conv(p2,p2)-[ alpha^2]; >> p5 = conv(p1,p4); >> p6 = p5 + [ p3]; >> p6 p6 = Columns 1 through e Column e+006 >> r = roots(p6) r = >> pos = r (r>0) pos =

Function C = cell(n) C = cell(n,m) celldisp(C) cellplot(C) C = num2cell(A) [X,Y,...] = deal(A,B,...) [X,Y,...] = deal(A) iscell(C) Description Creates an n × n cell array C of empty matrices. Creates an n × m cell array C of empty matrices. Displays the contents of cell array C. Displays a graphical representation of the cell array C. Converts a numeric array A into a cell array C. Matches up the input and output lists. Equivalent to X = A, Y = B,.... Matches up the input and output lists. Equivalent to X = A, Y = A,.... Returns a 1 if C is a cell array; otherwise, returns a 0. Cell array functions. Table 2.6–1 - Skip! 2-54 More? See pages

Structures - optional Structures allow to store arrays of different type and size together. The elements are accessed using named fields with dot operator: student.name =‘John Smith’; student.SSN = ‘ ’; student.tests = [ ] Arrays of Structures – just add index to student: student(2).name = ‘Mary Jones’; student(2).SSN = ‘ ’; … To reassign test 2 of Mary Jones: student(2).tests(2) = Figure 2.7–1

Function names = fieldnames(S) F = getfield(S,’field’) isfield(S,’field’) Description Returns the field names associated with the structure array S as names, strings. Returns the contents of the field ’field’ in the structure array S. Equivalent to F = S.field - better. Returns 1 if ’field’ is the name of a field in the structure array S, and 0 otherwise. Structure functions Table 2.7–1 2-56

Structure functions Table 2.7–1 (continued) S = rmfield(S,’field’) S = setfield(S,’field’, V) S = struct(’f1’,’v1’,’f 2’,’v2’,...) Removes the field ’field’ from the structure array S. Sets the contents of the field ’field’ to the value V in the structure array S. Better to use dot! Creates a structure array with the fields ’ f1 ’, ’ f2 ’,... having the values ’ v1 ’, ’ v2 ’, More? See pages In class: T2.7-1,2,3, p123

The remaining slides are figures from the chapter and its homework problems. 2-58

Plot for Example 2.3–6. Figure 2.3–5 2-59

2-60 Figure 2.3–3

Aortic pressure response for Example 2.3–3. Figure 2.3–4 2-61

Figure P

Figure P

Figure P

Figure P

Figure

Figure P