Skip to content

Commit

Permalink
Added new plot/control commands suggested by E Boje
Browse files Browse the repository at this point in the history
  • Loading branch information
alknemeyer committed Sep 13, 2018
1 parent 1671914 commit efb3f18
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
45 changes: 32 additions & 13 deletions Intro_to_control_using_MATLAB.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
%% this one

%% 3: basics of control
%% 3.1 creating transfer funtions
%% 3.1.1 creating transfer funtions
disp('------------------------------------------------')
% there are a couple ways of doing this. One way is to define 's' as a
% transfer function variable and then work with it
Expand All @@ -26,7 +26,7 @@
% a third is to define the zeros, poles and gains of the transfer function
tf_3 = zpk(-1, [2, 2], 5)

%% 3.2: basic transfer function arithmetic
%% 3.1.2: basic transfer function arithmetic
disp('------------------------------------------------')
% multiplication etc works as you'd expect
tf1 = 1/s;
Expand All @@ -36,7 +36,29 @@
tf_div = tf1 / tf2
tf_add = tf1 + tf2

%% 3.3.1: getting step responses
%% 3.2: getting some basic transfer function characteristics
disp('------------------------------------------------')
P = 1/(s + 2)/(s + 3)

% print the poles, damping factor, frequency, and time constant
damp(P)

% get the poles of the system
p = pole(P); fprintf('\nPoles of the system are %f, %f\n', p(1), p(2))

% factorise the system
factorised_system = zpk(P)

% get the residue and the roots of the system (google that if you don't
% understand). The funny {:} syntax unpacks a MATLAB 'cell' into an array.
% I don't really understand why it's like that - sorry!
[r, p, k] = residue(P.num{:}, P.den{:});
fprintf('r = %d, %d\np = %d, %d\nk = []\n', r(1), r(2), p(1), p(2))

% don't bother memorizing this stuff - just know that it's there, and that
% you can always come back here or use 'help residue' if you need it

%% 3.3.1: Step responses - how to make them
disp('------------------------------------------------')
% the 'step' command gives the OPEN LOOP step response of the system
sys = 3.5/(s + 2)
Expand All @@ -49,12 +71,7 @@

% don't think of sys/T(s) as a plant or any specific thing. It's just 'a
% transfer function' which relates some input to some output.

%% 3.3.2: more step responses
disp('------------------------------------------------')
% now, think back about how transfer functions work in the orignal block
% diagram. For example, we usually work with T_y/r (transfer function from
% input r to output y) but it's also useful to see other things, like the
the
% input to the plant. Let's make a simple controller - just a gain of 5
plant = 3/(s + 3);
controller = 5;
Expand All @@ -72,7 +89,7 @@
legend('r -> y, open loop', 'r -> y, closed loop', 'd_in -> y, closed loop')
shg;

%% 3.3.3: how do we interpret this?
%% 3.3.2: Step responses - how do we interpret this?
disp('------------------------------------------------')
% closing the loop and adding a gain of five greatly increases the plant's
% performance! However, we still have two issues -
Expand All @@ -96,7 +113,8 @@

T_y_r_cl = L/(1 + L);
T_y_u_cl = plant/(1 + L);
step(plant, T_y_r_cl, T_y_u_cl); grid on; shg;
step(plant, T_y_r_cl, T_y_u_cl, 8); % the 8 at the end = t_final
grid on; shg;
legend('plant - open loop', 'r -> y, closed loop', 'd_in -> y, closed loop')

% now, if there is a step input disturbance, the plant initially goes a bit
Expand Down Expand Up @@ -313,7 +331,8 @@
disp('------------------------------------------------')
% Bode plots are pretty and all, but how do we actually use them?
tf_complex_poles = 1/(s^2 + 0.1*s + 4) % poles at s = -0.05 +- 2i
bode(tf_complex_poles); grid on;
bode(tf_complex_poles, {0.1, 10}); grid on;
% the {0.2, 10} bit is the range of frequencies to plot

% look at figure 1 first

Expand All @@ -333,7 +352,7 @@
% -12dB. Convert from dB's: 10^(-12/20) = 0.25

% let's look at figure 2 to see whether these conclusions are true -
figure(2); step(tf_complex_poles)
figure(2); step(tf_complex_poles); grid on;
% high frequency component --> true
% low DC gain of 0.25 --> true

Expand Down
9 changes: 6 additions & 3 deletions Intro_to_plotting_using_MATLAB.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@

% by default, a new plot is made each time the plot command is called
% you can change this using the 'hold' command
y = sin(0:0.1:10);
plot(y); hold on; plot(y + 1); plot(y + 2);
legend('y', 'y + 1', 'y + 2'); grid on;
t = 0:0.1:20;
y = sin(t);
plot(y); hold on; plot(y .* sin(10*t)); % . for element-wise multiplication
legend('y', 'y * sin(10t)'); grid on;
xlabel('time[s]')

%% 2.4: making subplots
y = sin(0:0.1:100);
Expand All @@ -53,6 +55,7 @@
subplot(3, 2, 3); plot(y - 10); title('3: middle left')
subplot(3, 2, 4); plot(y * 5); title('4: middle right')
subplot(3, 2, 5); plot(y / 5); title('5: you get the idea?')
subplot(3, 2, 6); plot(y .* y); title('x_1^2 \infty <-- fancy text in plots')
shg;

% note that we didn't call figure() before, so the previous plot (if it
Expand Down

0 comments on commit efb3f18

Please sign in to comment.