Download MATLAB Arrays: Learning Goals, Operations, and Multiplication and more Slides Computational Methods in PDF only on Docsity!
Chp2 MATLAB
Arrays: Part-
Learning Goals
- Learn to Construct 1D Row and Column
Vectors
- Create MULTI-Dimensional ARRAYS and
MATRICES
- Perform Arithmetic Operations on Vectors
and Arrays/Matrices
- Analyze Polynomial Functions
Scalar-Array Multiplication
- Multiplying an Array B by a scalar w produces an Array whose elements are the elements of B multiplied by w.
− =
− 44. 4 51. 8
- 3 29. 6 12 14
9 8
- 7
>> B = [9,8;-12,14]; >> 3.7B ans = 33.3000 29. -44.4000 51.*
Using MATLAB
Array-Array Multiplication
- Multiplication of TWO ARRAYS is not nearly as straightforward as Scalar-Array Mult.
- MATLAB uses TWO definitions for Array-Array multiplication:
- ARRAY Multiplication
- also called element-by-element multiplication
- MATRIX Multiplication (see also MTH6)
- DIVISION and EXPONENTIATION must also be CAREFULLY defined when dealing with operations between two arrays.
Array-Array Operations
- 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×n elements:
if x and y are row vectors. For example, if
x.y = [x(1)y(1), x(2)y(2), ... , x(n)y(n)]*
x = [2, 4, – 5], y = [– 7, 3, – 8]
then z = x.*y gives
z =[ 2 ( − 7 ), 4 ( ) 3 , − 5 (− 8 )] =[− 14 , 12 , 40 ]
Array-Array Operations cont
- If u and v are column vectors, the result of u.v is a column vector. The Transpose operation z = (x’).(y’) yields 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
( ) ( ) ( )
z
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.
Array-Array Operations cont
- The built-in MATLAB functions such as sqrt(x) and exp(x) automatically operate on array arguments to produce an array result of the same size as the array argument x - Thus these functions are said to be VECTORIZED
- Some Examples
>> r = [7 11 19]; >> h = sqrt(r) h = 2.6458 3. 4.
>> u = [1,2,3]; >> f = exp(u) f = 2.7183 7. 20.
Array-Array Operations cont
- However, when multiplying or dividing these functions, or when raising them to a power, we must use element-by-element dot (.) operations if the arguments are arrays.
- To Calc: z = (e u^ sin r )•cos^2 r , enter command
>> z = exp(u).sin(r).(cos(r)).^ z = 1.0150 -0.0001 2.**
MATLAB returns an error message if the size of r is not the same as the size of u. The result z will have the same size as r and u.
Array-Array DIVISION cont.
>> A./B ans = -6 4 -3 2
Taking C = A./B yields
− =
−
= 3 2
4 5 9 4
24 20 A B
( )
−
− =
−
−
3 2
6 4 9 3 4 2
24 4 20 5 C
A = 24 20 -9 4 B = -4 5 3 2
Array EXPONENTIATION
- MATLAB enables us not only to raise arrays to powers but also to raise scalars and arrays to ARRAY powers.
- Use the .^ symbol to perform exponentiation on an element-by-element basis - if x = [3, 5, 8], then typing x.^3 produces the array [3^3 , 53 , 8^3 ] = [27, 125, 512]
- We can also 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].
Matrix-Matrix Multiplication
- Multiplication of MATRICES requires meeting the CONFORMABILITY condition
- The conformability condition for multiplication is that the COLUMN dimensions (k x m ) of the LEAD matrix A must be EQUAL to the ROW dimension of the LAG matrix B ( m x n)
- If
−
=
−
5 12
9 8
4 7
10 3
6 2 A B Then
AB = C ( C is 3x2) BA = Error
Matrix-Mult. Mechanics
- Multiplication of A ( k x m) and B (m x n ) CONFORMABLE Matrices produces a Product Matrix C with Dimensions ( k x n )
- The elements of C are the sum of the products of like-index Row Elements from A , and Column Elements from B ; to whit
−
−
1 116
75 116
64 24
4 9 7 5 4 8 7 12
10 9 3 5 10 8 312
6 9 2 5 6 8 2 12
5 12
9 8
4 7
10 3
6 2
C
Summation Notation Digression
- Greek letter sigma (Σ, for sum) is another convenient way of handling several terms or variables – The Definition
Σ
For the previous example
q q q
q
p
sum = xp = x + x + x + x − + x − + x
=
∑ 2 3 2 1 1
[ c 11 (^) c 12 c 13 ] = [ a 11 b 11 + a 12 b 21 a 11 b 12 + a 12 b 22 a 11 b 13 + a 12 b 23 ]
∑
2 1 1 1 p
a (^) p bp ∑=
2 1 1 2 p
a (^) p bp ∑=
2 1 1 3 p
a p bp
Matrix Mult by Σ-Notation
- In General the product of Conformable Matrices A & B when
Then Any Element, c (^) ij , of Matrix C for
- i = 1 to k (no. Rows) j = 1 to n (no. Cols)
k k kn
n
n
c c c
c c c
c c c
1 2
21 22 2
11 12 1
AB C
∑
=
=
=
p m
p
cij aipbpj 1
e.g.; (^) ∑
=
=
=
7
1
53 5 3
p
p
c a p bp
A ⇒ ( k × m ) B ⇒ ( m × n )