Is there something I am missing. No matter what I do I can't get all four animations to run at the same time. If I comment a spring and Pendulum then it runs properly. Thanks for the help.

3 views (last 30 days)
if true
% % Simulation of Spring Pendulum
clear ;
clc;
% Properties of Pendulum (Can be altered)
g = 9.81 ; % Acceleration due to gravity
M = 2 ; % Mass of the pendulum
L = 1 ; % Length of the Pendulum
K = 5 ; % Spring Constant
% Initial Boundary Conditions (Can be altered)
r = 3 ; % Extension Length
rdot = 1. ;
Phi = 0.1 ; % Position
Phidot = 0.1; % Velocity
duration = 60; % Duration of the Simulation
fps = 10; % Frames per second
%movie = true; % true if wanted to save animation as avi file
movie = false ; % false if only want to view animation
arrow = true ; % Shows the direction of phase plane plot
%arrow = false ; % Will not show the direction of phase plane plot
interval = [0, duration]; % Time span
ivp=[r ;rdot ;Phi ;Phidot ;g ;M ;L ; K]; % Initial value's for the problem
% Simulation of Simple Pendulum
Animation(ivp,duration,fps,movie,arrow);
ANIMATION FILE:
if true
%
function Animation(ivp,duration,fps,movie,arrow)
%--------------------------------------------------------------------------
% To Animate the Spring Pendulum, Equation is solved by MATLAB ode45, and
% running a loop over the time step; for every time step the position
% coordinates of the pendulum are updated.Phase plane plots of the spring
% and pendulum are plotted
%--------------------------------------------------------------------------
nframes = duration*fps; % Number of Frames
% Solving the Equation using ode45
sol=ode45(@Equation,[0 duration], ivp);
t = linspace(0,duration,nframes);
X = deval(sol,t);
% Spring Parameters
r = X(1,:)';
rdot = X(2,:)' ;
% Pendulum Parameters
Phi = X(3,:)';
Phidot = X(4,:)' ;
L = X(7,:)' ;
L = L(1) ;
% Figure
h = figure(1);
clf(h);
set(h,'name','The Spring Pendulum','numbertitle','off','Color','w') ;
% Plot for Time vs Velocity
subplot(222) ;
h1 = plot(t(1),rdot(1),'LineWidth',1,'Color','r') ;
maxt = duration ;
mint = 0 ;
maxrdot = max(rdot) ;
minrdot = min(rdot) ;
axis([mint maxt minrdot maxrdot]) ;
xlabel('t') ;ylabel('v') ;
set(get(gca,'YLabel'),'Rotation',0.0)
grid on ;
title('Spring Velocity vs Time','Color','r')
%Plot for Time vs R
subplot(221) ;
h3 = plot(t(1),r(1),'LineWidth',1,'Color','m') ;
maxt = duration ;
mint = 0 ;
maxr = max(r) ;
minr = min(r) ;
axis([mint maxt minr maxr]) ;
xlabel('t') ;ylabel('r') ;
set(get(gca,'YLabel'),'Rotation',0.0)
grid on ;
title('Spring Length vs Time graph','Color','m')
%Plot Time Vs Phi
subplot(223) ;
h4 = plot(t(1),Phi(1),'LineWidth',1,'Color','b') ;
mint = 0 ;
maxt = duration ;
minPhi = min(Phi) ;
maxPhi = max(Phi) ;
axis([mint maxt minPhi maxPhi]) ;
xlabel('t') ;ylabel('\phi') ;
set(get(gca,'YLabel'),'Rotation',0.0)
grid on ;
title('Pendulum Position vs Time','Color','b');
% Plot Time Vs Phidot
subplot(224) ;
h2 = plot(t(1),Phidot(1),'LineWidth',1,'Color','gr') ;
mint = 0 ;
maxt = duration ;
minPhidot = min(Phidot) ;
maxPhidot = max(Phidot) ;
axis([mint maxt minPhidot maxPhidot]) ;
xlabel('t') ;ylabel('\omega') ;
set(get(gca,'YLabel'),'Rotation',0.0)
grid on ;
title('Pendulum Phase Plane','Color','gr');
% Animation of Spring Pendulum starts
for i=1:length(t)-1
%Spring vs Time Plot
spring(i,:) = [t(i) r(i)] ;
set(h3,'XData',spring(:,1),'YData',spring(:,2));
drawnow;
% Pendulum Position Vs Time
pendulum(i,:) = [t(i) Phi(i)] ;
set(h4,'XData',pendulum(:,1),'YData',pendulum(:,2));
drawnow;
% Spring Time Vs Velocity
spring(i,:) = [t(i) rdot(i)] ;
set(h1,'XData',spring(:,1),'YData',spring(:,2));
drawnow;
% Pendulum Time vs Omega
pendulum(i,:) = [t(i) Phidot(i)] ;
set(h2,'XData',pendulum(:,1),'YData',pendulum(:,2));
drawnow;
end
if movie == true
msgbox('Please wait animaition being saved') ;
movie2avi(F,'SpringPendulum.avi','compression','Cinepak','fps',fps)
end
end
AND THE EQUATION FILE
if true
% function xx = Equation(t,x)
% Set the input values in order to pass onto ode45
%
n = length(x) ;
r = x(1) ;
rdot = x(2) ;
Phi = x(3) ;
Phidot = x(4) ;
g = x(5) ;
M = x(6) ;
L = x(7) ;
K = x(8) ;
%
wr = sqrt(K/M) ;
wPhi = sqrt(g/(L+r));
%
xx=zeros(n,1);
%
xx(1)= rdot;
xx(2)= (r+L)*Phidot^2+g*cos(Phi)-wr^2*(r) ;
xx(3) = Phidot ;
xx(4) = -2*rdot*Phidot/(L+r)-wPhi^2*sin(Phi) ;
end

Answers (0)

Categories

Find more on Dialog Boxes in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!