Skip to content

Instantly share code, notes, and snippets.

@brilliant-ember
Last active April 10, 2020 21:06
Show Gist options
  • Save brilliant-ember/c9de9963b053d51cabdbe9ce8748282c to your computer and use it in GitHub Desktop.
Save brilliant-ember/c9de9963b053d51cabdbe9ce8748282c to your computer and use it in GitHub Desktop.
Plots a system's response, and error response of various inputs namely step, ramp, and parabolic inputs.
# u can change the input type and response by uncommenting
## t the stions of the code that are below each subplot
clear; clc;
pkg load control;
K = 32.3;
s = tf('s');
openLoopTf = K/( s*(s+3)*(s+6));
##openLoopTf = (672*(s+5)) / (s*(s+6)*(s+7)*(s+8));;
## closed loop
## very important to follow this order for the input
## of the feedback function
G_tclosed = feedback(openLoopTf,1);
error_tf = 1/(1+openLoopTf)
input = tf(1,1);
rows = 3;
cols = 3;
u = step(input).'; ## .' to change cols to rows
t = 0:length(u)-1;
subplot(rows,cols,1);
step(input, "m");
title("The step input");
subplot(rows, cols, 2);
ramp(input, "m");
title("Ramp input");
subplot(rows, cols,3);
parabolaInput = 0.5.*u.*t.^2;
plot(t, parabolaInput, "m");
title("Parobolic input");
ylabel("y");
xlabel("Time s");
legend("input");
grid on;
subplot(rows, cols,4);
step(G_tclosed, "r");
title("system step respones");
subplot(rows, cols,5);
ramp(G_tclosed, "r");
title("Ramp respones");
subplot(rows, cols,6)
[y_out, time] = lsim(G_tclosed, parabolaInput, t);
plot(time,y_out, "r");
title("Parabolic response");grid on;
xlabel("Time s");
ylabel("y");
grid on;
subplot(rows, cols,7);
step(error_tf);
title("Error to step input");
grid on;
subplot(rows, cols,8);
rampInput = u.*t;
##y =lsim(G_tclosed, rampInput, t);
##plot(t, y -y');
ramp(error_tf);
title("Error to ramp input");
grid on;
subplot(rows, cols,9);
[y_outError, time] = lsim(error_tf, parabolaInput, t);
#plot(time,y_out-y'); u can also do this
plot(time, y_outError);
title("System error to parabolic response");
xlabel("Time s");
ylabel("y");
grid on
@brilliant-ember
Copy link
Author

If you're using Octave instead of Matlab, make sure to install the controls pkg using pkg install -forge control the load it using pkg load control

@brilliant-ember
Copy link
Author

This is the steady state error, and the system is of type 1 since we have one pole at the origin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment