disp(' ')
disp(' free.m ')
disp(' ver 1.4  December 15, 2011 ')
disp(' by Tom Irvine  Email: tomirvine@aol.com ')
disp(' ')
disp(' This program calculates the free vibration of a  ')
disp(' single-degree-of-freedom system. ')
disp(' ')
%
clear omegan;
clear omegad;
clear fn;
clear damp;
clear N;
clear t;
clear x;
clear v;
%
disp(' select units')
iunits=input(' 1=English  2=metric ');
%
if(iunits==1)
    disp(' ')
    v0 = input(' input initial velocity (in/sec) ');
    disp(' ')
    d0 = input(' input initial displacement (in) ');
else
    disp(' ')
    v0 = input(' input initial velocity (m/sec) ');
    disp(' ')
    d0 = input(' input initial displacement (m) ');    
end
%
disp(' ')
fn=input(' Enter the natural frequency (Hz)  ');
%
disp(' ')
idamp=input(' Enter damping format:  1= damping ratio   2= Q  ');	
%
disp(' ')
if(idamp==1)
    damp=input(' Enter damping ratio (typically 0.05) ');
else
    Q=input(' Enter the amplification factor (typically Q=10) ');
    damp=1./(2.*Q);
end
%
omegan=2.*pi*fn;
omegad=omegan*sqrt(1.-damp^2);
domegan=damp*omegan;
%
%   State Space Solution
%
period=1/fn;
dt=period/64;
sr=1/dt;
dur=12*period;
N=dur/dt;
%
t=linspace(0,dur,N);
t=t';
%
t1=-domegan*t;
t2=omegad*t;
%
x=zeros(N,1);
v=zeros(N,1);
%
if(damp<1)
%
    clear c;
    domegan=damp*omegan;
    c=(v0+domegan*d0)/omegad;
%
    x=exp(t1).*( d0*cos(t2) + c*sin(t2));
    v=-domegan*x + omegad*exp(t1).*( -d0*sin(t2) + c*cos(t2));
%
end
%
if(damp==1)
    x=exp(t1).*(d0+(v0+omegan.*d0).*t);
    v=-domegan*x + exp(t1).*(v0+omegan.*d0);
end
%
if(damp>1)
%    
    t3=omegan*t;
    dr=sqrt(damp^2-1);
    C=1/(2*omegan*dr);
    alpha=-damp+dr;
    beta=-damp-dr;
%    
    A= v0+omegan*d0*(-beta);
    B=-v0+omegan*d0*(alpha);
%    
    x=C*(A*exp(alpha*t3)+B*exp(beta*t3));
    v=C*omegan*(A*alpha*exp(alpha*t3)+B*beta*exp(beta*t3));
%    
end
%
disp(' ');
disp(' plot displacement?');
k=input(' 1=yes  2=no ');
if(k==1)
figure(1);
    plot(t,x);
    grid on;
    if(iunits==1)
        ylabel('Displacement (in)');
    else
        ylabel('Displacement (m)');
    end
    xlabel('Time (sec)');
end
%
disp(' ');
disp(' plot velocity?');
k=input(' 1=yes  2=no ');
if(k==1)
    figure(2);
    plot(t,v);
    grid on;   
    if(iunits==1)
        ylabel('Velocity (in/sec)');
    else
        ylabel('Velocity (m/sec)');
    end
    xlabel('Time (sec)');
end
%
disp(' ');
disp(' plot acceleration?');
k=input(' 1=yes  2=no ');
if(k==1)
    figure(3);
    clear a;
    a= -(2.*domegan)*v -(omegan^2)*x;
    if(iunits==1)
        a=a/386.;
    end     
    plot(t,a);
    grid on;  
    if(iunits==1)
        ylabel('Accel (G)');
    else
        ylabel('Accel (m/sec^2)');
    end
    xlabel('Time (sec)');
end
%
disp(' ');
disp(' plot phase portraits?');
k=input(' 1=yes  2=no ');
if(k==1)
    figure(4);
    plot(x,v);
    grid on;
    if(iunits==1)
        ylabel('Velocity (in/sec)');
        xlabel('Displacement (in)');       
    else
        ylabel('Velocity (m/sec)');
        xlabel('Displacement (m)');        
    end
%
    figure(5);
    plot(x,a);
    grid on;    
    if(iunits==1)
        ylabel('Accel (G)');
        xlabel('Displacement (in)');       
    else
        ylabel('Accel (m/sec^2)');
        xlabel('Displacement (m)');        
    end
%
end