Engr/Math/Physics 25. Chp2 MATLAB Arrays: Part-2. Bruce Mayer, PE Licensed Electrical & Mechanical Engineer [email protected] Learning Goals. Learn to Construct 1D Row and Column Vectors Create MULTI-Dimensional ARRAYS and MATRICES
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Engr/Math/Physics 25
Chp2 MATLABArrays: Part-2
Bruce Mayer, PE
Licensed Electrical & Mechanical [email protected]
>> r = [ 7 11 19];
>> v = 2*r
v =
14 22 38
Multiplying an Array B by a scalar w produces an Array whose elements are the elements of B multiplied by w.
>> B = [9,8;-12,14];
>> 3.7*B
ans =
33.3000 29.6000
-44.4000 51.8000
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 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:
x.*y = [x(1)y(1), x(2)y(2), ... , x(n)y(n)]
x = [2, 4, – 5], y = [– 7, 3, – 8]
If u and v are column vectors, the result of u.*v is a column vector. The Transpose operation z = (x’).*(y’) yields
The array operations are performed between the elements in corresponding locations in the arrays. For example, the array multiplication operation A.*B results in an array C that has the same size as A and B and has the elements cij = aij bij . For example, if
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.3166 4.3589
>> u = [1,2,3];
>> f = exp(u)
f =
2.7183 7.3891 20.0855
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 = (eusinr)•cos2r, enter command
>> z = exp(u).*sin(r).*(cos(r)).^2
z =
1.0150 -0.0001 2.9427
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. Both arrays must have the same size. The symbol for array right division is ./
Recallr = [ 7 11 19]u = [1,2,3]
then z = r./u gives
>> z = r./u
z =
7.0000 5.5000 6.3333
Consider
A =
24 20
-9 4
B =
-4 5
3 2
>> A./B
ans =
-6 4
-3 2
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 [33, 53, 83] = [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 [32, 34, 35] = [9, 81, 243].
>> A = [5 6 7; 8 9 8; 7 6 5]
A =
5 6 7
8 9 8
7 6 5
>> B = [-4 -3 -2; -1 0 1; 2 3 4]
B =
-4 -3 -2
-1 0 1
2 3 4
>> C = A.^B
C =
0.0016 0.0046 0.0204
0.1250 1.0000 8.0000
49.0000 216.0000 625.0000
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
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
A Vector and Matrix May be Multiplied if they meet the Conformability Critera: (1xm)*(mxp) or (kxm)*(mx1)
Given Vector-a, Matrix-B, and aB; Find Dims for all
Greek letter sigma (Σ, for sum) is another convenient way of handling several terms or variables – The Definition
In General the product of Conformable Matrices A & B when
e.g.;
>> A = [3 1.7 -7; 8.1 -0.31 4.6; -1.2 2.3 0.73; 4 -.32 8; 7.7 9.9 -0.17]
A =
3.0000 1.7000 -7.0000
8.1000 -0.3100 4.6000
-1.2000 2.3000 0.7300
4.0000 -0.3200 8.0000
7.7000 9.9000 -0.1700
>> B = [0.67 -7.6; 4.4 .11; -7 -13]
B =
0.6700 -7.6000
4.4000 0.1100
-7.0000 -13.0000
>> C = A*B
C =
58.4900 68.3870
-28.1370 -121.3941
4.2060 -0.1170
-54.7280 -134.4352
49.9090 -55.2210
Matrix multiplication is generally not commutative. That is, AB ≠ BAevenif BA is conformable
Consider an Illustrative Example
Two EXCEPTIONS to the NONcommutative 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.
Commutation of the Null & Identity Matrices
Identity Matrix is a square matrix and also it is a diagonal matrix with 1’s along the diagonal
similar to scalar “1”
Use the eye(n) command to Form an nxn Identity Matrix
>> I = eye(5)
I =
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
>> Z24 = zeros(2,4)
Z24 =
0 0 0 0
0 0 0 0
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.
function [q,r] = deconv(num,den) produces 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.
Find the PRODUCT for
>> f = [2 -7 9 -6];
>> g = [13,-5,3];
>> prod = conv(f,g)
prod =
26 -101 158 -144 57 -18
Find the QUOTIENT
>> f = [2 -7 9 -6];
>> g = [13,-5,3];
>> [quot1,rem1] = deconv(f,g)
quot1 =
0.1538 -0.4793
rem1 =
0 0.0000 6.1420 -4.5621
The function roots(h) computes the roots of a polynomial specified by the coefficient array h. The result is a column vector that contains the polynomial’s roots.
>> r = roots([2, 14, 20])
r =
-5
-2
>> rf = roots(f)
rf =
2.0000
0.7500 + 0.9682i
0.7500 - 0.9682i
>> rg = roots(g)
rg =
0.1923 + 0.4402i
0.1923 - 0.4402i
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.
Plot over (−4 ≤ x ≤ 7) the function
Time For
Live Demo
Cell-Arrays&StructureArrays
x = linspace(-4,7,20);
p3 = [2 -7 9 -6];
y = polyval(p3,x);
plot(x,y, x,y, 'o', 'LineWidth', 2), grid, xlabel('x'),...
ylabel('y = f(x)'), title('f(x) = 2x^3 - 7x^2 + 9x - 6')
>> f = [2 -7 9 -6];
>> x = [-4:0.02:7];
>> fx = polyval(f,x);
>> plot(x,fx),xlabel('x'),ylabel('f(x)'), title('chp2 Demo'), grid