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. July 1st, 2011 at 19:56
    Reply | Quote | #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. July 9th, 2011 at 22:31
    Reply | Quote | #2

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

  3. July 10th, 2011 at 08:23
    Reply | Quote | #3

    very nice it is too. Cheers Sander

  4. Eze Garcia
    August 21st, 2011 at 19:01
    Reply | Quote | #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. August 22nd, 2011 at 06:37
    Reply | Quote | #5

    What version of MATLAB are you using?

  6. Eze Garcia
    August 23rd, 2011 at 00:10
    Reply | Quote | #6

    MATLAB R2009a

  7. August 23rd, 2011 at 09:35
    Reply | Quote | #7

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

  8. Eze Garcia
    August 24th, 2011 at 12:42
    Reply | Quote | #8

    thanks

  9. hadas
    September 5th, 2011 at 12:31
    Reply | Quote | #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. Raul
    April 27th, 2012 at 01:21

    would this work for python?

  11. Iqra
    May 20th, 2017 at 15:26

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