How can I add a curve to both bode graphs?

150 Ansichten (letzte 30 Tage)
Sean Sun
Sean Sun am 15 Dez. 2016
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 Kommentar
John BG
John BG am 15 Dez. 2016
if you attach Book1.csv to your question the interested readers will be able to reproduce exactly your graphs

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Maverick
Maverick am 17 Mai 2017
Bearbeitet: Maverick am 17 Mai 2017
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')
  2 Kommentare
Zifeng Qiu
Zifeng Qiu am 29 Sep. 2020
If you do this, the curve on the lower plot would disappear.
Walter Roberson
Walter Roberson am 29 Sep. 2020
You could
hold(magChild, 'on')

Melden Sie sich an, um zu kommentieren.


Star Strider
Star Strider am 15 Dez. 2016
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 Kommentare
T. Wesley Schlemmer
T. Wesley Schlemmer am 5 Nov. 2018
using semilogx(wrad,mag) returns a "data cannot have more than 2 dimensions" error. mag and phase are generated as 1x1xlength(wrad)
Star Strider
Star Strider am 5 Nov. 2018
Use the squeeze function.

Melden Sie sich an, um zu kommentieren.


Vladislav Erdman
Vladislav Erdman am 6 Mai 2021
Bearbeitet: Walter Roberson am 6 Mai 2021

Kategorien

Mehr zu Plot Customization finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by