% Define constants and variables n = 3; Lx = 1; hbar = 1; m = 1; k = @(n) n*pi/Lx; w = hbar*k(n)^2/2*m; A = sqrt(2/Lx); x = linspace(0,Lx,200); t = linspace(0,10,500); % Define the position and time functions psi = @(x,n) A.*sin(k(n)*x); phi = @(t) exp(-1i.*w.*t); % Make a .gif of the plots filename = '1DParticle.gif'; for m = 1:3:length(t) plot(x, real(psi(x,n)*phi(t(m)))) xlim([0 1]) ylim([-1.5 1.5]) title('$\psi(x)$ for $n = 3$','interpreter','latex') drawnow frame = getframe(1); im = frame2im(frame); [Q,map] = rgb2ind(im,256); if m == 1; imwrite(Q,map,filename,'gif','LoopCount',Inf,'DelayTime',1); else imwrite(Q,map,filename,'gif','WriteMode','append','DelayTime',1); end end