DSC HW 6, William Wedler

Contents

Problem 1

Plot the step response

k = 100;
c = 2;

t = linspace(0, 0.2);
x = (1/k) .* ( 1 - exp(-(k/c).*t));

figure(1);
plot(t, x);
xlabel('Time (s)');
ylabel('Response, x (m)');
grid minor
title('Plot of the step response with k=100 and c=2');

Numerical solution for unit step and unit ramp using step and lsim

t = linspace(0, 0.2);
% Unit step
num = [1];
denom = [1 k/c];
%Transfer Function from analytical solution
system = tf(num, denom);

figure(2);
[step_response, t] = step(system, t);
subplot(2, 1, 1);
plot(t,step_response);
title('Numerical solution to a step input using step()');
xlabel('Time (s)');
ylabel('Response, x (m)');

u_ramp = t;
[ramp_response, t] = lsim(system, u_ramp, t);
subplot(2, 1, 2);
plot(t, ramp_response);
title('Numerical solution to a ramp input using lsim()');
xlabel('Time (s)');
ylabel('Response, x (m)');

% Use a larger time interval
t = linspace(0, 10);
% Unit step
num = [1];
denom = [1 k/c];
%Transfer Function from analytical solution
system = tf(num, denom);

figure(3);
[step_response, t] = step(system, t);
subplot(2, 1, 1);
plot(t,step_response);
title('Numerical solution to a step input using step() ');
xlabel('Time (s)');
ylabel('Response, x (m)');

u_ramp = t;
[ramp_response, t] = lsim(system, u_ramp, t);
subplot(2, 1, 2);
plot(t, ramp_response);
title('Numerical solution to a ramp input using lsim()');
xlabel('Time (s)');
ylabel('Response, x (m)');

Numericall solution using Simulink

figure(4);
% Step Response
k = 100;
c = 2;
sim('question1_step');
subplot(2, 1, 1);
plot(tout, step_resp);
title('Numerical solution to a step input using Simulink');
xlabel('Time (s)');
ylabel('Response, x (m)');

% Ramp Response
k = 100;
c = 2;
sim('question1_ramp');
subplot(2, 1, 2);
plot(tout, ramp_resp);
title('Numerical solution to a ramp input using Simulink');
xlabel('Time (s)');
ylabel('Response, x (m)');
Warning: Using a default value of 0.2 for maximum step size.  The simulation step size will be limited to be less than this value.  You can disable this diagnostic by setting 'Automatic solver parameter selection' diagnostic to 'none' in the Diagnostics page of the configuration parameters dialog.