How to change the default x-axis unit in a Bode diagram to Hertz?

Rad/s is nice for mechanical people, but I'm an EE, and I much prefer Hertz. It should be something simple, but I can't find it in the help.

2 Comments

Don't believe there is a way in the base routine; it's built entirely around rad/timeunits per the system object.
You could write a wrapper routine to convert frequency units and update the plot labels.
Actually you can, click on the plot tab from linearization manager, click plot properties, click units. Change to whatever units you need.

Sign in to comment.

 Accepted Answer

Use bodeplot (link) instead of bode.
It gives you that option, and the documentation specifically mentions that.

5 Comments

Thanks, this is exactly what I needed -- no fiddling with options or handles.
My pleasure.
The bodeplot function is useful, since bode does not allow easy adjustment of its properties.
A simple work-around is probably to get the outputs of bode and plot them. Since rad/s=2*pi*f (with ‘f’ in Hz), it is probably easiest to simply divide the ‘wout’ output by (2*pi):
Fs = 1000;
H = tf([-0.1,-2.4,-181,-1950],[1,3.3,990,2600]);
[mag,phase,wout] = bode(H);
wrange = [min(wout); max(wout)]; % Units: rad/sec
frange = wrange/(2*pi); % Units: Hz
figure
subplot(2,1,1)
plot(wout, squeeze(mag))
grid
subplot(2,1,2)
plot(wout, squeeze(phase))
grid
xlabel('Frequency (rad/s)')
figure
subplot(2,1,1)
plot(wout/(2*pi), squeeze(mag))
grid
subplot(2,1,2)
plot(wout/(2*pi), squeeze(phase))
grid
xlabel('Frequency (Hz)')
The two range vectors are not necessary for the code. I used them to check to be certain everything looked correct.
I've put the below function in my userpath . It'll do for now.
% a new bode() command that has Hz as default
function h = bodef(x)
P = bodeoptions; P.FreqUnits = 'Hz';
h = bodeplot(x,P);
That is what I would do.
I defer to bodeplot because it allows some customization. I use and plot the bode outputs only if I want other options.
EDIT
I add that this was my original recommendation!
Beginning in R2024b, the FrequencyUnit can be modified directly using the property on the output of "bodeplot".
h = bodeplot(zpk(-1,[-2 -3],1));
h.FrequencyUnit = 'Hz';
To set the default units, use "ctrlpref".

Sign in to comment.

More Answers (3)

Type 'ltiview' in your command window. The Linear System Analyzer will pop up. Click on: File --> Toolbox Preferences... --> Units --> Frequency: Hz

3 Comments

Then I must interact with the GUI for every plot. Ideally, everything is explicitly programmable.
You need to change it once in the GUI and you always obtain bode plots in Hz. The setting remains even when you restart Matlab.
And what when I share my function/script with others? Or use different (older) versions of MATLAB?

Sign in to comment.

clc;close all; clc
% test system
s=tf('s');
H=(s-1)/((s-3)*(s-2))
% bode
[mag,phase,wout] = bode(H);
%plot results, with frequency expressed at Hz
figure;
subplot(2,1,1);
semilogx(wout(:,1)/(2*pi), 20*log10(squeeze(mag)), '-b'); zoom on; grid on;
title('magnitude'); xlabel('Frequency (Hz)'); ylabel('Magnitude (dB)');
subplot(2,1,2);
semilogx(wout(:,1)/(2*pi), squeeze(phase), '-r'); zoom on; grid on;
title('Phase'); xlabel('Frequecy (Hz)'); ylabel('Phase (deg)');

2 Comments

This is the correct answer. Some of the others simply change the label to Hz without rescaling.
%%% G1 & and G2 is your TFs
[mag1, phase1, wout1] = bode(G1);
[mag2, phase2, wout2] = bode(G2);
% Convert angular frequency (rad/s) to frequency (Hz)
freq1 = wout1 / (2 * pi);
freq2 = wout2 / (2 * pi);
% Plot the magnitude response
figure;
subplot(2,1,1);
semilogx(freq1, 20*log10(squeeze(mag1)), '-b');
hold on;
semilogx(freq2, 20*log10(squeeze(mag2)), '-g');
grid on;
title('Magnitude');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
legend('G1', 'G2');
%%%%%%%%%%%%
% Plot the phase response
subplot(2,1,2);
semilogx(freq1, squeeze(phase1), '-r');
hold on;
semilogx(freq2, squeeze(phase2), '-m');
grid on;
title('Phase');
xlabel('Frequency (Hz)');
ylabel('Phase (deg)');
legend('G1', 'G2');

Sign in to comment.

ctrlpref allows one to change the default units for frequency axis when plotting with bode and bodeplot (among other defaults in the Control System Toolbox). As far as I know, this preference only affects the plot, it doesn't affect the the third output from bode if using output arguments.

4 Comments

It would be nice if it were possible to bulkload and bulkstore one's personal preferences. Then a distributed program could always start with something like
personal=loadPrefs; setPrefs(personal);
and (optionally) exit with
setPrefs(factory);
It would be necessary to edit/change one's preferences once in a while. Ideally all existing tools would be compatible with that tool (or use it).
Preferences set via ctrlpref "are automatically reloaded in future MATLAB® sessions using the Control System Toolbox software" so there should be no need to load them for each Matlab session.
Preferences for other things set through the Preferences button on the Enviroment section of the ribbon of the Matlab window also carry over to future Matlab sessions so no need to do anything on a per session basis.
Don't know why Control System Toolbox preferences are not accessible through the Preferences button as is the case for other toolboxes.
The startup.m file offers a way to "bulkload" things at the start of Matlab session.
This is fine, but I want those getter/setters to make sure that my programs work as intended when I share the file with somebody else, or when I work on a different machine in a different location under a different account. I am not a fan of storing everything in the cloud on potentially hostile servers, so I'd like to store these things explicitly in the code. The features I mentioned would help in (re)setting the environment, which is always a huge hassle.
Some things can be controlled programatically via settings. See Access and Modify Settings. Unfortunately, these don't cover the Control System Toolbox (why not?). As far as I know, the only way to deal with the CST is via ctrlpref, for which there is no programmatic interface (why not?), as far as I know. Those CST preferences are saved to disk somewhere somehow, so it might be possible copy them from one machine to another. You may want to open up a new question on this topic.
s = settings
s =
SettingsGroup with properties: drivingscenario: [1x1 SettingsGroup] matlab: [1x1 SettingsGroup] kits_common: [1x1 SettingsGroup] Simulink: [1x1 SettingsGroup] database: [1x1 SettingsGroup] roadrunner: [1x1 SettingsGroup] driving: [1x1 SettingsGroup] comparisons: [1x1 SettingsGroup] parallel: [1x1 SettingsGroup]

Sign in to comment.

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!