Azzera filtri
Azzera filtri

How can I add a curve to both bode graphs?

46 visualizzazioni (ultimi 30 giorni)
Sean Sun
Sean Sun il 15 Dic 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 Commento
John BG
John BG il 15 Dic 2016
if you attach Book1.csv to your question the interested readers will be able to reproduce exactly your graphs

Accedi per commentare.

Risposte (3)

Maverick
Maverick il 17 Mag 2017
Modificato: Maverick il 17 Mag 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 Commenti
Zifeng Qiu
Zifeng Qiu il 29 Set 2020
If you do this, the curve on the lower plot would disappear.

Accedi per commentare.


Star Strider
Star Strider il 15 Dic 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 Commenti
T. Wesley Schlemmer
T. Wesley Schlemmer il 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 il 5 Nov 2018
Use the squeeze function.

Accedi per commentare.


Vladislav Erdman
Vladislav Erdman il 6 Mag 2021
Modificato: Walter Roberson il 6 Mag 2021

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by