Animating a pendulum: error in code

Hi guys,
i'm trying to plot the oscillatory motion of a simple pendulum in the form of a GIF or AVI file and i'm trying the following code, but there seems to be an error:
The pendulum generated is not oscillating.
I got a warning when i ran this: Warning: The EraseMode property is no longer supported and will error in a future release.
Also, how do i save the animation as a GIF file?
% A script to animate the motion of the simple pendulum
clc
clear
disp('Specify the initial angle of the pendulum in degrees, e.g., 50')
disp('or press ENTER for the default value.')
theta=input('Initial angular displacement of the pendulum= ');
if isempty(theta)
theta=50
else
theta;
end
theta=theta*pi/180; % convert from degrees to radians
disp('Specify the final time, e.g. tfinal = 25')
disp('or press ENTER for default value.')
tfinal=input('Final time tfinal = ');
if isempty(tfinal)
tfinal=10
else
tfinal;
end
data_init=[0 0; -4 0]; %pendulum length
% rotation matrix
R=[cos(theta) -sin(theta);sin(theta) cos(theta)];
data=R*data_init; % initial pendulum position
bar=line('xdata',[0 data(1)],'ydata',...
[0 data(2)]','linewidth',3,'erase','xor');
mass=line('xdata',data(1),'ydata',data(2),'marker','o',...
'markersize',15,'MarkerFaceColor','b','erase','xor');
hinge=line('xdata',0,'ydata',0,'marker','o',...
'markersize',7,'erase','xor');
axis([-5 5 -5 5])
grid % comment this out if you do like the grid
set(gca,'Fontsize',14)
set(gca,'dataaspectratio',[1 1 1])
box on
dt=0.03; % step−size for solving differential
% equations can be arbitrarily selected
t=0; % initial time
thetadot=0; % initial angular speed
disp('Can maximize the display so you can see the action better,')
disp('then press ENTER.')
disp('If you are happy with the display size, press ENTER.')
pause
% solve the diff eqns using the Euler's method
while(t<tfinal)
t=t+dt;
theta = theta + thetadot*dt;
thetadot=thetadot - 5*sin(theta)*dt; %−0.01*thetadot;
% you can add some friction
R=[cos(theta) -sin(theta);sin(theta) cos(theta)];
datanew=R*data_init;
% change the property values of the bar and hinge objects
set(bar,'xdata',[0 datanew(1)],'ydata',[0 datanew(2)]);
set(mass,'xdata',datanew(1),'ydata',datanew(2));
set(hinge,'xdata',0,'ydata',0)
set(gca,'dataaspectratio',[1 1 1])
drawnow;
end

5 件のコメント

Geoff Hayes
Geoff Hayes 2020 年 11 月 16 日
Kletech - I tried running the code and saw the same warning as you. (Just remove the 'erase','xor' properity from each call to the function line. The pendulum did move for me given the inputs I provided. What were the inputs that you tried such that the pendulum would not move? As for converting the animation into an avi, please see the example from write video data to file.
KLETECH MOTORSPORTS
KLETECH MOTORSPORTS 2020 年 11 月 16 日
i entered the initial angle as pi/2 and the final time as 20 sec..
then i got an image of a pendulm, but it didn't oscillate.
okay. i think i realise the problem. the code accepts values in degrees, not radians, for some reason, and pi/4in degrees is really tiny.
it began to oscillate for an angle=45 deg
any idea how i can make it oscillate more smoothly or faster?
KSSV
KSSV 2020 年 11 月 16 日
KLETECH MOTORSPORTS
KLETECH MOTORSPORTS 2020 年 11 月 16 日
編集済み: KLETECH MOTORSPORTS 2020 年 11 月 16 日
I have tried that as well, but got some errors.
I tried putting the expression for the simple pendulum and the function fie Animation in the same script and running them together, and i got the following errors.
Unrecognized function or variable 'Equation'.
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in Animation1>Animation (line 30)
sol=ode45(@Equation,[0 duration], ivp);
Error in Animation1 (line 19)
Animation(ivp, duration, fps, movie,arrow);
KLETECH MOTORSPORTS
KLETECH MOTORSPORTS 2020 年 11 月 16 日
Is there anyway i can specifically add something to my existing code such that i can convert it to a series of frmes andthen into an AVI or GIF fie? I have read the syntax, but i am unable to understand how to use it in this case.

サインインしてコメントする。

回答 (1 件)

Geoff Hayes
Geoff Hayes 2020 年 11 月 16 日

0 投票

In order to convert to a series of frames and save as an AVI, just do
% open the video writer
v = VideoWriter('pendulum.avi');
open(v);
% solve the diff eqns using the Euler's method
while(t<tfinal)
% your existing code
drawnow;
% get the frame and write to the file
frame = getframe(gcf);
writeVideo(v,frame);
end
% close the video writer
close(v);

9 件のコメント

KLETECH MOTORSPORTS
KLETECH MOTORSPORTS 2020 年 11 月 18 日
編集済み: KLETECH MOTORSPORTS 2020 年 11 月 18 日
Hi, i tried what you suggested like so:
But the pendulum isn't oscillating after adding the code you suggested,
and it's not being saved in any format,
script name : pendmotion4rudrap
% A script to animate the motion of the simple pendulum
clc
clear
disp('Specify the initial angle of the pendulum in degrees, e.g., 50')
disp('or press ENTER for the default value.')
theta=input('Initial angular displacement of the pendulum= ');
if isempty(theta)
theta=50
else
theta;
end
theta=theta*pi/180; % convert from degrees to radians
disp('Specify the final time, e.g. tfinal = 25')
disp('or press ENTER for default value.')
tfinal=input('Final time tfinal = ');
if isempty(tfinal)
tfinal=10
else
tfinal;
end
data_init=[0 0; -4 0]; %pendulum length
% rotation matrix
R=[cos(theta) -sin(theta);sin(theta) cos(theta)];
data=R*data_init; % initial pendulum position
bar=line('xdata',[0 data(1)],'ydata',...
[0 data(2)]','linewidth',3,'erase','xor');
mass=line('xdata',data(1),'ydata',data(2),'marker','o',...
'markersize',15,'MarkerFaceColor','b','erase','xor');
hinge=line('xdata',0,'ydata',0,'marker','o',...
'markersize',7,'erase','xor');
axis([-5 5 -5 5])
grid % comment this out if you do like the grid
set(gca,'Fontsize',14)
set(gca,'dataaspectratio',[1 1 1])
box on
dt=0.03; % step−size for solving differential
% equations can be arbitrarily selected
t=0; % initial time
thetadot=0; % initial angular speed
disp('Can maximize the display so you can see the action better,')
disp('then press ENTER.')
disp('If you are happy with the display size, press ENTER.')
pause
% solve the diff eqns using the Euler's method
while(t<tfinal)
t=t+dt;
theta = theta + thetadot*dt;
thetadot=thetadot - 5*sin(theta)*dt; %−0.01*thetadot;
% you can add some friction
R=[cos(theta) -sin(theta);sin(theta) cos(theta)];
datanew=R*data_init;
% change the property values of the bar and hinge objects
set(bar,'xdata',[0 datanew(1)],'ydata',[0 datanew(2)]);
set(mass,'xdata',datanew(1),'ydata',datanew(2));
set(hinge,'xdata',0,'ydata',0)
set(gca,'dataaspectratio',[1 1 1])
drawnow;
v = VideoWriter('pendulum.avi');
open(v);
% solve the diff eqns using the Euler's method
while(t<tfinal)
% your existing code
drawnow;
% get the frame and write to the file
frame = getframe(gcf);
writeVideo(v,frame);
end
% close the video writer
close(v);
end
Geoff Hayes
Geoff Hayes 2020 年 11 月 18 日
Kletch - the comment I added
% your existing code
was just to indicate that your existing code should go here. I was just trying to make clear what code should be added to save the animation to file. So please replace this comment with your code like
v = VideoWriter('pendulum.avi');
open(v);
% solve the diff eqns using the Euler's method
while(t<tfinal)
t=t+dt;
theta = theta + thetadot*dt;
thetadot=thetadot - 5*sin(theta)*dt; %−0.01*thetadot;
% you can add some friction
R=[cos(theta) -sin(theta);sin(theta) cos(theta)];
datanew=R*data_init;
% change the property values of the bar and hinge objects
set(bar,'xdata',[0 datanew(1)],'ydata',[0 datanew(2)]);
set(mass,'xdata',datanew(1),'ydata',datanew(2));
set(hinge,'xdata',0,'ydata',0)
set(gca,'dataaspectratio',[1 1 1])
drawnow;
% get the frame and write to the file
frame = getframe(gcf);
writeVideo(v,frame);
end
% close the video writer
close(v);
KLETECH MOTORSPORTS
KLETECH MOTORSPORTS 2020 年 11 月 18 日
I tried that, it ran, thependulum did not oscillate, but that could be because my system is slow.
The avi file "pendulum" was saved in my matlab online drive.
however, when i try to open it, it says: Cannot import from an empty input file
and when i downloaded it and tried to play on a video player, i got : This file isn’t playable. That might be because the file type is unsupported, the file extension is incorrect, or the file is corrupt.
KLETECH MOTORSPORTS
KLETECH MOTORSPORTS 2020 年 11 月 18 日
Also, i got unrecognized function or variable "t", referring to the while loop
Geoff Hayes
Geoff Hayes 2020 年 11 月 18 日
編集済み: Geoff Hayes 2020 年 11 月 18 日
Kletech - you may need to post all of your code. The code I've pasted above is only a some of what is neede. I have excluded all of the code that precedes the while loop because it wasn't relevant for where we need to add code to save the animations to video. I wasn't suggesting that you replace all of your code with what I've posted. That may explain why you are getting the error "unrecognized function or variable "t"". This is the full code that worked for me.
function grh2
% A script to animate the motion of the simple pendulum
clc
clear
close all;
disp('Specify the initial angle of the pendulum in degrees, e.g., 50')
disp('or press ENTER for the default value.')
theta=input('Initial angular displacement of the pendulum= ');
if isempty(theta)
theta=50
else
theta;
end
theta=theta*pi/180; % convert from degrees to radians
disp('Specify the final time, e.g. tfinal = 25')
disp('or press ENTER for default value.')
tfinal=input('Final time tfinal = ');
if isempty(tfinal)
tfinal=10
else
tfinal;
end
data_init=[0 0; -4 0]; %pendulum length
% rotation matrix
R=[cos(theta) -sin(theta);sin(theta) cos(theta)];
data=R*data_init; % initial pendulum position
bar=line('xdata',[0 data(1)],'ydata',...
[0 data(2)]','linewidth',3);
mass=line('xdata',data(1),'ydata',data(2),'marker','o',...
'markersize',15,'MarkerFaceColor','b');
hinge=line('xdata',0,'ydata',0,'marker','o',...
'markersize',7);
axis([-5 5 -5 5])
grid % comment this out if you do like the grid
set(gca,'Fontsize',14)
set(gca,'dataaspectratio',[1 1 1])
box on
dt=0.03; % step−size for solving differential
% equations can be arbitrarily selected
t=0; % initial time
thetadot=0; % initial angular speed
disp('Can maximize the display so you can see the action better,')
disp('then press ENTER.')
disp('If you are happy with the display size, press ENTER.')
pause
v = VideoWriter('pendulum4.avi');
open(v);
% solve the diff eqns using the Euler's method
while(t<tfinal)
t=t+dt;
theta = theta + thetadot*dt;
thetadot=thetadot - 5*sin(theta)*dt; %−0.01*thetadot;
% you can add some friction
R=[cos(theta) -sin(theta);sin(theta) cos(theta)];
datanew=R*data_init;
% change the property values of the bar and hinge objects
set(bar,'xdata',[0 datanew(1)],'ydata',[0 datanew(2)]);
set(mass,'xdata',datanew(1),'ydata',datanew(2));
set(hinge,'xdata',0,'ydata',0)
set(gca,'dataaspectratio',[1 1 1])
drawnow;
% get the frame and write to the file
frame = getframe(gcf);
writeVideo(v,frame);
end
% close the video writer
close(v);
end
KLETECH MOTORSPORTS
KLETECH MOTORSPORTS 2020 年 11 月 18 日
Geoff, i tried the code you've posted, and it worked like a charm! I don't know where i went wrong, but i will scrutinize your code and try to figure it out!
a question i had was, what is function grh2? is that a random name?
Thank you for your help! i've been trying to figure this out for a week, and it's only been a month since i started using matlab.
Really grateful.
Geoff Hayes
Geoff Hayes 2020 年 11 月 18 日
Glad that the above code worked. I tend to use functions over scripts, so the function grh2 is just indicates that this code is part of a function named grh2 which is saved to a file named grh2.m.
KLETECH MOTORSPORTS
KLETECH MOTORSPORTS 2020 年 11 月 18 日
I didn't realize you could use functions over scripts. I thought the two had to be used in tandem.
How does only using functions work?
Can a script be used without a function? How are scripts and functions related even
Geoff Hayes
Geoff Hayes 2020 年 11 月 18 日
See scripts vs functions for a discussion on the differences between the two.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeAnimation についてさらに検索

製品

リリース

R2020b

質問済み:

2020 年 11 月 15 日

コメント済み:

2020 年 11 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by