You are on page 1of 36

Week 2

Matlab and Simulink I


Autonomous Systems – MANU2206

Dr. Tu Le
School of Engineering
RMIT University, Victoria, Australia
Email: tu.le@rmit.edu.au
Scalar Product of Two Vectors

>> u=[10 -11 12]; v=[20; -21; -22];


>> P=u*v
P = 167

2
Scalar Product of Two Vectors

>> u=[10 -11 12]; v=[20;-21;-22];


>> w=[2 1 3]; z=[7;6;5];
• We wish to form the scalar products of u with w
and v with z.
>> u*w
??? Error using ==> mtimes
Inner matrix dimensions must agree.

3
Scalar Product of Two Vectors

>> u=[10 -11 12]; v=[20;-21;-22];


>> w=[2 1 3]; z=[7;6;5];
>> u*w‘
ans = 45
>> v'*z
ans = -96
Question:
What does u*u‘ give us?

4
5
Dot Product and Dot Division

• .* : multiplying element-by-element
• ./ : dividing element-by-element
Both dot product and dot division only apply
when two same-length vectors or two same-size
matrices are involved.
>> w=[2 1 3]; z=[7;6;5];
>> w.*w
ans = 4 1 9
>> w.*z'
ans = 14 6 15
6
Dot Product and Dot Division

>> a = 1:5; b=6:10; a./b


ans = 0.1667 0.2857 0.3750 0.4444 0.5000
>> a./a
ans = 1 1 1 1 1
>> c = -2:2, a./c
c = -2 -1 0 1 2
Warning: Divide by zero.
ans = -0.5000 -2.0000 Inf 4.0000 2.5000

7
Dot Product and Dot Division

>> a = 1:5; b=6:10;


>> d = a.*b - 24, d./c
d = -18 -10 0 12 26
Warning: Divide by zero.
ans = 9 10 NaN 12 13

8
What is echoed by Matlab command line?

9
Dot Power

• .^ : element-by-element power computation


>> u
u = 10 -11 12
>> u.^2
ans = 100 121 144
>> u.*u
ans = 100 121 144
>> u.^4
ans = 10000 14641 20736
10
Dot Power

>> v >> v.^2


v = 20 ans = 400
-21
441
-22
484

Remember from past:


w = [2 1 3] and u = [10 -11 12]
>> u.*w.^(-2)
ans = 2.5000 -11.0000 1.3333
11
Dot Power

>> v >> v.^2


v = 20 ans = 400
-21
441
-22
484

Remember from past:


w = [2 1 3] and u = [10 -11 12]
>> u.*w.^(-2)
ans = 2.5000 -11.0000 1.3333
12
Deleting Rows and Columns

• To remove the second column of X:


>> X(:,2) = [];
• This will result in an error. Why?
X(1,2) = [];

13
Deleting Rows and Columns

• To remove the second column of X:


>> X(:,2) = [];
• This will result in an error. Why?
X(1,2) = [];

14
Operations involving scalars

16
Priorities

• MATLAB works according to the priorities:


1. quantities in brackets
2. powers
3. * / working left to right
4. + - working left to right

17
Variable Names

• Legal names consist of any combination of


letters and digits, starting with a letter.
• Allowed: NetCost, Left2Pay, x3, X3, z25c5
• Not allowed: Net-Cost, 2pay, %x, @sign
• Avoid these names:
– eps = 2.2204e-16
– pi = 3.14159... = π

18
Suppressing Output

• One often does not want to see the result of


intermediate calculations.
• This can be accomplished by terminating the
MATLAB statement with a semi-colon (;).

>> x=-13; y=5+x, z=x^2+y


y = -8
z = 161

19
Mathematical Built-In Functions

• sin, cos, tan, asin, acos, atan, atan2


• sinh, cosh, tanh, asinh, acosh, atanh,
• sqrt, exp, log, log10, abs, sign,
• conj, imag, real, angle
• round, floor, fix, ceil, rem

20
Mathematical Built-In Functions

• sin, cos, tan, asin, acos, atan, atan2


• sinh, cosh, tanh, asinh, acosh, atanh,
• sqrt, exp, log, log10, abs, sign,
• conj, imag, real, angle
• round, floor, fix, ceil, rem

21
Logical Subscripts

– >> A = rand(50,1);
– >> I = (A>0.5);
– >> B = A(I);
– >> C= A(A>0.5);
• A is created as a vector containing 50 random numbers
between 0 and 1.
• I will contain a 50-by-1 vector of zeros or ones. Each element
is one the corresponding element in A is greater than 0.5,
otherwise it is zero.
• B will contain those elements of A that correspond to ones in
I, i.e. the elements that are greater than 0.5.
• C will be exactly the same as B. It is just created directly
from A without separately building the indices vector I.
22
Logical Subscripts

– >> A = rand(50,1);
– >> I = (A>0.5);
– >> B = A(I);
– >> C= A(A>0.5);
• What is the size of C?
• You can use the “size”
command.
– >> [m,n] = size(C);
• This will create two
variables in the
workspace, m and n,
where the number of
rows and columns in C
23 will be saved.
Logical Subscripts

24
Logical Subscripts

