EE342.01: MATLAB M-FILE FOR COMPARING EXACT SOLUTION TO RECURSIVE SOLUTION

MATLAB M-File example5.m:
% Filename:  example5.m
%
% Description: This m-file computes and plots the exact step response of
%              a dc motor as well as the recursive step response found
%              from the differene equation approximation.
%

figure(1)                               % put plots in figure 1
clf;					% clear figure 1 and memory
clear;

% Plot continuous time response of motor to Va(t)=15u(t)

tmax = 0.03;                            % amount of time to run simulation
KT = 4.07;				% motor torque constant
L = 0.00156;                            % motor inductance
J = 0.0618;                             % motor rotor inertia
R = 1.89;                               % motor resistance
B = 6.68;                               % motor friction
Kb = 3.01;                              % motor back emf constant

num = [KT];				% numerator of motor TF
den = [L*J R*J+B*L B*R+KT*Kb];		% denominator of motor TF

printsys(num,den,'s');			% display H(s)

t = 0:0.00005:0.03;			% plot response to Va=15u(t)
y = step(num*15,den,t);
plot(t,y);
hold on;
grid;


% Plot discrete-time response of motor to Va[n]=15u[n] w/T=0.0005

T = 0.0005;                             % sample interval
wprev1 = 0;                             % w[1] = wdot(0)*T + w(0)
wprev2 = 0;                             % w[0] = w(0) = 0

for n=2:tmax/T,                         % loop over n
  
  if n>=2,                              % va[n]=15u[n] => va[n-2]=15u[n-2]
    vaprev2 = 15;
  else
    vaprev2 = 0;
  end
  
  w = (2-(R*J+B*L)*T/(L*J))*wprev1 + ...
      ((R*J+B*L)*T/(L*J) - 1 - (B*R+KT*Kb)*T*T/(L*J))*wprev2 + ...
      KT*T*T/(L*J)*vaprev2;             % DC motor difference eqn
  
  wprev2 = wprev1;                      % store values for next iteration
  wprev1 = w;
  
  nTvec(n-1) = n*T;                     % store nT,w into vectors for plotting
  wvec(n-1) = w;
  
end

nTvec = [0*T 1*T nTvec];                % put IC's into nT,w vectors
wvec  = [0 0 wvec];

plot(nTvec, wvec, 'o')                  % plot discrete-time approximation
legend('w(t) vs. t', 'w[n] vs. nT w/T=0.0005');
xlabel('time (sec)');
ylabel('angular velocity (krpm)');
title('Motor Step Response for 15V step input');
hold off;
MATLAB Plots Generated from Three Separate Runs: