Mathematical Operations

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Topic 3

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.

>> 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
>> 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*b b is the


ans = inverse of a
1.0000 0.0000 -0.0000
-0.0000 1.0000 0
-0.0000 0.0000 1.0000

>> a*a^-1
ans =

1.0000 0.0000 -0.0000 Both give the


-0.0000 1.0000 0 same result
-0.0000 0.0000 1.0000

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

In Matlab it can be done by;


i) inverse function X=A-1B ,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
9
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

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

AX=B form XC=D form

11
Division Example - cont’d AX=B form

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

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


>> Xb=inv(A)*B
>> B=[0;9;4];
Xb =
>> X=A\B
12.4800
X =
-0.5600
12.4800
-8.5200
-0.5600
-8.5200

>> Xc=A^-1*B ?
12
Division Example – cont’d XC=D form

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


X = D/C
>> D=[0 9 4];
>> Xd=D/C
Xd =
12.4800 -0.5600 -8.5200
OR

X=DC-1 >> Xe=D*inv(C)


Xe =
12.4800 -0.5600 -8.5200

>> Xf=D*C^-1 ?13


Array Operations
(element-by-element operations)

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.

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
exponentiation
A3 =
8 729 343 >> A^3 ?
512 216 27 17
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
60

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

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.

3. Determine value of y for the expression, y =


when x = 1 , 3, 5, ……..9.
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:pi/5:pi]
x =
0 0.6283 1.2566 1.8850 2.5133 3.1416
>> y=sin(x) Radian angle
y =
0 0.5878 0.9511 0.9511 0.5878 0.0000

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

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

>> m=[2 4 7 10 15 25];


>> F=[12.9 24.1 43.1 61.1 90 152];
>> mu=F./(m.*9.81)
mu =
0.6575 0.6142 0.6276 0.6228 0.6116 0.6198
>> aveMu=mean(mu)
aveMu =
0.6256 24
Problem Example 3
Determine the resultant of the forces acting on the bracket shown
below.

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


F = S(Fx2 + Fy2)
tan θ = Fy/ Fx

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


>> f2=200*[cosd(0) sind(0)];
>> f3=600*[cosd(-75) sind(-75)];
>> ft=f1+f2+f3;
>> fr=sqrt(ft(1)^2 + ft(2)^2);
>> th=atand(ft(2)/ft(1));
25
Problem Example 3 - cont’d
f1 =
289.2544 344.7200
>> f1=450*[cosd(50) sind(50)];
>> f2=200*[cosd(0) sind(0)]; f2 =
>> f3=600*[cosd(-75) sind(-75)]; 200 0
>> ft=f1+f2+f3;
>> fr=sqrt(ft(1)^2 + ft(2)^2); f3 =
>> th=atand(ft(2)/ft(1)); 155.2914 -579.5555

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.

Smn = S[(Xm2 - Xn2) + (Ym2 - Yn2)]

>> xa=[0 100 70 30];


>> xb=[xa(2:end) xa(1)];
>> ya=[0 0 40 40];
>> yb=[ya(2:end) ya(1)];
>> s=sqrt((xa-xb).^2+(ya-yb).^2)
s =
100 50 40 50

27
Problem Example 5
For the function y = , calculate the value of y

for the following values of x using element-by-element


operation: -2, -1, 0, 1, 2, 3.

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

>> a=[3 2;4 3];


>> b=[2;4];
3 2 x >> xy=inv(a)*b
4 3 = 2
y 4 xy =
-2
4

29
More practice see Problem A3

Thank You

30

You might also like