Skip to content

Instantly share code, notes, and snippets.

@radiosilence
Created December 9, 2009 05:52
Show Gist options
  • Save radiosilence/252294 to your computer and use it in GitHub Desktop.
Save radiosilence/252294 to your computer and use it in GitHub Desktop.
function ioviewer( sys )
do = true;
switch sys
case '1'
% System 1: Mass Spring
tra = tf( 5, [1 0 5] );
w = 0.3;
t = 0:0.01:40;
ylbl = 'Output (m)';
ttl = 'Input vs Output System 1, w=0.3';
case '2'
% System 2: Electronic Circuit
tra = tf(1, [40 22 3]);
w = 0.2;
t = 0:0.1:100;
ttl = 'Input vs Output System 2, w=0.2';
ylbl = 'Output ( V )';
case '3a'
% System 3a: Feedback Control Systems
%transfer function = tf(forward, loop);
S = 1; C= tf( [96 56 8], [1 0] ); L = 0; P = tf( 12.5, [12 7 1] ); M = 1;
tra = S * feedback ( C * feedback( P, L ), M );
w = 20;
t = 0:0.001:0.5;
ttl = 'Input vs Output System 3a, w=20';
ylbl = 'Output (units)';
case '3b'
% System 3b: Feedback Control Systems
S=1; C= 0.5 * tf( [6 1], [1 1] ); L = tf( [0.08 0], 1 ); P = tf( 12.5, [12 7 1] ); M = 1;
tra = S * feedback ( C * feedback( P, L ), M );
w = 0.1;
t = 0:0.1:100;
ttl = 'Input vs Output System 3b, w=0.1';
ylbl = 'Output (units)';
case '4'
% System 4: Wien Bridge Oscillator
K = 3; R1 = 10*10^3; R2 = R1; C1 = 10*10^-9; C2 = C1;
tra = K * tf( [ R2*C1 0 ], [ R1*R2*C1*C2 R1*C1+R2*C2+R2*C1 1 ] );
w = 10^4;
t = 0:(1*10^-5):(2*10^-3);
ttl = 'Input vs Output System 4, w=10^4';
ylbl = 'Output (units)';
otherwise
display( 'Sorry, invalid system name.' );
do = false;
end
if( do )
sn = sin( t*w );
[x,t] = lsim( tra, sn, t );
[z,t] = step( tra, t );
subplot( 2, 3, [1:3] );
plot( t, sn, ':', t, x );
title( ttl );
xlabel( 'Time (s) ');
ylabel( ylbl );
legend( 'Input', 'Output' );
subplot( 2, 3, [4:5] );
nyquist( tra );
subplot( 2, 3, 6 );
bode( tra );
end
function modelass( sys )
switch sys
case '1'
% System 1: Mass Spring
sys1_tf = tf( 5, [1 0 5] );
subplot( 1, 2, 1 );
[x, t ] = step( sys1_tf, [0:0.1:4] );
% Time domain of output.
o = 1 - cos( sqrt( 5 ) * t );
plot( t, x, t, o, '*' );
xlabel('Time (s)');
ylabel('Output (m)');
title('Response of System 1 to a Step Input');
legend( 'Simulated Response', 'Theoretical Response', 'Location', 'SouthEast' );
subplot( 1, 2, 2 )
bode( sys1_tf );
case '2'
% System 2: Electronic Circuit
sys2_tf = tf(1, [40 22 3])
subplot( 1, 2, 1 );
[x,t] = step( sys2_tf );
% Time domain
o = 1/3 + ( ( 5/3 )* exp( ( -3/10 ) * t ) ) - 2 * exp( -(1/4) * t );
plot( t, x, t, o, '*' );
title( 'Response of System 2 to a Step Input' );
xlabel( 'Time (s)' );
ylabel( 'Output (V)' );
legend( 'Simulated Response', 'Theoretical Response', 'Location', 'SouthEast' );
subplot( 1, 2, 2 );
bode( sys2_tf );
case '3a'
% System 3a: Feedback Control Systems
%transfer function = tf(forward, loop);
S = 1; C= tf( [96 56 8], [1 0] ); L = 0; P = tf( 12.5, [12 7 1] ); M = 1;
sys3a_tf = S * feedback ( C * feedback( P, L ), M );
subplot( 1, 2, 1 );
[x,t] = step( sys3a_tf );
% Time domain
o = 1-exp(-100*t);
plot( t, x, t, o, '*' );
title( 'Response of System 3a to a Step Input' );
xlabel( 'Time (s)' );
ylabel( 'Output (Units)' );
legend( 'Simulated Response', 'Theoretical Response', 'Location', 'SouthEast' );
subplot( 1, 2, 2 );
bode( sys3a_tf );
case '3b'
% System 3b: Feedback Control Systems
S=1; C= 0.5 * tf( [6 1], [1 1] ); L = tf( [0.08 0], 1 ); P = tf( 12.5, [12 7 1] ); M = 1;
sys3b_tf = S * feedback ( C * feedback( P, L ), M );
% Graphs
subplot( 1, 2, 1 );
[x,t] = step( sys3b_tf );
% Time domain
o = (25/29) * (1 - exp(-(3/4)*t) .* (cos((7/4)*t) + (3/7)*sin((7/4)*t)));
plot( t, x, t, o, '*' );
title( 'Response of System 3b to a Step Input' );
xlabel( 'Time (s)' );
ylabel( 'Output (Units)' );
legend( 'Simulated Response', 'Theoretical Response' );
subplot( 1, 2, 2 );
bode( sys3b_tf );
case '4'
% System 4: Wien Bridge Oscillator
K = 3; R1 = 10*10^3; R2 = R1; C1 = 10*10^-9; C2 = C1;
tra = K * tf( [ R2*C1 0 ], [ R1*R2*C1*C2 R1*C1+R2*C2+R2*C1 1 ] );
minreal(tra);
% Graphs
t = [0:0.000001:0.002];
subplot( 1, 2, 1 );
[x,t] = step( tra, t );
plot( t, x );
title( 'Response of System 4 to a Step Input' );
xlabel( 'Time (s)' );
ylabel( 'Output (V)' );
legend( 'Simulated Response' );
subplot( 1, 2, 2 );
bode( tra );
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment