Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

MANU2206 Week03 PDF

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

Week 3

Matlab and Simulink II


Autonomous Systems – MANU2206

Dr. Tu Le
School of Engineering
RMIT University, Victoria, Australia
Email: tu.le@rmit.edu.au
Plotting Variables in MATLAB

• The command
>>plot(x,y);
• plots y values versus x values, if x and y are
both vectors or row matrices with the same
lengths.
• Plotting several curves simultaneously:
>>plot(x1,y1,x2,y2,x3,y3);

2
Plotting Variables in MATLAB

>> N=10; h=1/N; x=0:h:1;


>> y=sin(3*pi*x);
>> plot(x,y)

3
Plotting Variables in MATLAB

>> N=100; h=1/N; x=0:h:1;


>> y=sin(3*pi*x);
>> plot(x,y)

>> title('Graph of y = sin(3*pi*x)')


>> xlabel('x-axis')
>> ylabel('y-axis')
4
Linear Regression

• The backslash operator has an important


application: It gives us the parameters of the
regression line in two-dimensions.
• Example: 14

12

x=(0:0.1:5)'; 10

y=2*x+3; 8

n=2*rand(size(x))-1;
6

y=y+n;
4
plot(x,y,'.');
2
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

5
6
Linear Regression

• Example (Continued):
>> X = [x ones(size(x))];
>> A = X\y 14

A = 12

2.0107 10

2.9766 8

>> hold on; 6

>> yhat = A(1)*x + A(2);


4

>> plot(x,yhat,'r'); 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

7
MATLAB Scripts

• A file with extension *.m;


• A script can contain several MATLAB commands.
• When you type the name of a script (with or without
the extension) in MATLAB command window, it is as
if you have typed all the commands there one by
one. MATLAB will execute them in the order they
appear in the script.
• A script can be best created, opened and edited in
MATLAB editor. But it is a text file and can be
opened by any other simple text editor such as
notepad.
8
MATLAB Scripts

Example: suppose that the contents of a script named


mycrop.m are as below.
clear;
A = rand(2,2); B = A^2;
C = (A+B>1.1);

When you type:


>> mycrop
in MATLAB command window, the following happens:
The workspace is cleared, a matrix A variable is created with size 2-by-2,
containing random numbers between 0 and 1, another 2-by-2 matrix is
created which is the square of A, and saved in variable B. A third 2-by-2
matrix is created in variable C, containing 0’s and 1’s. Its elements are 1 if the
corresponding elements in A and B sum to a number greater than 1.1.

9
FOR loops

• General form of a for loop:


for index = values
program statements :
end
• An example: Step by increments of -0.1, and
display the step values:
for s = 1.0: -0.1: 0.0
disp(s);
end

10
FOR loops

• Another example (Fibonnaci Sequence):


The Fibonnaci sequence starts off with numbers 0 and 1,
then each succeeding term is the sum of its two
predecessors.
𝑓1 = 0, 𝑓2 = 1, 𝑓𝑛 = 𝑓𝑛−1 + 𝑓𝑛−2 (𝑛 = 2, 3, … )
𝑓𝑛−1
We want to compute the ratios and show that for
𝑓𝑛
5−1
large 𝑛’s it approaches the golden ratio = 0.6180…
2
We open a new m-file, save it as Fibonnaci.m and fill in
the following commands.

11
The golden rectangle

A golden rectangle is a
rectangle whose side
lengths are in the golden
5−1
ratio:
2

12
The golden ratio

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144…

13
The golden ratio

© Depositphotos.com

14
FOR loops

15
FOR loops

1
Ratio of terms fn-1/fn
0.8

0.6

0.4

0.2

0
0 5 10 15 20
n
16
Logicals in MATLAB

• MATLAB represents true and false by means of the integers 0


and 1.
– TRUE = 1, FALSE = 0.
– If at some point in a calculation a scalar x, say, has been
assigned a value, we may make certain logical tests on it:
• x == 2 is x equal to 2?
• x ~= 2 is x not equal to 2?
• x > 2 is x greater than 2?
• x < 2 is x less than 2?
• x >= 2 is x greater than or equal to 2?
• x <= 2 is x less than or equal to 2?
– Pay particular attention to the fact that the test for equality
involves two equal signs ==.
– = means assigning a value to a variable in memory.
17
Logicals in MATLAB

>> x=pi
x = 3.1416
>> x ~= 3, x ~= pi
ans = 1
ans = 0

18
Logicals in MATLAB

• When x is a vector or a >> x > 1, x >= (-1)

