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)).

 Respuesta aceptada

Star Strider
Star Strider el 28 de Jun. de 2023
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 comentarios

Tom
Tom el 28 de Jun. de 2023
Editada: Tom el 28 de Jun. de 2023
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.
.
Tom
Tom el 28 de Jun. de 2023

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.)
.
Tom
Tom el 28 de Jun. de 2023
Thank you , the problem solved.
Star Strider
Star Strider el 28 de Jun. de 2023
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
Tom
Tom el 28 de Jun. de 2023
👍
Star Strider
Star Strider el 28 de Jun. de 2023
As always, my pleasure!
Lakshminarayanan
Lakshminarayanan el 29 de Ag. de 2024
Thanks
Star Strider
Star Strider el 29 de Ag. de 2024
My pleasure!
A Vote would be appreciated!

Iniciar sesión para comentar.

Más respuestas (1)

ProblemSolver
ProblemSolver el 27 de Jun. de 2023
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 comentarios

Tom
Tom el 28 de Jun. de 2023
Editada: Tom el 28 de Jun. de 2023
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.
Tom
Tom el 28 de Jun. de 2023

ProblemSolver

I wanna something like this...

Paul
Paul el 28 de Jun. de 2023
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

Iniciar sesión para comentar.

Categorías

Productos

Preguntada:

Tom
el 27 de Jun. de 2023

Comentada:

el 29 de Ag. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by