Saturday, May 14, 2011

Swinging the pendulum!!!

Watching the dell dock's swinging application challenged me as I saw matlab's icon swinging like pendulum.
I decided to make an object swing like simple pendulum on matlab.

Sounds easy, isn't it??? That's what I thought before starting to work..
But the pursuit took me from coordinate geometry to physics to differential equations and back to matlab...

First what I did was, used pythagoras theorem to compute the row coordinate while varying the column coordinate LINEARLY, and colored the neighborhood of equation describing the line segment joining the fixed point and computed coordinates. I thought it would work perfectly fine, But it looked strange, nowhere near the motion of a pendulum.

Here is that code.... check this out!!!



clear;
a=zeros(150,400,3);
len=100;
t=0;
p=-len;
cir=zeros(150,400,3);
mov=zeros(150,400,3,2*len+1);
for i=200-len:200+len-2
    a=cir;
    p=p+1;          
% linear increment of column coordinate.
    j=round(sqrt((len*len)-(p*p)));    
% pythagoras theorem.
    cir(j:j+1,i:i+1,2)=123;
   
for r=2:j
       
for s=200-len:200+len
           
if((s-200)*j-r*(i-200)<100 && (s-200)*j-r*(i-200)>-100) 
               a(r-1:r+1,s-1:s+1,3)=255;  
% coloring neighborhood 
               a(r-1,s-1,2)=123;    
      % of line segment
               t=t+1;
           
end
        end
    end

    mov(1:101,1:400,1:3,i-200+len+1)=a(1:101,1:400,1:3);
 
end
mov1=immovie(mov);
implay(mov1,20);
clear



Now the aim was to match the motion with the motion of pendulum.
The challenge lied in incrementing time linearly and varying the column coordinate with respect to time in accordance with the force equations of a pendulum.

Here is the solution--- 
 

 Free body diagram of the problem--


           
           
 where-

             'L' is length of the thread.
             'Φ' is angle subtended by thread.
             'm' is the mass of the pendulum.
             'T' is the tension in the string.
             'g' is the acceleration due to gravity.

             'x' is the column coordinate.
             'y' is the row coordinate.

Variables of concern--    


           

 it represent displacement, velocity and acceleration in x and y directions resp.

Solving the force equation from the free body diagram. 


where
             'ay' is acceleration downwards
             'ax' is acceleration toward right.  
 
Substituting the values and solving, we get solution(the first equation in image) via Lagrange's equation.


Finding the constants of the differential equation by putting the initial conditions of velocity and displacement. 

clear;
a=zeros(150,400,3);
len=100;
t=0;
cir=zeros(150,400,3);
mov=zeros(150,400,3,2*len+1);
i=0;
j=round(sqrt((len*len)-(i*i)));
q=zeros(1,200);

while i<=len-2
    i=round(len*sin(-1.4303*sin(t/68+(11/7))))
    t=t+1;
    a=cir;

    j=round(sqrt((len*len)-(i*i)));
    cir(j:j+1,i+200:i+201,2)=123;
   
for(r=2:j)
       
for s=200-len:200+len
           
if((s-200)*j-r*(i)<75 && (s-200)*j-r*(i)>-75)
              a(r-1:r+1,s-1:s+1,3)=255;
              a(r-1,s-1,2)=123;
   
        end
        end
    end

    mov(1:101,1:400,1:3,t)=a(1:101,1:400,1:3);

end
mov1=immovie(mov);
implay(mov1,50);