How can I plot transfer function?

Hi,How could I plot a transfer function (Magnitude (Amplitude),Phase) which has maybe complex zeros and poles. For example H=s/((s+1)*(s+2)).

 采纳的回答

You can use the transfer function that you posted, if you define ‘s’ first, using the tf function.
If you want the magnitude and phase matrices, and frequency vectors, use bode, since it can produce all those, however it has llimited plotting interactivity. For more extensive plot options, use bodeplot
s = tf('s');
H=s/((s+1)*(s+2))
H = s ------------- s^2 + 3 s + 2 Continuous-time transfer function.
figure
bode(H)
grid
figure
h = bodeplot(H);
grid
opts = getoptions(h);
opts.FreqUnits = 'Hz';
setoptions(h,opts)
See the relevant function documentation for details.
.

11 个评论

Tom
Tom 2023-6-28
编辑:Tom 2023-6-28
Thanks, For complex poles or zeros it doesn't differ? And How can I specify the w range(for negative value) ?And also if It has the gain (K)?
For complex poles or zeros it doesn't differ?
No.
And How can I specify the w range (for negative value) ?
I have not ever encountered negative frequencies on a Bode plot. However to specify a frequency range, see the bodeplot documentation section on w to specify it as a cell vector in units of rad/TimeUnit.
And also if It has the gain (K)?
Not specifically in the plot, however you can compute it using the zpk function —
s = tf('s');
H=s/((s+1)*(s+2))
H = s ------------- s^2 + 3 s + 2 Continuous-time transfer function.
zpk(H)
ans = s ----------- (s+2) (s+1) Continuous-time zero/pole/gain model.
So here it has unity gain.
.

Could I plot like this?

Do the experiment to find out —
s = tf('s');
H=s/((s+1)*(s+2))
H = s ------------- s^2 + 3 s + 2 Continuous-time transfer function.
figure
bode(H, {-30, 30})
Error using DynamicSystem/bode
The frequency interval must be specified as {WMIN,WMAX} where WMIN and WMAX are real frequencies satisfying 0<=WMIN<WMAX<=Inf.
grid
figure
h = bodeplot(H, {-30, 30});
grid
opts = getoptions(h);
opts.FreqUnits = 'Hz';
setoptions(h,opts)
So, the answer is a resounding No for both bode and bodeplot.
(See following Comment, since I want to keep the error message in place in this Comment.)
.
My compluter crashed (again). I thought this originally posted before then, apparently it didn’t.
Fs = 10000;
Fn = Fs/2;
T = 1000;
t = linspace(0, T*Fs, T*Fs+1).'/Fs;
L = numel(t);
s = tf('s');
H = s/((s+1)*(s+2))
H = s ------------- s^2 + 3 s + 2 Continuous-time transfer function.
u = zeros(size(t));
u(ceil(L/2)) = 1;
s = lsim(H, u, t);
% figure
% plot(t, s)
% grid
FTs = fft(s)/L;
FTu = fft(u)/L;
FTy = FTs ./ FTu;
FTy2 = fftshift(FTy);
Fv2 = linspace(-Fn, Fn, numel(FTy));
figure
subplot(2,1,1)
plot(Fv2, mag2db(abs(FTy2)))
grid
xlim([-10 10])
ylim([-50 0])
xlabel('Frequency (Hz)')
ylabel('Magnitude (dB)')
subplot(2,1,2)
plot(Fv2, rad2deg(angle(FTy2)))
grid
xlim([-10 10])
xlabel('Frequency (Hz)')
ylabel('Phase (°)')
sgtitle('Two-Sided Fourier Transform')
figure
subplot(2,1,1)
plot(Fv2*pi, mag2db(abs(FTy2)))
grid
xlim([-10 10]*pi)
ylim([-50 0])
xlabel('Frequency (rad/s)')
ylabel('Magnitude (dB)')
subplot(2,1,2)
plot(Fv2*pi, rad2deg(angle(FTy2)))
grid
xlim([-10 10]*pi)
xlabel('Frequency (rad/s)')
ylabel('Phase (°)')
sgtitle('Two-Sided Fourier Transform')
Fv1 = linspace(0, 1, fix(numel(FTy)/2)+1)*Fn;
Iv1 = 1:numel(Fv1);
figure
subplot(2,1,1)
semilogx(Fv1, mag2db(abs(FTy(Iv1))))
grid
xlim([1E-3, 10])
xlabel('Frequency (Hz)')
ylabel('Magnitude (dB)')
subplot(2,1,2)
semilogx(Fv1, rad2deg(angle(FTy(Iv1))))
grid
xlim([1E-3, 10])
xlabel('Frequency (Hz)')
ylabel('Phase (°)')
sgtitle('One-Sided Fourier Transform')
figure
subplot(2,1,1)
semilogx(Fv1*pi, mag2db(abs(FTy(Iv1))))
grid
xlim([1E-3, 10]*pi)
xlabel('Frequency (rad/s)')
ylabel('Magnitude (dB)')
subplot(2,1,2)
semilogx(Fv1*pi, rad2deg(angle(FTy(Iv1))))
grid
xlim([1E-3, 10]*pi)
xlabel('Frequency (rad/s)')
ylabel('Phase (°)')
sgtitle('One-Sided Fourier Transform')
This uses a centred impulse function as the ‘u’ input. The rest is straightforward. (It is not possible to plot logarithmic frequency axes with negative frequencies.)
.
Thank you , the problem solved.
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
As always, my pleasure!
My pleasure!
A Vote would be appreciated!

