-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_root_locus_picker.m
More file actions
44 lines (38 loc) · 1.45 KB
/
interactive_root_locus_picker.m
File metadata and controls
44 lines (38 loc) · 1.45 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
function interactive_root_locus_picker()
% interactive_root_locus_picker - Click to select poles and zeros, then plot root locus.
% Automatically adds conjugates to ensure real coefficients.
figure;
axis([-10 10 -10 10]);
axis equal;
grid on;
title({'Select Pole Locations (Press ENTER When Done)', ...
'Then Select Zeros (Press ENTER When Done)'});
xlabel('Real Axis'); ylabel('Imaginary Axis');
hold on;
% Step 1: Select Poles
disp('Select poles by clicking. Press ENTER when done.');
[x_p, y_p] = ginput();
poles = x_p + 1i * y_p;
poles = add_conjugates(poles);
plot(real(poles), imag(poles), 'rx', 'MarkerSize', 10, 'LineWidth', 2);
% Step 2: Select Zeros
title({'Now Select Zero Locations (Press ENTER When Done)'});
disp('Now select zeros by clicking. Press ENTER when done.');
[x_z, y_z] = ginput();
zeros_ = x_z + 1i * y_z;
zeros_ = add_conjugates(zeros_);
plot(real(zeros_), imag(zeros_), 'bo', 'MarkerSize', 10, 'LineWidth', 2);
% Step 3: Build real-coefficient transfer function and plot root locus
clf;
num = poly(zeros_);
den = poly(poles);
G = tf(num, den);
rlocus(G);
title('Root Locus Plot from Selected Poles and Zeros');
grid on;
end
function c_full = add_conjugates(c)
% Adds conjugates of complex numbers (if not purely real)
c_conj = conj(c(imag(c) ~= 0));
c_full = unique([c; c_conj]); % Add only unique points
end