How can I add a curve to both bode graphs?

44 次查看(过去 30 天)
So I have a single bode graph, with gain and phase response. And I'd like to add one curve to each graph, not sure how I can do it. I have tried to use hold on, but didnt work so well.
Below is the code I used.
if true
csvname = 'Book1.csv';
Gain = csvread(csvname,1,0,[1,0,32,1]);
Phase = csvread(csvname,35,0,[35,0,66,1]);
H = tf(1,[0.01,1])
bode (H,{10,1000})
hold on;
grid on;
plot(Gain(:,1), Gain(:,2))
plot(Phase(:,1), Phase(:,2))
end
Many thanks.
  1 个评论
John BG
John BG 2016-12-15
if you attach Book1.csv to your question the interested readers will be able to reproduce exactly your graphs

请先登录,再进行评论。

回答(3 个)

Maverick
Maverick 2017-5-17
编辑:Maverick 2017-5-17
The answer to your question can be found here , however the thread is pretty messy, so let me bring on minimal working example. It all comes to getting into upper plot, since after bodeplot command the lower one is active. Intuitively one would want to call subplot(2,1,1), but this just creates new blank plot on top of if. Therefore we should do something like this:
% First, define arbitrary transfer function G(s), domain ww
% and function we want to plot on magnitude plot.
s = tf('s');
G = 50 / ( s*(1.6*s+1)*(0.23*s+1) );
ww = logspace(0,3,5000);
y = 10.^(-2*log10(ww)+log10(150));
hand = figure; % create a handle to new figure
h = bodeplot(G,ww);
hold on;
children = get(hand, 'Children') % use this handle to obtain list of figure's children
% We see that children has 3 objects:
% 1) Context Menu 2) Axis object to Phase Plot 3) Axis object to Magnitude Plot
magChild = children(3); % Pick a handle to axes of magnitude in bode diagram
axes(magChild) % Make those axes current
loglog(ww,y,'r');
legend('transfer function','added curve')

Star Strider
Star Strider 2016-12-15
The Control System Toolbox bode function will not let you do that. You have to ask it for the magnitude and phase, and then plot them in a regular subplot figure.
Example:
[mag,phase] = bode(H,{10,1000});
wrad = linspace(10, 1000, length(mag));
Then plot them as for example:
figure(1)
subplot(2,1,1)
semilogx(wrad, mag)
hold on
plot(Gain(:,1), Gain(:,2))
hold off
grid
subplot(2,1,2)
semilogx(wrad, phase)
hold on
plot(Phase:,1), Phase(:,2))
hold off
grid
You will have to experiment with these and your data.
Note that these:
plot(Gain(:,1), Gain(:,2))
plot(Phase(:,1), Phase(:,2))
will plot ‘Gain(:,2)’ as a function of ‘Gain(:,1)’, not of frequency (unless ‘Gain(:,1)’ is frequency. The same applies to the phase plots.
NOTE This is UNTESTED CODE but should work.
  2 个评论
T. Wesley Schlemmer
using semilogx(wrad,mag) returns a "data cannot have more than 2 dimensions" error. mag and phase are generated as 1x1xlength(wrad)

请先登录,再进行评论。


Vladislav Erdman
Vladislav Erdman 2021-5-6
编辑:Walter Roberson 2021-5-6

类别

Help CenterFile Exchange 中查找有关 Plot Customization 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by