Intro. to Mathematical Operation
Transcript of Intro. to Mathematical Operation
-
7/31/2019 Intro. to Mathematical Operation
1/22
Introduction to
Mathematical Modelling
-
7/31/2019 Intro. to Mathematical Operation
2/22
MATRIX OPERATION
-
7/31/2019 Intro. to Mathematical Operation
3/22
Multiplication of vectors
The matrix product of a row vector u with a column vectorw is defined in a same way as the vector dot product, that is:
If each vector has n element. Thus the result of multiplying a 1
X n vector times an n X 1 vector is a 1 X 1 array, that is a
scalar.
= 32
-
7/31/2019 Intro. to Mathematical Operation
4/22
Using MATLAB
>> a=[1 2 3]>> b=[4;5;6]
>> a*b
ans =
32
>> a=[1 2 3]
>> b=[4 5 6]
>> a*b
??? Error using ==> mtimes
Inner matrix dimensions must agree.
-
7/31/2019 Intro. to Mathematical Operation
5/22
Vector-matrix multiplication
Scalar result of each row-column multiplication forms an elementin the result, which is column vector :
For example :
The result of multiplying a 2x2 matrix times a
2x1 vector is a 2x1 array; that is a column vector.
==
=
-
7/31/2019 Intro. to Mathematical Operation
6/22
Using MATLAB
>> c=[2 7;6 -5]
>> d=[3;9]
>> e=c*d
e =
69
-27
-
7/31/2019 Intro. to Mathematical Operation
7/22
Matrix-Matrix Multiplication
In the product of two matrices AB, the number ofcolumnsin A must equal the number of rowsin B. Therow-column multiplications form column vectors, andthese column vectors form the matrix result. Theproduct AB has the same number of rowsas A and the
same number of columnsas B. For example,
6 2
10 3
4 7
9 8
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)
64 24
75 116
1 116
=
-
7/31/2019 Intro. to Mathematical Operation
8/22
Use the operator * to perform matrix multiplication inMATLAB. The following MATLAB session shows how to
perform the matrix multiplication shown in (2.44).
>>A = [6,-2;10,3;4,7];
>>B = [9,8;-5,12];
>>A*B
ans =
64 24
75 116
1 116
-
7/31/2019 Intro. to Mathematical Operation
9/22
Matrix multiplication does not have the commutativeproperty; that is, in general, AB BA.A simple
example will demonstrate this fact:
AB =6 2
10 3
9 8
12 14=
78 20
54 122
BA =9 8
12 14
6 2
10 3=
134 6
68 65
whereas
Reversing the order of matrix multiplication is acommon and easily made mistake.
-
7/31/2019 Intro. to Mathematical Operation
10/22
Matrix Division Matrix division uses both the right and left division operators, /
and \ We can add, subtract, multiply and exponentiate matrices, but we
cannot divide two matrices.
We might define matrix division as a process to do the same.
xy= z
Simple algebra tells us that dividing both sides by y yields a truestatement for the same variables:
x= z/ y ; Which is the same as :x=z*1/y
Or, xequals z times the inverse ofy. So matrix division can bedefined as multiplying both sides of an equation by the inverseof a matrix. For two by two matrices this should be as follows:
AB= C becomes B= (A -1)C
-
7/31/2019 Intro. to Mathematical Operation
11/22
Matrix notation enables us to represent multiple equationsas a single matrix equation. For example, consider the
following set:2x1+ 9x2= 5
3x1- 4x2= 7
This set can be expressed in vector-matrix form as
which can be represented in the following compact form
Ax = b
2 93 -4
x1x2
=57
-
7/31/2019 Intro. to Mathematical Operation
12/22
For the equation set Ax = b,
if |A| = 0, then there is no unique solution.
Depending on the values in the vector b, theremay be no solution at all, or an infinite number ofsolutions.
We can compute |A| by command det(A)= -35or by calculateEg. if A= = (2)(-4) - (3)(9) = -352 9
3 -4
-
7/31/2019 Intro. to Mathematical Operation
13/22
MATLAB provides the left-divisionmethod for solving the
equation set Ax = b. The left-division method is based onGauss elimination.
To use the left-division method to solve for x, type x = A\b.
For example,6 -10 X1 = 2
3 -4 X2 5
>> A = [6, -10; 3, -4]; b = [2; 5];
>> x = A\b
x =
7 4
This method also works in some cases where the number ofunknowns does not equal the number of equations.
Left Division Method
-
7/31/2019 Intro. to Mathematical Operation
14/22
If the number of equations equals the number ofunknowns and if |A| 0, then the equation set
has a solution and it is unique.
If |A| = 0 or if the number of equations does notequal the number of unknowns, then you mustuse the methods presented in Section 6.4.
6-11
-
7/31/2019 Intro. to Mathematical Operation
15/22
Matrix Inverse
The MATLAB command inv(A) computes the inverse of
the matrix A. The following MATLAB session solves thefollowing equations using MATLAB.
2x+ 9y= 53x- 4y= 7
>>A = [2,9;3,-4];
>>b = [5;7]
>>x = inv(A)*b
x =
2.37140.0286
If you attempt to solve a singular problem using the inv
command, MATLAB displays an error message.
-
7/31/2019 Intro. to Mathematical Operation
16/22
Underdetermined Systems
An underdetermined systemdoes not containenough information to solve for all of the unknownvariables, usually because it has fewer equationsthan unknowns.
Thus an infinite number of solutions can exist,with one or more of the unknowns dependent onthe remaining unknowns.
For such systems the matrix inverse method andCramers method will not work.
-
7/31/2019 Intro. to Mathematical Operation
17/22
A simple example of an underdetermined systems isthe equation
x+ 3y= 6
All we can do is solve for one of the unknowns interms of the other; for example, x= 6 - 3y. An infinitenumber of solutions satisfy this equation.
When there are more equations than unknowns, theleft-division method will give a solution with some ofthe unknowns set equal to zero. For example,>>A = [1, 3]; b = 6;
>>solution = A\b
solution =
0
2
which corresponds to x= 0 and y= 2.
-
7/31/2019 Intro. to Mathematical Operation
18/22
Special Product
Syntax
C = cross(A,B)
C = cross(A,B,dim)Description
C = cross(A,B) returns the cross product of the vectors A and B.That is, C = A x B. A and B must be 3-element vectors. If A andB are multidimensional arrays, cross returns the cross product of
A and B along the first dimension of length 3. C =cross(A,B,dim) where A and B are multidimensional arrays,returns the cross product of A and B in dimension dim . A and Bmust have the same size, and both size(A,dim) and size(B,dim)must be 3.
Examples
The cross and dot products of two vectors are calculated as shown:
a = [1 2 3];
b = [4 5 6];
c = cross(a,b)
c = -3 6 -3
-
7/31/2019 Intro. to Mathematical Operation
19/22
Special ProductSyntax
C = dot(A,B)
C = dot(A,B,dim)
DescriptionC = dot(A,B) returns the scalar product of the vectors A and B. A
and B must be vectors of the same length. When A and B areboth column vectors, dot(A,B) is the same as A'*B.
For multidimensional arrays A and B, dot returns the scalar product
along the first non-singleton dimension of A and B. A and Bmust have the same size.
C = dot(A,B,dim) returns the scalar product of A and B in thedimension dim.
Examples
The dot product of two vectors is calculated as shown:a = [1 2 3];
b = [4 5 6];
c = dot(a,b)
c = 32
-
7/31/2019 Intro. to Mathematical Operation
20/22
Summary Vectors/Matrix
Matlab also gives you the following operation such as transpose(`),
Addition (+) and multiplication (*), multiplication (*) by
transpose (`), dot product, u.v and cross product, uxv.
-
7/31/2019 Intro. to Mathematical Operation
21/22
Operation Matlab command Mathemathical
tools/comment
Define two vectors
u and v
>> u=[1 2 3]
u=
1 2 3>>v=[2 4 6]
v=
2 4 6
Define a pair of vector
u=[1 2 3]
&v=[2 4 6]
Transpose (`)
Addition (+)
Subtraction (-)
>> u`
Ans =
12
3
>> v`
Ans=
2
4
6>> u+v
Ans=
3 6 9
>> u-v
Ans=
-1 -2 -3
Interchange the
elements between rows
and columnsThe dimension of both
matrices must same for
addition and
substraction.
u v = [ 1 2 3 ] [ 2
4 6]
1x31x3
-
7/31/2019 Intro. to Mathematical Operation
22/22
Multiplication (*) >> u * v
??? Error using==>*
The dimension dont
agreeu v = [ 1 2 3 ] [ 2 4 6]
1x3 1x3
Multiplication (*)
by transpose (`)
>> u*v`
Ans =
28
Taking the
transpose works
uv` = [ 1 2 3 ] 2
4
6
1x3 3x1
dot product, u.v >> dot(u,v)
Ans=
28
The dot product is the
same thing
cross product, uxv >> cross(u,v)
Ans=
0 0 0
The cross product
works only for 3-D
vectors