% Evaluate the displacement of an mass-spring system %clc; clear; global k m C k = 3; % stiffness [kg /s^2] m = 2; % inertia [m/s^2] C = 0; x0 = 1; % initial displacement [m] v0 = 0; % initial velocity [m/s] t0 = -5; tf = 25; %start and end time [s] %Integrate Mass-Spring System [t,x] = ode45('undamped', [t0,tf], [x0,v0]); xx=x(:,1); vv=x(:,2); w=0.3; h=0.3; d=0.2; for an=1:1:(length(xx)) if mod(an,3)==0 subplot(3,3,[1 4]); plot(xx(1:an),t(1:an),'b-','linewidth', 2); axis([-1 1 -5 25]) hold on plot(xx(an),t(an),'ko','linewidth',6,'markersize',5); hold off ylabel('Displacement, x', 'fontsize', 13 );xlabel('Time (sec)','fontsize', 13 );grid; subplot(3,3,[8 9]); plot(t(1:an),vv(1:an),'r-','linewidth', 2); axis([-5 25 -2 2]) hold on plot(t(an),vv(an),'bo','linewidth',6,'markersize',5); hold off ylabel('Velocity, v','fontsize', 13 );xlabel('Time (sec)','fontsize', 13 );grid; subplot(3,3,7); plot(xx(1:an),vv(1:an),'g-', 'linewidth', 2); axis([-1 1 -2 2]) hold on plot(xx(an),vv(an),'bo','linewidth',6,'markersize',5); hold off ylabel('Velocity','fontsize', 13);xlabel('Displacement','fontsize', 13 );grid; %pause(0.1); pause(0.01) %change the speed of plotting time subplot(3,3,[2 3 5 6]) plot([-1.4 -1.4 1.4],[1.4 0.1 0.1],'k','linewidth',3), axis([-1.5, 1.5,0,1.5]), hold on plot([xx(an)-w/2,xx(an)+w/2],[0.15,0.15],'ko','linewidth',5,'markersize',10) patch([xx(an)-w xx(an)-w xx(an)+w xx(an)+w],[d d+h d+h d],[0 0 0 0],'y') hold off else hi=1; end pause(0.01) %change the speed of plotting time end %patch([1.5 1.5 1.6 1.6],[1.25 1.35 1.35 1.25],[0 0 0 0],'r')