25
Common Data Types in MATLAB

• double: Double-precision floating-point number


• float: Floating-point number
• char: An ASCII character
• int: integer
• uint: unsigned integer
• uint16: 16-bit unsgined integer
• uint8: 8-bit unsigned integer

26
Some Examples to Recap

>> v = [1 3, sqrt(5)]
v = 1.0000 3.0000 2.2361
>> length(v)
ans = 3
>> v2 = [3+ 4 5]
v2 = 7 5
>> v3 = [3 +4 5]
v3 = 3 4 5
>> v+v3
ans = 4.0000 7.0000 7.2361
>> v4 = 3*v
v4 = 3.0000 9.0000 6.7082
>> v5 = 2*v -3*v3
v5 = 7.0000 -6.0000 -10.5279
>> v+v2
??? Error using ==> plus
27
Matrix dimensions must agree.
Some Examples to Recap

>> w = [1 2 3], z = [8 9]
w = 1 2 3
z = 8 9
>> cd=[2*z,-w], sort(cd)
cd = 16 18 -1 -2 -3
ans = -3 -2 -1 16 18
>> w(2) = -2, w(3)
w = 1 -2 3
ans = 3

28
Some Examples to Recap

>> 1:4
ans = 1 2 3 4
>> 3:7
ans = 3 4 5 6 7
>> 1:-1
ans = Empty matrix: 1-by-0
>> 0.32:0.1:0.6
ans = 0.3200 0.4200 0.5200
>> -1.4:-0.3:-2
ans = -1.4000 -1.7000 -2.0000
>> r5 = [1:2:6,-1:-2:-7]
r5 = 1 3 5 -1 -3 -5 -7
>> r5(3:6)
ans = 5 -1 -3 -5
>> r5(1:2:7)
ans = 1 5 -3 -7
>> r5(6:-2:1)
29
ans = -5 -1 3
Some Examples to Recap

>> c = [1; 3; sqrt(5)]


c = 1.0000
3.0000
2.2361
>> c2 = [3
4
5]
c2 = 3
4
5
>> c3 = 2*c-3*c2
c3 = -7.0000
-6.0000
-10.5279

30
Some Examples to Recap

>> w = [1 -2 3]
w = 1 -2 3
>> w'
ans = 1
-2
3
>> c=[1; 2; 3]
c = 1
2
3
>> c'
ans =1 2 3

31
Some Examples to Recap

>> A = ones(2,3)
>> c=[0 1;3 -2;4 2], x=[8;-4;1]
A = 1 1 1
c = 0 1
1 1 1
3 -2
>> B = zeros(1,3)
4 2
B = 0 0 0
x = 8
>> C = ones(size(B))
-4
C = 1 1 1
1
>> D = eye(3)
>> g=[c x]
D = 1 0 0
g = 0 1 8
0 1 0
3 -2 -4
0 0 1
4 2 1
>> d=[-3 4 2]
d = -3 4 2
>> D=diag(d)
D = -3 0 0
0 4 0
32
0 0 2
Some Examples to Recap

>> a=[5 7 9;1 -3 -7]


a = 5 7 9
1 -3 -7
>> b=[-1 2 5;9 0 5]
b = -1 2 5
9 0 5
>> h = [a;b]
h = 5 7 9
1 -3 -7
-1 2 5
9 0 5
>> j=[1:4;5:8;20 0 5 4]
j =
1 2 3 4
5 6 7 8
20 0 5 4
33
Some Examples to Recap

>> j=[1:4;5:8;9:12;20 0 5 4]
j =
1 2 3 4
5 6 7 8
9 10 11 12
20 0 5 4
>> k=[diag(1:4) j;j' zeros(4,4)]
k =
1 0 0 0 1 2 3 4
0 2 0 0 5 6 7 8
0 0 3 0 9 10 11 12
0 0 0 4 20 0 5 4
1 5 9 20 0 0 0 0
2 6 10 0 0 0 0 0
3 7 11 5 0 0 0 0
4 8 12 4 0 0 0 0
34
Some Examples to Recap

>> x=0:0.1:0.5;y=4*sin(3*x); u=3*sin(4*x);


>> [x' y' u']
ans =
0 0 0
0.1000 1.1821 1.1683
0.2000 2.2586 2.1521
0.3000 3.1333 2.7961
0.4000 3.7282 2.9987
0.5000 3.9900 2.7279
>> x=(0:0.1:0.5)'; [x 4*sin(3*x) 3*sin(4*x)]
ans =
0 0 0
0.1000 1.1821 1.1683
0.2000 2.2586 2.1521
0.3000 3.1333 2.7961
0.4000 3.7282 2.9987
35 0.5000 3.9900 2.7279
Some Examples to Recap

>> j=[1:4;5:8;9:12;20 0 5 4]
j =
1 2 3 4
5 6 7 8
9 10 11 12
20 0 5 4
>> j(4,1)=j(1,1)+6
j =
1 2 3 4
5 6 7 8
9 10 11 12
7 0 5 4
>> j(1,1)=j(1,1)-3*j(1,2)
j =
-5 2 3 4
5 6 7 8
9 10 11 12
36
7 0 5 4

You might also like