Document

advertisement
Simulink
Create a model in Simulink
Run simulation from Simulink
Invoke simulation from a MATLAB script
Create a subsystem
Create a testbed for testing subsystem
1
Differential Equation
𝑑2𝑦
𝑑𝑦
𝑎 2 + 2𝑎
+𝑦 =𝑥
𝑑𝑥
𝑑𝑥
Find unit-step response in two cases:
𝑎=1
𝑎 = 10
2
Simulation
𝑑2 𝑦 1
𝑑𝑦
= 𝑥 − 𝑦 − 2𝑎
2
𝑑𝑥
𝑎
𝑑𝑥
𝑥
+
−
Σ
+
Σ
1 𝑎
𝑑2 𝑦
𝑑𝑥 2
(∙) 𝑑𝑥
𝑑𝑦
𝑑𝑥
𝑦
(∙)𝑑𝑥
−
2𝑎
3
File > New > Model
or
4
Drag Blocks from Sublibraries into Model
File > Save as … example.slx
5
Flipping a Block: Right-Click the Block, Select “Rotate & Flip”
6
Connect Two Blocks: Click Output, Drag Arrow to Input
(Click 1st Block, Control-Click 2nd Block)
7
Tap into One Line, Creating a Branch
Position cursor on the line, control-click and drag new arrow
8
Renaming Blocks
1. Highlight name of block
2. Type new name (erasing old name)
3. Esc key (not Enter)
9
Change Parameters in a Block
double-click the block
10
Blocks for This Simulation
Sublibrary
Block
Sources
Step
Math Operations
Sum (2)
Math Operations
Gain (2)
Continuous
Integrator (2)
Sinks
Scope
Sinks
To Workspace
Parameters
Step time: 0
Initial value: 0
Final value: 1
List of signs: | + −
Gain: 1.0
(1st Gain)
Gain: 1.414 (2nd Gain)
Variable name: y
Saved as: Array
11
example.slx
12
Simulation > Model Configuration Parameters
(from the example.slx window)
13
Model Configuration Parameters
Pane
Parameters
Solver
Start time: 0.0
Max step size: 0.1
Data Import/Export Time: t
Output: y
Stop time: 20
Format: Array
14
Run
then double-click Scope
15
Click Autoscale
16
Create MATLAB script runexample.m and run it from MATLAB
a = [1 10];
curv = {'-b','--r'};
figure(1)
for n = 1:2
C = 1/a(n);
K = sqrt(2*a(n));
set_param('example/C','Gain',num2str(C))
set_param('example/K','Gain',num2str(K))
sim('example')
plot(t,y,curv{n})
hold on
end
axis([0 20 0 1.2])
set(gca,'YTick',0:0.2:1.2)
set(gca,'FontSize',20)
xlabel(‘t')
ylabel('y')
title('Step Response')
legend('a = 1','a = 10','Location','SouthEast')
set(findobj(gcf,'LineWidth',0.5),'LineWidth',2)
saveas(1,'Step Response','png')
17
18
Prepare New Model with Generic Input/Output
Remove these blocks: Step, Scope, To Workspace.
Add In1 (Sources Sublibrary) and Out1 (Sinks Sublibrary).
Rename In1 and Out1.
File > Save as … diffEq.slx
19
Create Subsystem
Edit > Select All
Diagram > Subsystem & Model Reference > Create Subsystem from Selection
20
Create Testbed for Testing Subsystem
Rename subsystem block.
Add Step on input. Step time: 0, Start value: 0, Final value: 1
Add Scope to output.
Add To Workspace. Variable name: y, Save format: Array
Save as … test.slx
21
Simulation > Model Configuration Parameters
(from the test.slx window)
22
Model Configuration Parameters
Pane
Parameters
Solver
Start time: 0.0
Max step size: 0.1
Data Import/Export Time: t
Output: y
Stop time: 20
Format: Array
23
Create MATLAB Script runtest.m and run it from MATLAB
a = [1 10];
curv = {'-b','--r'};
figure(1)
for n = 1:2
C = 1/a(n);
K = sqrt(2*a(n));
set_param(‘test/diffEq/C','Gain',num2str(C))
set_param(‘test/diffEq/K','Gain',num2str(K))
sim(‘test')
plot(t,y,curv{n})
hold on
end
axis([0 20 0 1.2])
set(gca,'YTick',0:0.2:1.2)
set(gca,'FontSize',20)
xlabel(‘t')
ylabel('y')
title('Step Response')
legend('a = 1','a = 10','Location','SouthEast')
set(findobj(gcf,'LineWidth',0.5),'LineWidth',2)
saveas(1,'Step Response','png')
24
Exercise
Simulate the following differential equation
using a step input.
𝑑𝑦
+ 2𝑦 = 4𝑥
𝑑𝑥
25
Download