Code:
clc
clear all
m = 3; % Mass in kg
k = 6000; % Spring Constant in N/m
b = 20; % Damping Coeficient in N/(m/s)
dt = 0.0001; % Time Interval
tf = 2; % Final Time
t = [0:dt:tf]; % Time of Simulation (Vector)
% Matricies A, B, C, & D for Mathematical Model
A = [0 1; (-k/m) (-b/m)];
B = [0; (1/m)];
C = [1 0; 0 1; (-k/m) (-b/m)];
D = [0; 0; (1/m)];
x0 = [0; 0]; % Initial Conditions [Position; Velocity]
F = 400; % Input Force in N
u = F*ones(size(t)); % Input Force (Vector)
sys1 = ss(A,B,C,D); % State Space Representation of Math Model
[y,t,x] = lsim(sys1,u,t,x0); % Linear Simulation
% y: Outputs Matrix [Position Velocity Acceleration]
% t: Time of Simulation Vector
% x: State Variables Matrix [q1 q2]
figure(1)
subplot(311) % 3 Plots (3x1 Array) Plot 1
plot(t,y(:,1), 'b','LineWidth',1.5)
title('MSD - Position v. Time Plot')
xlabel('Time (sec)','FontSize',12)
ylabel('Position (m)','FontSize',12)
grid on;
subplot(312) % 3 Plots (3x1 Array) Plot 2
plot(t,y(:,2), 'r','LineWidth',1.5)
title('MSD - Velocity v. Time Plot')
xlabel('Time (sec)','FontSize',12)
ylabel('Velocity (m/s)','FontSize',12)
grid on;
subplot(313) % 3 Plots (3x1 Array) Plot 3
plot(t,y(:,3), 'g','LineWidth',1.5)
title('MSD - Acceleration v. Time Plot')
xlabel('Time (sec)','FontSize',12)
ylabel('Acceleration (m/s^2)','FontSize',12)
grid on;