第c题
dt = [0.1, 0.25, 0.35];
t0 = 50;
figure(1);
for k = 1: length(dt)
n = round(t0/dt(k)) + 1;
x = zeros(1, n);
y = zeros(1, n);
x(1) = 1.;
for j = 1: n-1
y(j+1) = y(j) - dt(k) * x(j);
x(j+1) = x(j) + dt(k) * y(j);
end
t = (0: n-1) * dt(k);
es = cos(t);
subplot(length(dt), 1, k);
plot(t, x, 'linewidth', 2);
hold on;
plot(t, es, 'r', 'linewidth', 2);
legend({['dt=', num2str(dt(k)), 's'], 'exact'});
xlabel('Time (s)');
ylabel('x');
set(gca, 'fontname', 'times new roman', 'fontsize', 20);
end |