Sei sulla pagina 1di 56

Topic 3

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.

• Let us 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.

>> va=[3 8 6];vb=[6 3 5]; Both are vectors


>> vc=va-vb
vc =
-3 5 1

>> vd=10+vc Scalar & vector


vd =
7 15 11

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

>> a=[ 3 6 8;5 2 4;2 7 5]; 3 x 3 matrix


>> b=[4 3 6 3;1 6 3 9;4 7 9 8]; 3 x 4 matrix
>> f=a*b
f =
50 101 108 127
38 55 72 65
35 83 78 109 3 x 4 matrix

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

In Matlab it can be done by;


i) inverse function X=A-1*B ,or Uses inverse

ii) Left division X=A\B Uses Gauss elimination

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.

In Matlab it can be done by


right division X=D/C

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

AX=B form XC=D form

10
Division Example - cont’d AX=B form

X = A\B OR X=A-1B

>> a=[3 6 2;4 -3 5;2 7 2];


>> v2=inv(a)*b
>> b=[0;9;4];
v2 =
>> v1=a\b
-3.7674
v1 = 0.2326
-3.7674 x 4.9535
0.2326 y
4.9535 z

>> v3=a^-1*b ?
11
Division Example – cont’d XC=D form

>> c=[3 4 2;6 -3 7;2 5 2];


X = D/C
>> d=[0 9 4];
>> v4=d/c
v4 =
-3.7674 0.2326 4.9535
x y z
OR

X=DC-1 >> v5=d*inv(c)


v5 =
-3.7674 0.2326 4.9535

>> 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.

In other situations element-by-element operations are


required. i.e. operations are carried out on each
element of an array(s).
Element-by-element array can be done only with
arrays of the same size.

(By definition addition and subtraction are already


element-by-element operation)
15
Element-by-Element Operations - cont’d

Element-by-element multiplication, division and


exponentiation of 2 arrays is entered in Matlab by
typing a period in front of the arithmetic operator.

.* .\ ./ .^
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’?

>> B./A Element-by-element


ans = division
4.0000 0.5556 0.8571
0.6250 1.1667 3.0000
>> A3=A.^3 Element-by-element
A3 = exponentiation
8 729 343
512 216 27 >> A^317 ?
Element-by-Element Operations - Example
Determine y for the expression, y = x2 – 2x + 5 when
x = 1, 2, 3, …..8.
>> x=[1:8];
>> y=x.^2-2.*x+5
y =
4 5 8 13 20 29 40 53
>> plot(x,y) 60

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)

2. Two vectors are given:


u = -2i +8j -3k and v = 7i -5j – 4k
Calculate the dot product of the vectors in three ways:
(a) Write an expression using element-by-element calculation and
the MATLAB built-in function sum.
(b) Define u as a row vector and v as a column vector, and then
use matrix multiplication.
(c) Use the MATLAB built-in function dot.
𝑥 2 −3
3. For the function y = 𝑥+4
, calculate the value of y for the following
values of x using element-by-element operation: -2, -1, 0, 1, 2, 3.
19
Arrays in Built-in Math Functions
When an array is passed as the argument of a function,
it is executed on each element of the array. Like
element-by-element operation.

>> 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

>> m=[1 5 8;45 65 130]


m =
1 5 8
45 65 130

>> 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

0.5 plot the function


again using x
0
interval 0.1
-0.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

Plot the BMD. 26


27
Problem Example 3
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. m=F/mg
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=[2 4 7 10 15 25]; Input


>> F=[12.9 24.1 43.1 61.1 90 152]; vectors
>> nu=F./(m.*9.81) Element by element
nu =
0.6575 0.6142 0.6276 0.6228 0.6116 0.6198
>> avenu = mean(nu) mean – array function
avenu =
0.6256
28
29
Problem Example 4 (a) - scalar analysis
Determine the resultant
of the forces acting on
the bracket shown.

>> 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.

>> f=[450 200 600]'; force vector


>> a=[50 0 285]'; angle vector
>> fx=f.*cosd(a); fx components
>> fy=f.*sind(a); fy components
>> fx=sum(fx);fy=sum(fy); Sum of fx & fy
>> fr=sqrt(fx^2+fy^2) Resultant force
fr =
685.9935
>> th=atand(fy/fx) Resultant force angle
th =
-20.0188

31
Problem Example 4 (c) - vector analysis
Determine the resultant of the forces acting on the bracket shown.

F = Fxi + Fyj = F(cosθi + sin θ j)

F = S(Fx2 + Fy2)

tan θ = Fy/ Fx

>> f1=450*[cosd(50) sind(50)]; F1 force vector

>> f2=200*[cosd(0) sind(0)]; F2 force vector

>> f3=600*[cosd(-75) sind(-75)]; F3 force vector

>> ft=f1+f2+f3; Resultant vector

>> fr=sqrt(ft(1)^2 + ft(2)^2); Resultant force magnitude

>> th=atand(ft(2)/ft(1)); Resultant force angle


32
Problem Example 4(c) - cont’d
>> f1=450*[cosd(50) sind(50)]; f1 =
>> f2=200*[cosd(0) sind(0)]; 289.2544 344.7200

>> f3=600*[cosd(-75) sind(-75)]; f2 =


>> ft=f1+f2+f3; 200 0

>> fr=sqrt(ft(1)^2 + ft(2)^2); f3 =


155.2914 -579.5555
>> th=atand(ft(2)/ft(1));
ft =
atand returns a degree 644.5459 -234.8355
angle between -90 and 90
fr =
For angle between
685.9935
0 and 360 degree use
if statement
th =
-20.0188
33
34
Problem Example 5
Determine the location of the centroid for the section shown
below. Use array input as shown below.

