computer applications lab lab 3 arithmetic operations on ... · computer applications lab lab 3...
TRANSCRIPT
Computer Applications LabComputer Applications LabLab 3Lab 3
Arithmetic Operations on Arithmetic Operations on Arrays Arrays
Chapter 2Chapter 2Sections 3,4,5Sections 3,4,5
Dr. Iyad Jafar
Adapted from the publisher slides
OutlineOutline
� Element-by-element operations
� Matrix operations
� Special Matrices
� Polynomial operations using arrays� Polynomial operations using arrays
2
ElementElement--byby--Element OperationsElement Operations
� It is used to perform the desired mathematical operation between corresponding elements of arrays or by corresponding elements of arrays or by applying the operation between a scalar and each element of the array
3
ElementElement--byby--Element OperationsElement Operations
� scalar-array operations◦ Addition B = A + 4
◦ Subtraction B = A – 4
◦ Multiplication B = A * 4
◦ Division B = A / 4
◦ The operation is repeated for each element in the array separately. ◦ The operation is repeated for each element in the array separately.
� Example
4
ElementElement--byby--Element OperationsElement Operations
� Array-array operations
◦ The mathematical operation is repeated between corresponding elements in the arrays
◦ Arrays should be of equal dimensions
� Example� Example
5
NoteSubtraction is
performed in similar way
ElementElement--byby--Element OperationsElement Operations
� Array-array operations
◦ For multiplication (division) use .* (./) to multiply (divide) corresponding elements of the two arrays
� Example
6
NoteDivision is performed
in similar way
ElementElement--byby--Element OperationsElement Operations
7
ElementElement--byby--Element OperationsElement Operations
8
ElementElement--byby--Element OperationsElement Operations
� Array-array operations
◦ For exponentiation, use .^ to raise each element in the array to specified base
� Example
9
ElementElement--byby--Element Element OperationsOperations
� Can be used to evaluate functions at a set of points
� Example
evaluate f(x,y) = xy^2 + 8x at (1,2), (3,1), (5,9)evaluate f(x,y) = xy^2 + 8x at (1,2), (3,1), (5,9)
>> x = [1,3,5];
>> y = [2,1,9];
>> f = x.*(y.^2) + 8*x
ans =
12 27 445
10
Matrix OperationsMatrix Operations
� We have discussed addition and subtraction earlier
� Multiplication
11
Matrix OperationsMatrix Operations• In MATLAB, use * to perform multiplication of matrices
12
Matrix OperationsMatrix Operations� Matrix division is more challenging topic than
matrix multiplication.
� Matrix division uses both right and left division operators, / and \, for various applications
� One application is solving linear algebraic equations� One application is solving linear algebraic equations6x+ 12y+ 4z= 70
7x–2y+ 3z= 5
2x+ 8y–9z= 64
>> A = [6,12,4;7,-2,3;2,8,-9];
>> B = [70;5;64];
>> Solution = A\B
Solution = 3 5 -2
The solution is x= 3, y= 5,and z= –2.13
Matrix OperationsMatrix Operations
� Solving the previous set of linear equations can be done by matrix inversion.
>> A = [6,12,4;7,-2,3;2,8,-9];
>> B = [70;5;64];
>> solution = inv(A) * B
solution =
3.0000
5.0000
-2.0000
14
Matrix OperationsMatrix Operations
� Matrix exponentiation
◦ An is equivalent to multiplying the matrix by itself n times
◦ A should be a square matrix
◦ AB , where B is a matrix, is not defined
15
Special MatricesSpecial Matrices
� Identity matrix I◦ eye(n) , eye(m,n) , eye(m,n)
� All-ones matrix � All-ones matrix ◦ ones(n) , ones(m,n) , ones (size(A))
� All-zeros matrix◦ zeros(n), zeros(m,n), zeros(size(A))
16
Polynomial Operations Using ArraysPolynomial Operations Using Arrays� The polynomial
can be represented in MATLAB by
� Use the function roots(A) to find the polynomial roots
>> c = roots ([1,5,6])
n n 1 n 21 2 3 n 1f (x) = a x + a x + a x + ... + a− −
+
1 2 n 1A = [a , a , ... , a ]+
>> c = roots ([1,5,6])
c = -2 -3
� Use the function poly(c) to find the coefficients of the polynomial from its roots.
>> c = [-1,2]
>> ploy(c)
ans = 1 -1 -2
This means that the answer is x2 – x – 2 17
Polynomial Operations Using ArraysPolynomial Operations Using Arrays
� Multiplication
Use the function conv(a,b) to compute 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.
� DivisionUse the function [q,r] = deconv(num,den) to compute 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.
18
Polynomial Operations Using ArraysPolynomial Operations Using Arrays
� Example: if a = 9x3 – 5x2 + 3x + 7 and b = 6x2 + 2, then find a x b and a / b.
>> a = [9,-5,3,7] ; b = [6,0,2] ; >> product = conv(a,b)
product = product = 54 -30 36 32 6 14
(This is equivalent to 54x 5 – 30x 4 + 36x 3 + 32x 2 + 6x + 14)
>> [Q,R] = deconv(a,b) Q =
1.5000 -0.8333R =
0 0 0 8.6667
19
Polynomial Operations Using ArraysPolynomial Operations Using Arrays
� Evaluation of 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 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.
>> a = [9,-5,3,7];
>> x = [-2,3,4,5];
>> f = polyval(a,x)
f = -91 214 515 1022
20