function f = spring_mass_eq(m,b,k) close all n = [1]; d = [m b k]; [A,B,C,D] = tf2ss(n,d); sys = ss(A,B,C,D); t = 0:0.15:10; u = zeros(1,length(t)); xo = 0.5*[10 0; 0 10; -10 0; 0 -10]; p1 = subplot(121); plot(0,0,'o','MarkerSize',2,'MarkerFaceColor','b') hold on axis([-10 10 -10 10]) axis square set(gca,'FontSize',12,'FontWeight','b') xlabel('Position') ylabel('Velocity') title('Phase Portrait') p2 = subplot(122); plot(0,0); hold on axis([0 10 -15 15]) set(gca,'FontSize',12,'FontWeight','b') xlabel('Time (s)') ylabel('Position') title('Zero Input Response') disp('hit any key to continue') pause for j = 1:length(xo) [Y, T, X] = lsim(sys,u,t,xo(j,:)); for i = 1:length(X) axes(p1) %plot(X(i,1),X(i,2),'ro','MarkerFaceColor','r') %hold on pph(j) = plot(X(1:i,1),X(1:i,2),'-','LineWidth',2); %set(gca,'Xlim',[-10 10],'Ylim',[-10 10]) %axis square drawnow %hold off axes(p2) zirh(j) = plot(T(1:i),Y(1:i),'-','LineWidth',2); drawnow %pause(0.01) end set(pph(j),'Color',[0.8 0.8 0.8]); set(zirh(j),'Color',[0.8 0.8 0.8]); end