Matlab Matrix Operation
Matlab Matrix Operation
Matrix 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.
– Scalars operate just like a number.
– Vectors and matrices mathematical operations are more
complex.
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 Dot product of 2 vectors
>> m=a*b >> c =[1 5 3];
m = >> m1=dot(a,b)
54
>> n=b*a matrix * matrix
n =
3 6 7
15 30 35
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 =
a*b=?
1.0000 0.8571 -1.1429
a*inv(a)=?
-1.3333 -1.4762 1.8571
a*a^1=?
-0.3333 -0.0476 0.2857
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
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
8
Division – cont’d
• Right division, /
Use to solve matrix equation XC=D, where X and D
are row vectors.
9
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
10
Division Example - cont’d AX=B form
X = A\B OR X=A-1B
>> v3=a^-1*b ?
11
Division Example – cont’d XC=D form
>> x6=d*c^-1 ? 12
Scalars versus Arrays
• Matlab considers all variables as arrays, so the
following arithmetic operators +, -, *, / and ^
have to obey the rules of linear algebra.
• For scalar variables (1 by 1 array) the usual
rules of algebra apply.
• Dot notation (.*, ./ and .^) enables arithmetic
operations perform on same size array on
element by element basis.
Array Operations
(element-by-element operations)
Perform element-by-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.
.* .\ ./ .^
Element-by-element calculation are very useful for
calculating the values of a function at many values.
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
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 d) dot(u,v)
>> x=[0:10:40];
>> sqrt(x)
ans =
0 3.1623 4.4721 5.4772 6.3246
>> x1=[0:36:180];
>> x1= x1/180*pi;
>> y1=sin(x1)
y1 =
0 0.5878 0.9511 0.9511 0.5878 020
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
Arrays in Built-in Math Functions - Example
Given the coordinates of A(2,10) and B(5,3). Determine;
a) dx and dy,
b) gradient of AB, and
c) distance AB. >> a=[2 10];b=[5 3];
>> deltaxy=b-a
deltaxy =
3 -7
>> grad=deltaxy(2)/deltaxy(1)
grad =
-2.3333
>> l=sqrt(sum((a-b).^2))
l =
7.6158
22
Problem Examples
23
Problem Example 1
𝒙𝟑 +𝟓𝒙
Plot the function, y = from x = 1,2,3,4, …. 9
𝟒𝒙𝟐 −𝟕
>> x=1:9;
>> y=(x.^3+5*x)./(4*x.^2-7);
>> plot(x,y)
2.5
1.5
-1
-1.5
-2
1 2 3 4 5 6 7 8 9 24
25
Problem Example 2
A simply supported beam carries a udl along the whole span.
Determine the bending moment at every metre of the beam.
>> q=70;
>> l=6;
>> ra=q*l/2;
>> x=[0:6]; vector
>> mx=ra.*x-q.*x.^2/2 Element by element
ra =
210
r a = ql/2
mx=r a x – qx2/2 x =
0 1 2 3 4 5 6
mx =
0 175 280 315 280 175 0
>> f1=450;f2=200;f3=600;
>> a1=50*d2r;a2=0*d2r;a3=-75*d2r;
>> fx=f1*cos(a1)+f2*cos(a2)+f3*cos(a3); Sum of forces in x direction
>> fy=f1*sin(a1)+f2*sin(a2)+f3*sin(a3); Sum of forces in y direction
>> fr=sqrt(fx^2+fy^2) Resultant force
fr =
685.9935
>> th=atand(fy/fx) Angle of the resultant force
th =
atand returns a degree For angle between
-20.0188 angle between 0 and 360o use 30
-90o and 90o if statement
Problem Example 4 (b) – element by element
Determine the resultant of the forces acting on the bracket shown.
31
Problem Example 4 (c) - vector analysis
Determine the resultant of the forces acting on the bracket shown.
F = S(Fx2 + Fy2)
tan θ = Fy/ Fx
scalar
>> x1=0;x2=100;x3=90;x4=10;
>> y1=20;y2=25;y3=60;y4=40;
>> s1=sqrt((x1-x2)^2+(y1-y2)^2)
s1 =
100.1249
Sab = S[(Xa- Xb)2 + (Ya - Yb) 2] >> s2=sqrt((x2-x3)^2+(y2-y3)^2)
s2 =
36.4005
S1=S[(x1-x2)2 + (y1-y2)2] >> s3=sqrt((x3-x4)^2+(y3-y4)^2)
S2=S[(x2-x3)2 + (y2-y3)2] s3 =
S3=S[(x3-x4)2 + (y3-y4)2] 82.4621
S4=S[(x4-x1)2 + (y4-y1)2] >> s4=sqrt((x4-x1)^2+(y4-y1)^2)
s4 =
37
22.3607
Problem Example 6(b) - element by element
Determine the length of the sides of the polygon shown.
Sab = S[(Xa- Xb)2 + (Ya - Yb) 2]
40
Problem Example 7 (cont’d)
Joint A: F1 + F3 + F4 cos30 = 0 ∑Fx
F2 + F4 sin30 = 0 ∑Fy
F5 + F6 sin 50 = 0 ∑Fy
KF=P
F = K-1 P Use Matlab to solve 41
Problem Example 7 (cont’d)
c1=cosd(30);s1=sind(30);c2=cosd(50);s2=sind(50);
>> k=[1 0 1 c1 0 0
0 1 0 s1 0 0
0 0 -1 0 0 -c2
0 0 0 0 1 s2
0 0 0 -c1 0 c2
0 0 0 -s1 0 -s2];
>> p=[0 0 0 0 10 -25]';p=-p;
>> f=k\p
f=
-10.0000 F1
4.2695 F2
17.3950 F3
-8.5390 F4
20.7305 F5
-27.0618 F6
>>
42
43
Problem 8
Determine the support reactions for the beam shown using slope
deflection method. Use Matlab matrix operation to solve the matrix.
𝐸𝐼 𝐸𝐼
MNF = 4 𝑁 + 2 + FEM
𝐿 𝐿 𝐹
In Matrix Form
(2𝐸𝐼) (2𝐸𝐼) (10)(8)
MAB = 4 𝑨 +2 𝑩 +
8 8 8 1 0.5 0 𝑨 10
MBA = 2
(2𝐸𝐼)
𝑨 +4
(2𝐸𝐼)
𝑩 -
(10)(8) 0.5 1 0 -10
8 8 8 𝑩
EI 0 2 1 12
2
MBC = 4
(3𝐸𝐼)
𝑩 + 2
(3𝐸𝐼)
𝑪 +
4 6
0 1 2 𝑪 -12
6 6 12
(3𝐸𝐼) (3𝐸𝐼) 4 62
MCB = 2 𝑩 + 4 𝑪 -
6 6 12
44
Problem 8 (cont’d)
Condition
MAB = 0
MBA + MBC = 0
In Matrix Form 𝑪 = 0
𝑨 MAB = 0
1 0.5 0 10 0 Rows & columns
EI 𝑩 + = related to C is
0.5 3 1 𝑪 2 0 MBA + MBC = 0 removed
Rearrange In MATLAB
1 0.5 𝑨 -10 >> a=[1 0.5;0.5 3];
EI = >> f=[-10;-2]; 𝑨 𝟏 -10.5455
0.5 3 𝑩 -2 >> t=a\f =
𝑩 𝑬𝑰 1.0909
t=
A = B -10.5455 A
B
= A-1B 1.0909
45
Problem Example 8 (cont’d)
Substitude A and B in the slope deflection eqns.
MBC = 4
(3𝐸𝐼)
𝐵 + 2
(3𝐸𝐼)
𝐶 +
4 62 = + 14.18 kNm 𝐁 = 1.0909/EI
6 6 12
𝐂 = 𝟎
(3𝐸𝐼) (3𝐸𝐼) 4 62 = - 10.91 kNm
MCB = 2 𝐵 + 4 𝐶 -
6 6 12
In matrix form:
1 0.5 0 -10.5455 10
MAB
MBA 0.5 1 0 -10
1.0909 +
= EI 1/EI
MBC 0 2 1 12
0
MCB 0 1 2 -12
46
Problem Example 8 (cont’d)
Use Matlab
>> t=[-10.5455 1.0909]‘;
>> fem=[10;-10;12;-12];
>> m=k1*t+fem
m =
-0.0000 MAB
-14.1819 MBA
14.1818 MBC
-10.9091 MCB
47
More practice see Problem A3
Thank You
48
Hands 0n Problem 1
Two forces F1=40 and F2=50 pass through OA and OB as
shown. Determine a) the coefficient of the position vectors
OA and OB, b) the coeff. of the unit vectors in OA and OB,
c) the resultant and d) the angle AOB.
Given the coord. of O(1,2,0), A(7,6,5), B(3,9,4).
From
mechanics
rmn = (xm-xn)i + (ym-yn)j + (zm-zn)k position vector
Manual
rmn= (xm−xn)2 + (ym−yn)2 + (zm−zn)2 distance mn calculation
umn = rmn/rmn unit vector roa = (7-1)i + (6-2)j + (5-0)k
roa =
6 4 5
rob =
2 7 4
α = cos-1(-152.2/253.4) = 126.9o
β = cos-1(39.7/253.4) = 81o
= cos-1(-198.6/253.4) = 141.6o 53
Matlab
Hands 0n Problem 2 - cont’d Command
window
ucd =
0.7361 0.4907 -0.4662
>> fcd=90*ucd Force vector F
fcd =
66.2489 44.1660 -41.9577
54
Matlab
Hands 0n Problem 2 - cont’d Command
window
ma =
-152.3726 39.7494 -198.7468
>> mamag=sqrt(sum(ma.^2)) moment magnitude at O
mamag =
253.5699
>> angles=acosd(ma/mamag) direction angles
angles =
126.9351 80.9812 141.6094
55
Problem
Determine the forces in all members of the truss loaded as shown.
Answer
VA = 52.5 kN
VD = 7.5 kN
HD = 30 kN
F1 = -65.6251
F2 = 39.3751
F3 = 80.0002
F4 = -12.0061
F5 = 39.3751
HD
VA VD