Mathematical Operations
Mathematical Operations
Mathematical Operations
Mathematical Operations
Matrix & Linear Algebra Operations
Element-by-Element(array) Operations
1
Introduction
• Matlab is designed to carry out advanced array
operations that have many applications in science
and engineering.
• We have seen scalars operate just like a number.
• Vectors and matrices mathematical operations are
more complex.
• We begin with basic mathematics operations of
matrix and linear algebra.
2
Matrix & Linear Algebra
Operation
Add
Subtract
Multiplication
Division
Inverse
3
Addition & Subtraction
• To add/subtract, arrays must have identical size.
• A scalar can be added to an array.
4
Multiplication
• If A & B are arrays, * is executed according to the
rules of linear algebra.
• A * B - no. of columns A must be equal to no. of rows B
• A*B ≠ B * A - not commutative
>>g=b*a? 5
Multiplication - cont’d
>> a=[3 6 7]; Row vector
>> b=[1;5;3]; Column vector
>> m=a*b
m =
Dot product
54 Scalar
of 2 vectors
>> n=b*a
n =
3 6 7
15 30 35 3 x 3 matrix
9 18 21
>> z=3*n Scalar * matrix
z =
9 18 21
45 90 105
27 54 63 6
Division
• Division also is executed according to the rules of
linear algebra
• Identity matrix - eye command
• Inverse matrix - inv( )function, or ^-1
>> a=[7 4 2;5 2 7;9 5 7]; Square matrix
>> b=inv(a)
b =
1.0000 0.8571 -1.1429
-1.3333 -1.4762 1.8571
-0.3333 -0.0476 0.2857 a*b=?
A matrix has an inverse only if it is square and its determinant is not zero
Use det command to calculate the determinant of a matrix (square)
7
Division – cont’d
>> a*a^-1
ans =
8
Division – cont’d
Two types of array division:
• Left division, \
Use to solve matrix equation AX=B, where X and B
are column vectors.
The solution for AX=B is X=A-1B
Both of the above give the same result, but for large matrices the \ is more accurate
9
Division – cont’d
• Right division, /
Use to solve matrix equation XC=D, where X and D
are row vectors.
10
Division Example
• Solving linear equations
3x + 6y + 2z =0
4x - 3y + 5z =9
2x + 7y + 2z =4
3 6 2 x 0 3 4 2
y = 9 x y z
4 -3 5 6 -3 7 = 0 9 4
2 7 2 z 4 2 5 2
11
Division Example - cont’d AX=B form
X = B\A OR X=A-1B
>> Xc=A^-1*B ?
12
Division Example – cont’d XC=D form
Perform
element-to-element
exponential, multiplication & division
on vectors and matrices
14
Element-by-Element Operations
When multiplication and division symbols are used
with arrays, the mathematical operations follow the
rules of linear algebra.
.* .\ ./ .^
16
Element-by-Element Operations - cont’d
>> A=[2 9 7;8 6 3]; Element-by-element
multiplication
>> B=[8 5 6;5 7 9];
>> C=A.*B >> A*B ?
C = >> A*B’?
16 45 42 >> A’*B?
40 42 27 >> A’*B’?
50
40
x-y plot
30
20
10
0
1 2 3 4 5 6 7 8 18
Try This !
1. Define the following vectors:
u = [4 -2 3] v = [-2 5 1]
What will be displayed if the following commands are executed.
a) u.*v b) u*v’ c) u’*v
>> x1=[0:36:180];
>> y1=sind(x1) Degree angle
y1 =
0 0.5878 0.9511 0.9511 0.5878 0
20
Arrays in Built-in Math Functions - cont’d
>> r=sqrt(m)
r =
1.0000 2.2361 2.8284
6.7082 8.0623 11.4018
21
Problem Examples
22
Problem Example 1
A simply supported beam carries a udl along the whole span.
Determine the bending moment at every metre of the beam.
r a = ql/2
mx=r a x – qx2/2
>> q=70; ra =
>> l=6; 210
>> ra=q*l/2;
>> x=[0:6]; x =
>> mx=ra.*x-q.*x.^2/2 0 1 2 3 4 5 6
mx =
0 175 280 315 280 175 0
23
Problem Example 2
The coefficient of friction, m, is determined experimentally by
measuring force F required to move mass m. The result of the
experiment is given below. Determine m for each test, and the
average of all the tests.
Test 1 2 4 5 6 7
m (kg) 2 4 7 10 15 25
F (N) 12.9 24.1 43.1 61.1 90 152
m=F/mg
ft =
644.5459 -234.8355
fr =
685.9935
th =
-20.0188
26
Problem Example 4
Determine the length of the sides of the polygon shown.
27
Problem Example 5
For the function y = , calculate the value of y
>> x =[-2:1];
>> y=(x.^2-3)./(x+4)
y =
0.5000 -0.6667 -0.7500 -0.4000 0.1667 0.8571
28
Problem Example 6
Solve the following simultaneous equations.
3x + 2y =2
4x + 3y = 4
29
More practice see Problem A3
Thank You
30