>> D=[1.5 1 6;1.5 2.5 2]; 2 x 3 array


>> xs=D(:,1); Extracting x coords
>> ys=D(:,2); Extracting y coords
>> As=D(:,3); Extracting areas
>> a=sum(As);
>> xbar=sum(As.*xs)/a
xbar =
1.5000
>> ybar=sum(As.*ys)/a
Input array ybar =
1.3750
x y area
1.5 1.0 6.0 bottom rect.
1.5 2.5 2.0
top rect. 35
36
Problem Example 6(a) - scalar
Determine the length of the sides of the polygon shown.

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]

S1= S[(x1-x2)2 + (y1-y2)2]


S2= S[(x2-x3)2 + (y2-y3)2]
S3= S[(x3-x4)2 + (y3-y4)2]
S4= S[(x4-x1)2 + (y4-y1)2]
vectors s xa xb ya yb

>> xa=[0 100 90 10]; Create vector xa


>> xb=[xa(2:end) xa(1)]; Create vector xb
>> ya=[20 25 60 40]; Create vector ya
>> yb=[ya(2:end) ya(1)]; Create vector yb
>> s=sqrt((xa-xb).^2+(ya-yb).^2) Array operation to get s vector
s =
38
100.1249 36.4005 82.4621 22.3607
39
Problem Example 7
Determine support reactions and all member forces of the truss
loaded as shown.

1) Write joint equilibrium equation


for each joint.
2) Put the equations in matrix form.
3) Solve the simultaneous equations
using AX=B.

40
Problem Example 7 (cont’d)
Joint A: F1 + F3 + F4 cos30 = 0 ∑Fx
F2 + F4 sin30 = 0 ∑Fy

Joint B: -F3 - F6 cos 50 = 0 ∑Fx

F5 + F6 sin 50 = 0 ∑Fy

Joint C: - F4 cos30 + F6 cos50 + 10 = 0 ∑Fx


- F4 sin30 - F6 sin50 – 25 = 0 ∑Fx
Putting the eqns. in matrix form
F1 0
1 0 1 0.8660 0 0
F2 0
0 1 0 0.5000 0 0
0
0
0
0
0 -1 0
0 0 0
0 0 -0.8660
0 0 -0.5000
K 0
1
0
0
-0.6428
0.7660
0.6428
-0.7660
F P
F3
F4
F5
F6
0
0
-10
25

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.

(2𝐸𝐼) (2𝐸𝐼) (10)(8)


MAB = 4 𝐴 +2 𝐵 + =0
8 8 8
(2𝐸𝐼) (2𝐸𝐼) (10)(8)
MBA = 2 𝐴 +4 𝐵 - = - 14.18 kNm A = −10.5455/EI
8 8 8

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];

>> k1=[1 0.5;0.5 1;0 2;0 1]


k1 =
1.0000 0.5000
0.5000 1.0000
0 2.0000
0 1.0000

>> 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

Fmn=Fumn force vector roa= (6)2 + (4)2 + (5)2 = 8.775


Fr = ∑F resultant vector uoa = 0.6838i + 0.4558j + 0.5698k
θ = cos-1 (uoa.uob/uoauob) angle Foa =Fa( 0.6838i + 0.4558j + 0.5698k)
between
49
vectors
Matlab Command
Hands 0n Problem 1– cont’d window

>> o=[1 2 0];a=[7 6 5];b=[3 9 4]; Define points

>> roa=a-o position vector OA

roa =
6 4 5

>> rob=b-o position vector OB

rob =
2 7 4

>> oa=sqrt(sum(roa.^2)) magnitude OA


oa =
8.7750

>> uoa=roa./oa unit vector OA


uoa =
0.6838 0.4558 0.5698 50
Hands 0n Problem 1 – cont’d
>> ob=sqrt(sum(rob.^2)) magnitude OB
ob =
8.3066
>> uob=rob./ob unit vector OB
uob =
0.2408 0.8427 0.4815
>> FRv=40*uoa+50*uob Resultant vector
A better
FRv = way to
39.3891 60.3687 46.8693 solve
this
>> FRm=sqrt(sum(FR.^2)) Resultant magnitude problem
FRm = is by
writing a
85.9803 Angle AOB program
(script)
>> AngAOB=acosd(dot(roa,rob)/(oa*ob))
AngAOB =
34.5982
51
52
Hands 0n Problem 2
Moment – 3D (Vector Formulation) rC
5. A force F=90N directed from C to D.
Determine the magnitude of the moment
created about the support at point A, and their rD
coordinate direction angles.
Solution From mechanics
rD= 3j + 0.6k
MA = rD X F or MA = r C X F
i j k
MA = r D X F = 0 3 0.6 F = FuF = 90[ 3i + 2j – 1.9k ]
4.08
66.2 44.1 -41.9 = 66.2i + 44.1j – 41.9k

= [(3)(-41.9) - (0.6)(44.1)]i - [-(0.6)(66.2)]j +[-(3)(66.2)]k


= -152.2i + 39.7j – 198.6k

MA = √ (-152.2)2 + (39.7)2 + (-198.6)2


= 253.4Nm

α = 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

>> rd=[0 3 0.6]; position vector od

>> rcd=[3 2 -1.9]; position vector cd

>> ucd=rcd/sqrt(sum(rcd.^2)) unit vector cd

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=cross(rd,fcd) moment vector M at O

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

1) Write equilibrium equation for the C C


joints [∑Fx = 0, ∑Fy = 0].
2) Put the equations in matrix form.
HD
3) Solve the simultaneous equations VA VD
using AX=B.

Potrebbero piacerti anche