||
各位同学:这个利用matlab做电影的技术不作要求。但是求解运动方程的算法作要求。
程序如下
% damped harmonic oscillator;2016.12.20
clear all; close all; clc;
omega = 1;
gamma = 0.05;
dt = 0.02;
N = 1e4; step = 10;
Tmax = dt*N;
tlist = dt*(0:N-1);
xlist = zeros(2, N);
x0 = [0.5;0.5]; % initial position and velocity
xlist(:,1) = x0;
M = [0, 1; -omega^2, -gamma];
[VV,DD] = eig(M);
invVV = inv(VV);
dd = diag(DD);
h1= plot(nan);
hold on
plot(tlist, zeros(1,N),':');
hold on
xlim([0 Tmax])
ylim([-1 1])
xlabel('t','fontsize',20)
ylabel('x','fontsize',20)
str = strcat('gamma=',num2str(gamma));
title(str,'fontsize',20)
for s = 2: N
time = dt*(s-1);
xlist(:,s) = VV*diag(exp(time*dd))*invVV*x0;
if mod(s,step)==0
set(h1,'XData',tlist(1,1:s),'YData',xlist(1,1:s))
drawnow
pause(0.00)
mov(s/step) = getframe;
end
end
str=strcat('Omega=',num2str(omega),'_gamma=',num2str(gamma),'.avi');
movie2avi(mov, str, 'compression', 'None','fps',15);
电影如下(横轴时间,纵轴位置):
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-23 04:23
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社