Dealing with transfer function in symbolic math toolbox.
146 views (last 30 days)
Show older comments
I used the command syms to symbolize the letter 's' and use it to construct transfer functions. is there a way to bodeplot the TF or getting it's DC gain without having to manually copy the numbers into a Numerator and Denominator vectors to use the function TF(Numerator,Denominator)?
thanks in advance.
0 Comments
Accepted Answer
Star Strider
on 9 Sep 2023
Yes.
Once you have the symbolic polynomials in s, use the sym2poly function to extract the coefficiinets. (It may be necesary to use the collect function fiirst on each of the numerator and denominator polynomials.)
Example —
syms s
H(s) = (2*s^2 + 5*s + 1) / (s^3 + 7*s^2 +2*s + 7) % Arbitrary Transfer Function
[nm,dn] = numden(H)
nmd = sym2poly(nm)
dnd = sym2poly(dn)
Hs = tf(nmd,dnd)
figure
bode(Hs)
grid
figure
step(Hs)
grid
At least my random transfer function appears to be stable!
.
2 Comments
More Answers (1)
Sam Chak
on 9 Sep 2023
Hi @Hamza Harbi
In MATLAB, the transfer function is preferred to be modeled as a tf-class System Object, which is used for simulating dynamic systems with inputs that change over time. System Objects can take advantage of many useful functions from the Control System, Robust Control, and System Identification Toolboxes for modeling, analysis, design, and simulation.
However, if a dynamic system is modeled as a Symbolic Function, then some extra work is required to mathematically compute the log-magnitude curve and the phase-angle curve. Two methods are shown below:
Method 1: When a dynamic system is modeled as a System Object:
% transfer function as a System Object
G = tf(12, [1 2 3])
className = class(G)
get(G)
A = dcgain(G)
figure(1)
bodeplot(G), grid on
Method 2: When a dynamic system is modeled as a Symbolic Function:
% transfer function as a Symbolic Function
syms G(s)
G(s) = 12/(s^2 + 2*s + 3)
className = class(G)
Gdc = limit(G, s, 0) % calculate DC gain using limit as s → 0
% Compute response against the frequency ω on a logarithmic scale with base 10.
omega = logspace(-1, 2); % create log scale for ω on x-axis from 10⁻¹ to 10²
Gjw = vpa(subs(G, s, 1j*omega), 4); % G(jω)
GjwMag = 20*log10(abs(Gjw)); % log-magnitude of G(jω)
GjwPha = rad2deg(angle(Gjw)); % phase-angle of G(jω)
figure(2)
tiledlayout(2, 1, 'TileSpacing', 'Compact');
nexttile()
semilogx(omega, GjwMag), , grid on % plot the log-magnitude of G(jω)
ylabel('Magnitude (dB)');
title("Frequency Response of G(s) = "+string(G))
set(gca, 'XLim', [0 max(omega)]);
nexttile()
semilogx(omega, GjwPha), grid on % plot the phase-angle of G(jω)
xlabel('Frequency (rad/s)');
ylabel('Phase (deg)');
set(gca, 'XLim', [0 max(omega)]);
set(gca, 'YLim', [-180 0]);
yticks(-180:45:0)
2 Comments
Paul
on 9 Sep 2023
Seems pretty short, and can be made even shorter using Control System Toolbox for the plotting.
syms G(s) w
G(s) = 12/(s^2 + 2*s + 3);
omega = logspace(-1, 2); % create log scale for ω on x-axis from 10⁻¹ to 10²
Gjw = vpa(subs(G, s, 1j*omega), 4); % G(jω)
bodeplot(frd(double(Gjw),omega));
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!