Correct Axis Not Used

1 view (last 30 days)
Samantha Boger
Samantha Boger on 22 Oct 2020
Answered: dpb on 22 Oct 2020
I wrote a script that is supposed to plot a graph with definded axis limits. For most of the functions I have input, it has worked correctly, but for this following one, it doesn't have any regard for the axis limits.
This is the incorrect output graph:
This is the code:
close all
clearvars
clc
j = sqrt(-1);
w = 0.001:0.001:100;
s = j.*w;
GH = (5.*s+1)./(s.^2.*(4.*s+1));
makeNyquist(GH, w, 3, 'example.png')
makeNyquist Function:
function makeNyquist(GH, w, ylimit, plotFilename)
fig = figure;
ylabel('Im(GH)')
grid on
xlim([-ylimit,ylimit])
ylim([-ylimit,ylimit])
axis equal
xlabel('Re(GH)')
hold on
% plot axes
line([0 0], ylim, 'color', 'k'); % y-axis
hold on
line(xlim, [0 0], 'color', 'k'); % x-axis
hold on
% plot unit circle
y = -1:0.001:1;
plot(sqrt(1-y.^2), y, '-k')
hold on
plot(-sqrt(1-y.^2), y, '-k')
hold on
% plot (-1, j0) point
plot(-1, 0, 'ok')
hold on
% plot nyquist locust
rgh = real(GH);
igh = imag(GH);
plot(rgh, igh, '-r')
hold on
% plot mirrored image (dotted)
plot(rgh, -igh, '--r')
hold on
% calculate gain margin and phase crossover freq
for i = 2:length(igh)
if (igh(i)<0 && igh(i-1)>=0) || (igh(i)>0 && igh(i-1)<=0)
igh_xover_idx = i;
phz_xover_freq = w(igh_xover_idx);
reInt = rgh(igh_xover_idx);
GM = -20 * log10(abs(reInt));
sprintf('real axis intersect at Re = %.1f', reInt)
sprintf('gain margin (dB) = %.3f', GM)
plot(rgh(i), igh(i), '*b')
hold on
sprintf('phase crossover freq = %.3f', phz_xover_freq)
break
end
end
% calculate phase margin and gain crossover freq
for j = 2:length(igh)
if (sqrt((rgh(j)).^2 + (igh(j)).^2)) <= 1
x = rgh(j);
y = igh(j);
plot(rgh(j), igh(j), '*b')
hold on
gain_xover_freq = w(j);
sprintf('gain crossover freq = %.3f', gain_xover_freq)
sprintf('unit circle intersect at (%.1f, %.1f)', x, y)
u = [x y];
v = [-1 0];
theta = acosd(dot(u,v) / (norm(u)*norm(v)));
if y > 0
theta = -theta;
elseif theta < 0
theta = theta + 180;
end
PM = theta;
sprintf('phase margin (deg) = %.3f', PM)
break
end
end
% save figure
saveas(fig, plotFilename)
end

Answers (1)

dpb
dpb on 22 Oct 2020
Move the xlim, ylim calls to the end of the function makeNyquist
The plotting commands after the limits end up autoscaling again to the data range.
Also, once hold on is called; there's no need to call it again unless you explicitly take it of.
"ON" can't get any "ON-ER" by being repeated.

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!