Simulink
Simulink
ECE743
Jin-Woo Jung,
Ph. D. Student and GTA
1 /36 ECE743
Features of Matlab and Simulink
¾ Matlab (*.m):
Only text code (Not easy to model complicated systems)
Easy to edit figures
¾ Simulink (*.mdl):
Schematic (Easy to model complicated systems)
Not easy to change parameters
Can not edit figures
4 /36 ECE743
2. Starting “Simulink”
5 /36 ECE743
3. Open “A new file”
6 /36 ECE743
4. Building “System” (1)
Find ‘’Block‘’ when you know “block’s name”
1). Type block’s name and then drag it to a new file
“Press a right button on a mouse” “Double click your model”
8 /36 ECE743
5. Set up “Model properties”
Set up m files for parameter initialization and plot (later)
1). Click “Model properties” Then, type file names: Initialization.m and Plot.m
9 /36 ECE743
6. Start “Simulation” (1)
Set up “Simulation parameters”
1). Click “Simulation parameters” Then, change “Stop time”
10/36 ECE743
6. Start “Simulation” (2)
Start Simulation
1). Click “Start simulation”
11/36 ECE743
7. Example for Matlab/Simulink
Example 1:
12/36 ECE743
7. Example 1
Four different Methods
Case 1: Only Matlab
Case 2: Matlab + Simulink: S-Function
1. S-function: “asglpr3b.m”
2. Simulink: “Example_1.mdl”
3. Plot: Plot_1.m
Case 3: Matlab + Simulink: Not S-Function
1. Parameter initialization: “Initialization.m”
2. Simulink: “Case_3.mdl”
3. Plot: Plot_1.m
Case 4: Matlab + Simulink: “SimPowerSystems”
1. Parameter initialization: “Para_Initial.m”
2. Simulink: “Case_4.mdl”
3. Plot: Plot_1.m
clear all
Lp = 0.1;
Ls = 0.2;
Mi = 0.1;
Rp = 1;
Rs = 2;
R1 = 1;
C = 1e-6;
V = 10;
alpha = 0.1;
A = Linv*R;
B = Linv*D;
X = [0;0;0];
U = V;
14/36 ECE743
7. Example 1 − Case 1 – (2)
Case 1: Only Matlab – Matlab code continued - (2)
T = 0.0001; % time step
for n = 1:10000
% Trapezoidal Integration
n1(n) = n;
Xest = X + T*(A*X + B*U);
Xdotest = A*Xest + B*U;
alpha1 = 1 + alpha;
alpha2 = 1 - alpha;
term1 = alpha1*Xdotest;
termint = A*X + B*U;
term2 = alpha2 + termint;
X = X + (T/2)*(term1 + term2);
i1(n) = X(1);
i2(n) = X(2);
Vc(n) = X(3);
end
15/36 ECE743
7. Example 1 − Case 1 – (3)
Case 1: Only Matlab – Matlab code continued - (3)
figure (1)
subplot(3,1,1)
plot(n1*T,i1)
grid
ylabel('i_1 [A]')
title('i_1 vs time')
subplot(3,1,2)
plot(n1*T,i2)
grid
axis([0 1 -0.01 0.01])
ylabel('i_2 [A]')
title('i_2 vs time')
subplot(3,1,3)
plot(n1*T,Vc)
grid
axis([0 1 -5 10])
xlabel('Time')
ylabel('V_c [V]')
title('V_c vs time')
16/36 ECE743
7. Example 1 − Case 1 – (4)
Results
i1 vs time
10
i1 [A] 5
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
i2 vs time
0.01
0.005
i2 [A]
-0.005
-0.01
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Vc vs time
10
5
Vc [V]
-5
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Time
17/36 ECE743
7. Example 1 − Case 2 – (1)
Case 2: Matlab + Simulink: S-function
S-function code: “asglpr3b.m” --- (1)
Lp = 0.1;
Ls = 0.2;
Mi = 0.1;
Rp = 1;
Rs = 2;
Rl = 1;
C = 1e-6;
V = 10;
alpha = 0.1;
R = [-Rp 0 0; 0 -(Rs+Rl) -1; 0 1 0]
D = [1;0;0]
L = [(Lp+Mi) -Mi 0; -Mi (Ls+Mi) 0; 0 0 C]
Linv = inv(L);
A = Linv*R;
B = Linv*D;
18/36 ECE743
7. Example 1 − Case 2 – (2)
S-function code: “asglpr3b.m” --- Matlab code continued - (2)
if abs(flag)==1
sys(1:3)=A*x(1:3)+B*u;
elseif abs(flag)==3
sys(1:3)= x(1:3);
elseif flag==0
sys(1)=3;
sys(2)=0;
sys(3)=3;
sys(4)=1;
sys(5)=0;
sys(6)=0;
19/36 ECE743
7. Example 1 − Case 2 – (3)
Simulink code: “Example_1.mdl” --- (1)
20/36 ECE743
7. Example 1 − Case 2 – (4)
Simulink code: “Example_1.mdl” --- (2)
1. “To Workspace”
21/36 ECE743
7. Example 1 − Case 2 – (5)
Simulink code: “Example_1.mdl” --- (3)
2. “S-Function”
“Blank”
22/36 ECE743
7. Example 1 − Case 2 – (6)
Simulink code: “Example_1.mdl” --- (4)
3. “Scope”
“Type the number of axes”
“Click Parameters”
23/36 ECE743
7. Example 1 − Case 2 – (7)
Plot Matlab code: “Plot_1.m” --- (Method1 for plot)
24/36 ECE743
7. Example 1 − Case 2 – (8)
Plot Matlab code: “Plot_1.m” --- (Method 2 for plot)
25/36 ECE743
7. Example 1 − Case 2 – (9)
Results
i1 vs time
10
i1 [A]
5
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
i2 vs time
0.01
0.005
i2 [A]
-0.005
-0.01
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Vc vs time
10
5
Vc [V]
-5
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Time
26/36 ECE743
7. Example 1 − Case 3 – (1)
Case 3: Matlab + Simulink: Not S-Function
Parameter Initialization: “Initialization.m” - (1)
% Parameters Initialization - Example 1 - Case 3
clear all
Lp = 0.1;
Ls = 0.2;
Mi = 0.1;
Rp = 1;
Rs = 2;
R1 = 1;
C = 1e-6;
V = 10;
alpha = 0.1;
A = Linv*R;
B = Linv*D;
C = eye(3);
D = zeros(3,1);
27/36 ECE743
7. Example 1 − Case 3 – (2)
Simulink code: “Case_3.mdl” --- (1)
28/36 ECE743
7. Example 1 − Case 3 – (3)
Simulink code: “Case_3.mdl” --- (2)
1. “State-space”
29/36 ECE743
7. Example 1 − Case 3 – (4)
Set up m files for parameter initialization and plot
1). Click “Model properties” Then, type file names: Initialization.m and Plot_1.m
30/36 ECE743
7. Example 1 − Case 3 – (5)
Results
i1 vs time
10
i1 5
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
i2 vs time
0.01
0.005
0
i2
-0.005
-0.01
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Vc vs time
10
5
Vc
-5
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
31/36 ECE743
7. Example 1 − Case 4 – (1)
Case 4: Matlab + Simulink (SimPower Systems)
Parameter Initialization: “Para_Initial.m”
clear all
V = 10;
Lp = 0.1;
Ls = 0.2;
Mi = 0.1;
Rp = 1;
Rs = 2;
R1 = 1;
C = 1e-6;
32/36 ECE743
7. Example 1 − Case 4 – (2)
Simulink code: “Case_4.mdl”
33/36 ECE743
7. Example 1 − Case 4 – (3)
Set up m files for parameter initialization and plot
1). Click “Model properties” Then, type file names: Para_Initial.m and Plot_1.m
34/36 ECE743
7. Example 1 − Case 4 – (4)
Set up “Simulation parameters”
1). Click “Simulation parameters” Then, change “Stop time” and “Solver options”
5
i1
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
i2 vs time
0.01
0.005
0
i2
-0.005
-0.01
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Vc vs time
10
5
Vc
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
36/36 ECE743