Asinine
Asinine

Reputation: 37

Vertical lines for Bode plots in Matlab

I have graphed a Bode plot for my transfer function, and I was wondering if there is some way to insert either horizontal or vertical lines to show a specific value for the gain/phase angle or frequency?

I have found with the following code I can draw a horizontal line on the phase angle graph:

x = linspace(10^-1,10^2,100);

for bleh = 1:length(x)
      y(bleh) = -30.9638;
end

bode(num, den)
hold on
plot(x,y)

But this does not seem to apply in the gain graph, nor does my limited knowledge (and only way that makes sense to me) of vertical lines. I tried:

y1 = get(gca,'ylim');
w1 = 1.2;

bode(num, den)
hold on

plot(x,y,[w1 w1],y1)

But I only get the one horizontal line as was done from the above code. Is this a possibility?

(Using R2017a, if that matters.)

Upvotes: 1

Views: 6528

Answers (1)

il_raffa
il_raffa

Reputation: 5190

I'm not sure I've understood you question, nevertheless, I propose the following.

When there are more one axes in a figure, as it is the case of the bode diagram, if you want to add something in a specific axes (or in all) you have to specify, in the call to plot the handle of the axes.

So, to add lines in the bode diagram, you have first to identify the handles of the two axes: you can do it in, at least two way:

  • using the findobj function: ax=findobj(gcf,'type','axes')
  • extract them as the Children of the figure: ax=get(gcf,'children')

Once you have the handles of the axes, you can get their XLim and YLim that you can use to limit the extent of the line you want to add.

In the following example, I've used the above proposed approach to add two lines in each graph.

The horizontal and vertical lines are added in the middle point of the X and Y axes (problably this point does not have a relevant meaning, but it is ... just an example).

% Define a transfer function
H = tf([1 0.1 7.5],[1 0.12 9 0 0]);
% PLot the bode diagram
bode(H)
% Get the handles of the axes
ax=findobj(gcf,'type','axes')
phase_ax=ax(1)
mag_ax=ax(2)
% Get the X axis limits (it is the same for both the plot
ax_xlim=phase_ax.XLim
% Get the Y axis limits
phase_ylim=phase_ax.YLim
mag_ylim=mag_ax.YLim
%
% Define some points to be used in the plot
%    middle point of the X and Y axes of the two plots
%
mid_x=(ax_xlim(1)+ax_xlim(2))/2
mid_phase_y=(phase_ylim(1)+phase_ylim(2))/2
mid_mag_y=(mag_ylim(1)+mag_ylim(2))/2
% Set hold to on to add the line
hold(phase_ax,'on')
% Add a vertical line in the Phase plot
plot(phase_ax,[mid_x mid_x],[phase_ylim(1) phase_ylim(2)])
% Add an horizontal line in the Phase plot
plot(phase_ax,[ax_xlim(1), ax_xlim(2)],[mid_phase_y mid_phase_y])
% Set hold to on to add the line
hold(mag_ax,'on')
% Add a vertical line in the Magnitide plot
plot(mag_ax,[mid_x mid_x],[mag_ylim(1) mag_ylim(2)])
% Add an Horizontal line in the Magnitide plot
plot(mag_ax,[ax_xlim(1), ax_xlim(2)],[mid_mag_y mid_mag_y])

enter image description here

Hope this helps,

Qapla'

Upvotes: 3

Related Questions