% This file will generate 2 plots for typical physical linear systems
% The idea is to observe a mass spring damper with varied damping and
% stiffness effects. Here a basic mass of 10kg, varied damper, and varied
% weakly stiff spring are used to simulate the dynamics.
% there are 6 different sets of dynamics/plants to observe variations in
% the physics. 
% 1)underdamped system 
% 2)critically damped system 
% 3)undamped system 
% 4)overdamped system 
% 5)unstiff system 
% 6)lone mass system

% Created by Brevin Banks 
% Modified 3/7/2023

% Plant_dynamics_select
% Inputs
%   wanted_plants - an array of numbers 1 to 6 for which plants/systems to return
%   figure_on - boolean to decide whether or not to plot the step response
%   of all the plants/systems
% Outputs
%   des_plants - an array that contains the transfer functions for each
%   plant specified by wanted_plants
function des_plants = Plant_dynamics_select(wanted_plants,figure_on)


if nargin~=2 % if figure_on not chosen don't open figure
    figure_on=false;
end
if nargin==0 % if no specific plant chosen return all plants
    wanted_plants=[1,2,3,4,5,6];
end

s = tf('s');
t=50; %s; time interval for simulating system

%System 1 Under Damped
m = 10;
b = 5;
k = 1;
sys1 = tf([1],[m b k]);

%System 2 Critically Damped
m = 10;
b = 2*0.3162*m;
k = 1;
sys2 = tf([1],[m b k]);

%System 3 UnDamped
m = 10;
b = 0;
k = 1;
sys3 = tf([1],[m b k]);

%System 4 Over Damped
m = 10;
b = 10;
k = 1;
sys4 = tf([1],[m b k]);

%System 5 No Stiffness Critically Damped
m = 10;
b = 2*0.3162*m;
k = 0;
sys5 = tf([1],[m b k]);

%System 6 No Stiffness or Damping mass system (double integrator)
m = 10;
b = 0;
k = 0;
sys6 = tf([1],[m b k]);

% Figure plotting
if figure_on == true
    % Plot for position of the systems
    figure('name','Step Response of a Mass Spring Damper For Position')
    subplot(3,2,1)
    step(sys1,t)
    title('Step for Underdamped')
    
    subplot(3,2,2)
    step(sys2,t)
    title('Step for Critically')
    
    subplot(3,2,3)
    step(sys3,t)
    title('Step for Undamped')
    
    subplot(3,2,4)
    step(sys4,t)
    title('Step for Over damped')
    
    subplot(3,2,5)
    step(sys5,t)
    title('Step for Damped W/O Stiff')
    
    subplot(3,2,6)
    step(sys6,t)
    title('Step for W/O Stiff Or Damp (double int)')
    
    % Plot for velocity of the systems
    figure('name','Step Response of a Mass Spring Damper For Velocity')
    subplot(3,2,1)
    step(sys1*s,t,'r')
    title('Step for Underdamped')
    
    subplot(3,2,2)
    step(sys2*s,t,'r')
    title('Step for Critically')
    
    subplot(3,2,3)
    step(sys3*s,t,'r')
    title('Step for Undamped')
    
    subplot(3,2,4)
    step(sys4*s,t,'r')
    title('Step for Over damped')
    
    subplot(3,2,5)
    step(sys5*s,t,'r')
    title('Step for Damped W/O Stiff')
    
    subplot(3,2,6)
    step(sys6*s,t,'r')
    title('Step for W/O Stiff Or Damp (double int)')
end
%   Grab the desired Plants
plants = [sys1,sys2,sys3,sys4,sys5,sys6];

for k = 1:length(wanted_plants)
    des_plants(k) = plants(wanted_plants(k));
end

end



