%
% Filename: example6.m
%
% Description: M-file to compare exact and numerical solutions
% of a CLTI differential equation.
%
clear; % clear matlab memory
tmax = 10.0; % amount of time to run simulation
t = 0:0.01:tmax; % CT time values
y = 0.5 + 1.26*exp(-0.75*t).*cos(1.2*t + 1.98); % Exact Solution
plot(t,y); xlabel('t '); % Plot exact (CT) solution
ylabel('y(t)'); title('Solutions to Differential Equation');
hold on;
T = 0.2; % Sample time
N = tmax/T; % Final DT value
yprev2 = 0; % y[0]
yprev1 = -T; % y[1]
nvec = [0 1]; % Store ICs for plotting
yvec = [yprev2 yprev1];
for n = 2:N,
y = (2 - 3*T/2)*yprev1 + (3*T/2 - 2*T*T - 1)*yprev2 + T*T; % Diff. Eqn.
yprev2 = yprev1;
yprev1 = y;
nvec(n+1) = n;
yvec(n+1) = y;
end;
plot(nvec*T, yvec, 'o');
hold off;
gtext('--- y(t); ooo y[n] w/T=0.2');
MATLAB Plots Generated: