%
%   
disp(' ');
disp(' SDOF_sine.m   ver 1.0   August 28, 2004 ');
disp(' by Tom Irvine  Email: tomirvine@aol.com ');
disp(' ');
disp(' This program generates an animation of a single-degree-of-freedom ');
disp(' system subjected to sinusoidal base excitation. ');
%
clear all
nframes = 20;
%
%
refresh;
    w=1.;
    h=1.;
    hb=0.2;
    x=3.5;
%    
    X(1)=4.;
    X(2)=4.;
    X(3)=4.-0.15;  
    X(4)=4.+0.15;
    X(5)=4.-0.15;
    X(6)=4.+0.15; 
    X(7)=4.-0.15;
    X(8)=4.+0.15;
    X(9)=4.;
    X(10)=4.;    
%
    disp(' ');
    fn=input(' Enter natural frequency (Hz) ');
    disp(' ');
    f =input(' Enter excitation frequency (Hz) ');	
    disp(' ');
    damp =input(' Enter the damping ratio (typically 0.05) ');	   
%  
    rho=f/fn;
    f=1.;
%
    aa=1.+(2.*damp*rho)^2;
    bb=(1.-rho^2)^2+(2.*damp*rho)^2;
    trans=sqrt(aa/bb);
%    
    aa=2.*damp*rho^3;
    bb=1.+rho^2*(4.*damp^2-1);
    phase=atan(aa/bb);
%
    A=0.2;
%    
    rectangle('Position',[0,0,8,8],'FaceColor','w','EraseMode','background'); 
for k=1:nframes
    T=1.;
    dt=1./nframes;
    t=(k-1)*dt;
%
    y(k) =4.0 +  A*trans*sin(2*pi*t*f-phase);
    yb(k)=1.0 +  A*sin(2*pi*t*f);
%
    h1=rectangle('Position',[x,y(k),w,h],'FaceColor',[0 0.2 0.2],'EraseMode','xor'); 
    h2=rectangle('Position',[x,yb(k),w,hb],'FaceColor',[0.2 0.2 0.2],'EraseMode','xor');
    d=y(k)-yb(k)-hb;
    delta=d/8.;
    Y(1)=yb(k)+hb;
    Y(2)=Y(1)+delta;
    Y(3)=Y(2)+delta/2.;
    Y(4)=Y(3)+delta;
    Y(5)=Y(4)+delta;
    Y(6)=Y(5)+delta;
    Y(7)=Y(6)+delta; 
    Y(8)=Y(7)+delta;
    Y(9)=Y(8)+delta/2.;    
    Y(10)=Y(9)+delta;         
%    
    h3=line(X,Y,'EraseMode','xor','Color',[0 0.2 0.5]);
    drawnow;
    pause(0.5);
    M(k)=getframe;
    delete(h1);
    delete(h2);
    delete(h3);
end    
movie(M,12);
%
%
%   disp(' ');
%   disp(' Enter the output movie filename: ');
%   output_filename = input(' ','s');
%
% movie2avi(M,output_filename,'quality',100);


