-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.m
More file actions
83 lines (67 loc) · 1.84 KB
/
select.m
File metadata and controls
83 lines (67 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
% Clear workspace and command window
close all;
clear all;
clc;
param = 5; % To select wi
w = [2*pi/5, 4*pi/5];
selection = 2; % Select w1
wc = w(selection);
wi = wc / param;
% Calculate Kp for |G(jw1)| = 0dB
% Solving the equations explained in the Method part:
Kp = wc^2/sqrt(wc^2 + wi^2);
Ki = wi * Kp;
% Create transfer functions
s = tf('s');
PI_controller = Kp * (1 + wi/s);
Plant = 1 / s;
G = PI_controller * Plant;
% Verify the magnitude at w1
[mag, ph] = bode(G, wc);
mag_db = 20*log10(mag)
ph
% Bode plot
omega = logspace(-2, 2, 1000);
bp = figure;
[mag, phase, wout] = bode(G, omega);
% Convert data
mag = squeeze(mag);
phase = squeeze(phase);
mag_db = 20*log10(mag);
% Generate Bode plots
subplot(2,1,1);
semilogx(wout, mag_db, 'LineWidth', 1.5); hold on;
plot([wout(1) wout(end)], [0 0], 'k');
grid on;
ylabel('Magnitude (dB)');
title(sprintf('Bode of G(jw) for w = %.2f rad/s', wc));
% Highlight frequencies...
[~, idx1] = min(abs(wout - wc));
plot([wc wc], ylim, 'r--', 'LineWidth', 1.2);
plot(wc, mag_db(idx1), 'ro', 'MarkerFaceColor', 'r', 'MarkerSize',8);
text(wc, mag_db(idx1), sprintf('%.2f dB', mag_db(idx1)), 'VerticalAlignment', 'bottom');
subplot(2,1,2);
semilogx(wout, phase, 'LineWidth', 1.5); hold on;
grid on;
ylabel('Phase (degrees)');
xlabel('Frequency (rad/s)');
phase_margin = 180 + phase(idx1)
title(sprintf('Phase margin = %.2f degrees', phase_margin));
plot([wc wc], ylim, 'r--', 'LineWidth', 1.2);
plot(wc, phase(idx1), 'ro', 'MarkerFaceColor', 'r', 'MarkerSize',8);
text(wc, phase(idx1), sprintf('%.1fdeg', phase(idx1)), 'VerticalAlignment', 'bottom');
plot_name = sprintf('plots/ode_plot_%d', selection);
saveas(bp, plot_name, 'png');
% Display transfer function, poles, and zeros
disp('Open loop:');
G
[zeros, poles, gain] = zpkdata(G, 'v');
disp('Zeros:');
disp(zeros);
disp('Poles:');
disp(poles);
disp('Gain:');
disp(gain);
wc
Kp
Ki