How to animate plots after ode45

I've created code to simulate a 2 mass 3 spring system using ode45. I'd like to animate the plots, but I'm pretty new at matlab and having trouble with some commands. Any help would be greatly appreciated. Thanks. Here's my code:
clc; clear; close all;
x0 = .1; dx0 = 0; x1 = 0; dx1 = 0;
a0 = [x0; dx0; x1; dx1];
[t,a] = ode45(@springthreemasstwo, [0 6*pi], a0);
x = a(:,1); y = a(:,3);
% plot(t,x,t,y) % xlabel('Time'); % ylabel('Displacement');
h1 = animatedline; h2 = animatedline; axis(t,x,t,y);
%set(gca, 'XLim', [0 6*pi], 'YLim', [-.1 .1]);
for i = 1: length(t); addpoints(h1,x(i),y(i)); drawnow hold on addpoints(h2,x(i),y(i)); drawnow hold off end
function [da] = springthreemasstwo(t,a) m = 1; %kg k = 1; %N/m omega = sqrt(k/m);
% Set input values to useful names x0 = a(1); dx0 = a(2); x1 = a(3); dx1 = a(4);
da = [0;0;0;0]; %make da a column vector
% Define spring force
Fx = -k*x0-k*(x0-x1); Fx1 = -k*x1-k*(x1-x0);
da(1) = dx0; da(2) = Fx/m ; da(3) = dx1; da(4) = Fx1/m; end

Answers (0)

Asked:

on 27 Apr 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!