Last active
May 20, 2024 18:11
-
-
Save yuvve/f1303f2ff559d541e9b044848cd746bc to your computer and use it in GitHub Desktop.
MATLAB transfer function information
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% Transfer function information | |
% This script takes either polynomial coefficients for the numerator and denominator | |
% OR zero and pole locations (factorized roots) + gain | |
% OR characteristic equation (undamped natural frequency and damping ratio) | |
% of a system with a second order complex pole | |
% of the transfer function of a system, and then returns: | |
% 1. A graph of system's impulse response. | |
% 2. A graph of the system's step response. | |
% 3. The pole-zero diagram of the system. | |
% 4. The factorized transfer function printed with LaTeX. | |
% 5. The transfer function printed with LaTeX. | |
% 6. The partial fraction decomposition of the transfer function printed | |
% with LaTeX. | |
% 7. The time-domain equation (impulse response) of the system printed with | |
% LaTeX. | |
% 8. Bode diagrams. | |
%% Configuration Parameters | |
mode = 2; % 0 = polynomial coefficients, 1 = factorized roots, 2 = complex conjugate pair | |
time_multiplier = 2; % Increase to make the impulse response show a longer timespan | |
digits(6); % maximum number of digits for the program to disply | |
% Polynomial coefficients (i.e. [1, 2, 0] will yield s^2 + 2s; [1 0 0 0] will yield s^3) | |
num = [10 -10 0]; % Enter coefficients for TF zeros here | |
den = [1 4 17/4]; % Enter coefficients for TF poles here | |
% Factorized roots (i.e. [1 0] will yield (s-1)s) | |
zeros = [1 0]'; % Enter roots of TF zeros | |
poles = [-2+0.5j -2-0.5j]'; % Enter roots of TF poles | |
gain = 10; % Enter DC gain | |
% Complex conjugate pair char. equation (omega^2/(s^2 + 2*zeta*omega*s + omega^2)) | |
omega = 0.4; | |
zeta = 0.2; | |
%% Program | |
syms s | |
if mode == 0 | |
zeros = roots(num); | |
poles = roots(den); | |
elseif mode == 1 | |
[num, den] = zp2tf(zeros, poles, gain); | |
else % mode == 2 | |
num = [omega^2]; | |
den = [1 2*zeta*omega omega^2]; | |
end | |
H = tf(num,den); | |
num_sym = poly2sym(num,s); | |
den_sym = poly2sym(den,s); | |
h = ilaplace(num_sym/den_sym); | |
t=0:0.1:32*time_multiplier; | |
factored_num = ''; | |
for n=factor(num_sym,"FactorMode","complex") | |
factored_num = strcat(factored_num,'(',string(n),')'); | |
end | |
factored_den = ''; | |
for d=factor(den_sym,"FactorMode","complex") | |
factored_den = strcat(factored_den,'(',string(d),')'); | |
end | |
factored = strcat('$H(s)=','\frac{', factored_num, '}{', factored_den, '}$'); | |
figure(1) | |
clf(); | |
subplot(2,3,1) | |
hold on | |
impulse(H, t); | |
grid on | |
hold off | |
subplot(2,3,4) | |
hold on | |
step(H, t); | |
grid on | |
hold off | |
subplot(2,3,3) | |
hold on | |
zplane(num,den) | |
grid on | |
hold off | |
subplot(2,3,2) | |
hold on | |
title("Transfer Function") | |
text(-0.25, 0.75, factored , 'Interpreter', 'latex', 'FontSize', 16) | |
text(-0.25, 0.5, ['= $' latex(num_sym/den_sym) '$ = $' latex(partfrac(num_sym/den_sym, s)) '$'], 'Interpreter', 'latex', 'FontSize', 22); | |
axis off; | |
hold off | |
subplot(2,3,5) | |
hold on | |
title("Impulse Response") | |
text(-0.25, 0.5, ['h(t) = $' latex(h) '$'], 'Interpreter', 'latex', 'FontSize', 16); | |
axis off; | |
hold off | |
subplot(2,3,6) | |
hold on | |
bode(H) | |
grid on | |
hold off |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment