%% Something to Note %The best way to animate when animating data calculated %using ode45 is not shown here, it's shown in % ProjectileMotionOde45.m %% Animate points/objects (plot plot plot - the bad way) close all; clear all; clc; %Animate square following sinusoid path (center of square follows path) x = linspace(0,2*pi,101); y = sin(x); figure for i = 1:length(x) %find points of vertices of square at current time xsquare = x(i) + [-.5 .5 .5 -.5 -.5]; ysquare = y(i) + [-.5 -.5 .5 .5 -.5]; plot(xsquare,ysquare) axis equal axis([-1 2*pi+1 -2 2]) pause(.01) end %% Animate Line close all; clear all; clc; %create points x = linspace(0,2*pi,501); y = sin(x); figure a = animatedline(x(1),y(1)); axis equal axis([0 2*pi -1 1]) for i =2:length(x) addpoints(a,x(i),y(i)) drawnow end %% Animate points/objects (the good way) close all; clear all; clc; %create path square will travel x = linspace(0,2*pi,501); y = sin(x); %create square vertices xsquare = [-.5 .5 .5 -.5 -.5]; ysquare = [-.5 -.5 .5 .5 -.5]; %Setup figure p = plot(x(1)+xsquare,y(1)+ysquare); axis equal axis([-1 2*pi+1 -2 2]) %Animate for i = 2:length(x) %update line object p.XData = x(i) + xsquare; p.YData = y(i) + ysquare; drawnow end