请先登录,再进行评论。

更多回答(1 个)

To plot the magnitude and phase of a transfer function with complex zeros and poles in MATLAB, you can use the bode function.
% Define the transfer function
num = [1 0]; % Numerator coefficients for s
den = [1 3 2]; % Denominator coefficients for s
% Create the transfer function object
H = tf(num, den);
% Plot the magnitude and phase using the bode function
bode(H);
This code will generate a plot with two subplots: one for the magnitude (amplitude) and one for the phase response of the transfer function. The frequency range of the plot is determined automatically based on the system dynamics. You can customize the plot further by modifying the properties of the bode function. For example, you can specify a frequency range using the bode(H, w) syntax, where w is a vector of frequencies at which to evaluate the transfer function. Additionally, you can use the subplot function to create separate plots for magnitude and phase if you prefer individual plots rather than subplots.

4 个评论

Tom
Tom 2023-6-28
编辑:Tom 2023-6-28
Thank you but how can I specify a frequency for example in range -30 to 30 for plotting transfer function? And also if It has the gain (K)?
If you want to specify a linear frequency range from -30 to 30 for plotting the transfer function using the bode function in MATLAB, you can use the linspace function to create the desired frequency range.
% Define your transfer function numerator and denominator coefficients
num = [1]; % Example numerator coefficients
den = [1, 2, 1]; % Example denominator coefficients
% Create the transfer function model
tf_model = tf(num, den);
% Specify the custom frequency range
frequency_range = linspace(-30, 30, 1000); % Specify the desired frequency range
% Plot the Bode response with the custom frequency range
bode(tf_model, frequency_range);
OR if you are using bodeplot as suggested by @Star Strider you need to befine the Frequency scale, and you can find the example by running the code line below:
openExample('controls_id/BodePlotOfTransferFunctionExample')
For further details you can view the Documentation for these functions by accessing the Help.

ProblemSolver

I wanna something like this...

which can be very simply achieved using the solution in this answer, with one modification.
bode w/o output arguments won't meet the need, but one can always use the output arguments and then plot manually.
[m,p] = bode(tf_model, frequency_range);
plot(frequency_range,db(abs(squeeze(m)))),grid % and similar for phase

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Plot Customization 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by