## Pendulum Waves Animation in MATLAB

July 1st, 2011 | Categories: just for fun, math software, matlab, physics | Tags:

The Pendulum Waves video is awesome and the system has been simulated in Mathematica (twice), Maple and probably every other programming language by now.  During a bout of insomnia I used the Mathematica code written by Matt Henderson as inspiration and made a simple MATLAB version.  Here’s the video

Here’s the code.

freqs  = 5:15;
num = numel(freqs);
lengths = 1./sqrt(freqs);
piover6 = pi/6;

figure
axis([-0.3 0.3 -0.5 0]);
axis off;
org=zeros(size(freqs));
xpos=zeros(size(freqs));
ypos=zeros(size(freqs));
pendula =  line([org;org],[org;org],'LineWidth',1,'Marker','.','MarkerSize',25 ...
,'Color',[0 0 0],'visible','off' );

% Open the avi file
vidObj = VideoWriter('pendula_wave.avi');
open(vidObj);

count =0;
for t=0:0.001:1
count=count+1;
omegas = 2*pi*freqs*t;
xpos = sin(piover6*cos(omegas)).*lengths;
ypos = -cos(piover6*cos(omegas)).*lengths;

for i=1:num
set(pendula(i),'visible','on');
set(pendula(i),'XData',[0 xpos(i)]);
set(pendula(i),'YData',[0 ypos(i)]);
drawnow
end
currFrame = getframe;
writeVideo(vidObj,currFrame)

F(i) = getframe;
end

% Close the file.
close(vidObj);
1. Nice!
One annoying thing since writing my code though is that it should be length = 1/freq^2
I didn’t rearrange the formula for lengths and frequencies correctly. Still makes a cool animation.
M

2. Because everyone is doing them, I also did one:
http://shuisman.com/?p=1370

3. very nice it is too. Cheers Sander

4. ??? Undefined function or method ‘VideoWriter’ for input arguments of type ‘char’.

Error in ==> Untitled5 at 16
vidObj = VideoWriter(‘pendula_wave.avi’);

what could be??

5. What version of MATLAB are you using?

6. MATLAB R2009a

7. That explains it then. The VideoWriter function was introduced in 2010b

8. thanks

9. i want to save my movie as ‘avi’ file, i working with R2009a too. How do I do that?
I wrote :”myVideo = VideoWriter(‘C:\….\palletmapping.avi’);”
And I received an error notice:
??? Undefined function or method ‘VideoWriter’ for input arguments of type ‘char’.
thanks!!

10. would this work for python?

11. Im having problem in line 11 pendula=line….
Any one plz guide me