disp(' ')
disp(' sinebase_ode45.m ')
disp(' ver 1.2  February 26, 2005 ')
disp(' by Tom Irvine  Email: tomirvine@aol.com ')
disp(' ')
disp(' This program calculates the response of a  ')
disp(' single-degree-of-freedom system to a sinusoidal ')
disp(' base input using the ode45 method. ')
disp(' ')
disp(' The ode45 method is a numerical method.')
disp(' ')
%
clear omegan;
clear omegad;
clear fn;
clear damp;
clear N;
clear t;
clear x;
clear w;
%
disp(' select units')
iunits=input(' 1=English  2=metric ');
%
if(iunits==1)
    disp(' ')
    v0 = input(' input initial relative velocity (in/sec) ');
    disp(' ')
    d0 = input(' input initial relative displacement (in) ');
else
    disp(' ')
    v0 = input(' input initial relative velocity (m/sec) ');
    disp(' ')
    d0 = input(' input initial relative 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
%
disp(' ')
f=input(' Enter the base excitation frequency (Hz)  ');
%
if(iunits==1)
    disp(' ')
    A = input(' input the base acceleration (G) ');
    A=A*386.;
else
    disp(' ')
    A = input(' input the base acceleration (m/sec^2) ');   
end
%
omega=2.*pi*f;
%
omegan=2.*pi*fn;
omegad=omegan*sqrt(1.-damp^2);
%
%   State Space Solution
%
if(fn>f)
   period=1/fn;
else
   period=1./f;
end
%
dt=period/64;
sr=1/dt;
dur=30*period;
N=dur/dt;
%
t=linspace(0,dur,N);
t=t';
%
w(1)=d0;
w(2)=v0;
%
clear b;
clear c;
%
b=-omegan^2;
c=-2.*damp*omegan;
domegan=damp*omegan;
%
[t,w] = ode45(@sinebase_function,t,w,[],[],b,c,A,omega);
%
disp(' ');
disp(' plot relative displacement?');
k=input(' 1=yes  2=no ');
if(k==1)
    figure(1);
    plot(t,w(:,1));
    if(iunits==1)
        ylabel('Relative Displacement (in)');
    else
        ylabel('Relative Displacement (m)');
    end
    xlabel('Time (sec)');
end
%
clear xdd;
xdd=c*w(:,2)+b*w(:,1);
% 
if(iunits==1)
    xdd=xdd/386;
end
%        
disp(' ');
disp(' plot absolute acceleration?');
k=input(' 1=yes  2=no ');
if(k==1)
    figure(2);
    plot(t,xdd);
    if(iunits==1)
        ylabel('Accel (G)');
    else
        ylabel('Accel (m/sec^2)');
    end
    xlabel('Time (sec)');
end
%        
disp(' ');
disp(' plot phase portrait?');
k=input(' 1=yes  2=no ');
if(k==1)
    figure(3);
    plot(w(:,1),xdd);
    if(iunits==1)
        ylabel('Accel (G)');
        xlabel('Relative Displacment (in)');     
    else
        ylabel('Accel (m/sec^2)');
        xlabel('Relative Displacment (m)');       
    end
end
%        
disp(' ');
disp(' plot steady state transfer function magnitude?');
k=input(' 1=yes  2=no ');
if(k==1)
    clear omega;
    clear mag;
    clear phase;
    clear trans;
    omega=linspace(0,  2*pi*fn*10, 1000 );
    figure(4);
 %  
    n1=0; 
    n2=2.*domegan; 
    n3=omegan^2;
    d1=1;
    d2=2.*domegan; 
    d3=omegan^2;
%
    [mag,phase]=bode_transfer_function(n1,n2,n3,d1,d2,d3,omega);
%
    omega=omega/(2.*pi);
    plot( omega,mag);
    grid;
%    
    title('Transfer Function Magnitude');
    if(iunits==1)
        ylabel('Accel (G/G)');     
    else
        ylabel('Accel (m/sec^2)/(m/sec^2)');       
    end   
    xlabel('Frequency (Hz)');    
%
    disp(' plot steady state transfer function phase angle?');
    kk=input(' 1=yes  2=no ');
    if(kk==1)
        figure(5);
        plot( omega,phase);
        grid;
%    
        title(' ');
        ylabel('Phase Angle (deg)');     
        xlabel('Frequency (Hz)');    
    end
end