matrix, these tests are ans = 0 1 1


performed element- 0 0 0
wise: ans = 0 1 1
1 1 1
>> x=[-2 pi 5;-1 0 1]
>> y = x >= (-1), x > y
x = -2.0000 3.1416 5.0000
y = 0 1 1
-1.0000 0 1.0000
>> x == 0 1 1 1
ans = 0 0 0 ans = 0 1 1
0 1 0
0 0 0

19
While Loops

• General form of a while loop:


while a logical test,
program statements

end
– Example:
>> S = 1; n = 1;
>> while S+(n+1)^2 < 100,
n = n+1; S = S + n^2;
end
>> [n,S]
ans = 6 91

20
While Loops
• Example: Find the approximate root of the equation 𝑥 =
cos 𝑥 .
• We can do this by making a guess 𝑥1 = 𝜋/4, say, then
computing the sequence of values 𝑥𝑛 = cos 𝑥𝑛−1 for 𝑛 =
2, 3, … and continuing until the difference between two
successive values 𝑥𝑛 − 𝑥𝑛−1 is small enough.
• Contents of the script:
• Run it and you will xold = pi/4; n = 1; d = 1; xnew
= xold;
see MATLAB while d > 0.001 & n < 20,
echoing: xold = xnew;
n = 14 n = n+1; xnew = cos(xold);
d = abs(xnew-xold);
xnew = 0.7388 end
[n xnew d]
21
IF Statements

• General form of an if statement: if a logical test,


program statements

elseif another logical test,
program statements

elseif another logical test,
program statements

else
program statements

end

22
IF Statements

• An example:
n = sum(1:5:1000);
if n>1e5,
m = n;
end
• What is the value of m if n≤1e5?

23
24
IF Statements

• An example (continued):
n = sum(1:5:1000);
if n>1e5,
m = n;
else
m = 0;
end

25
MATLAB Functions

• In this course and for successful completion of


the project, you will need to compose MATLAB
functions in Simulink.
• Similar to math, a MATLAB function has inputs
and outputs.
• Inputs and outputs are MATLAB variables.
• The function reads in the inputs, processes
them, generates the outputs and reports them.

26
MATLAB Functions

• General form:
function [out1, out2, ...] = myfun(in1, in2, ...)
statements

end

• In MATLAB, a function needs to be saved in an


m-file.
• In Simulink, there are blocks in which you type
and save your functions.
• Apart from the above, there is no difference.

27
MATLAB Functions

Example:
The following function inputs three sides of a triangle and outputs its
perimeter and area:
Formulas: For sides a, b, c, the perimeter is S = a+b+c and the area
𝑆
is given by 𝐴 = 𝑝(𝑝 − 𝑎)(𝑝 − 𝑏)(𝑝 − 𝑐) where 𝑝 = .
2

function [S,A] = Per_Area(a,b,c)


% Returns the perimeter and area
% of a triangle with sides a, b
% and c.
% Inputs: Lengths of sides a,b,c
% Outputs: Perimeter S and Area A
% Written by Reza H. 30/07/2012
S = a+b+c;
p=S/2;
A = sqrt(p*(p-a)*(p-b)*(p-c));
28 end
MATLAB Functions

Revisiting the Fibonnaci function f = Fib(n)


sequence: % Returns the nth number in the
% Fibonacci sequence.
Write a function that gets an if n==1
input n and returns the n-th f = 0;
elseif n==2
number in the Fibonnaci f = 1;
sequence denoted by fn : else
f1 = 0; f2 = 1;
f1 = 0 ; f2 = 1 ; for i = 2:n-1
f = f1 + f2;
f1=f2; f2 = f;
for n >2 end
end
fn = fn-1 + fn-2 end

29
Simulink

What is Simulink good for?


• Modeling/designing dynamic
systems (including nonlinear
dynamics)
• Modeling/designing control
systems (including nonlinear
controllers and plants)
• Signal processing design/simulation

30
Simulink

31
Simulink interface

There are two important parts of the Simulink interface:


The Model Window: contains your model/block diagram.
The Library Browser: A library of all possible blocks you can drag-and-drop
into your model.

32
Simulink

33
Simulink

34
Simulink

35
Simulink

36
Simulink

37
Simulink

38
Simulink

39
Simulink

40
Simulink

41
Simulink

42
Simulink

43
Simulink

44
Simulink

45
Simulink

46
Simulink

47
Simulink

48
Simulink

49
Simulink

50
Simulink

51
Simulink

52

You might also like