% TF_1mass_1spring_1damper
% by Maarten Dekker
% Version #01

% Computation of transfer function of mech. system
% with 1 mass m, 1 spring c and 1 damper d

% Clear history
clear all; close all; clc;

% Parameters:
m = 100;
c = 10;
d = 0.001;

% Transfer functions
H1 = tf([1],[m d c]);   % input = Fe,  output = x
H2 = tf([d c],[m d c]); % input = xin, output = x
bode(H1,H2)

% Frequency range
dw = 1;
w  = 1:dw:1e5;
f  = w./2./pi;

% Magnitude
A1 = sqrt((1).^2 + (0).^2)    ./   sqrt((c-m.*w.^2).^2 + (d.*w).^2);
A2 = sqrt((c).^2 + (d.*w).^2) ./   sqrt((c-m.*w.^2).^2 + (d.*w).^2);
A1dB = 20.*log10(A1);
A2dB = 20.*log10(A2);

% Phase
ph1 = atan2((0) , (1))    -   atan2((d.*w) , (c-m.*w.^2));
ph2 = atan2((d.*w) , (c)) -   atan2((d.*w) , (c-m.*w.^2));
ph1d = ph1/pi*180;
ph2d = ph2/pi*180;

% Plot
figure;
subplot 211; semilogx(w,A1dB,w,A2dB);
subplot 212; semilogx(w,ph1d,w,ph2d);