function sdof(option, arg1, arg2)

%************************************************************************
%*									*
%*      STRUCTURAL DYNAMICS TUTORIAL					*
%*      FREE/FORCED VIBRATION OF SINGLE DEGREE OF FREEDOM SYSTEMS	*
%*									*
%*      This MATLAB program was written by Gilberto Mosqueda		*
%*      working under the supervision of Prof. Eduardo Kausel at the	*
%*      Department of Civil Engineeering				*
%*      Massachusetts Institute of Technology				*
%*									*
%*      Last Edited: April 4, 1998					*
%*									*
%*      This program written for use with MATLAB will grahically 	*
%*      demonstrate the free and forced vibrations of a single 		*
%*      degree of freedom systems.  The user can set the properties 	*
%*      of the system (mass, damping, stiffness) or the loading, and 	*
%*      instantly obtain the response.  Previous plots remain on 	*
%*      the screen until the CLEAR button is pressed.  Other plots 	*
%*      such as the transfer function, the impulse response function,	*
%*      and the fft of periodic loading are also displayed.		*
%*									*
%*      To run:								*
%*         load MATLAB							*
%*         change directory to where this file (sdof.m) is located	*
%*         issue the command    sdof     (without the m extension)	*
%*									*
%*      Please report any bugs or problems to				*
%*      E. Kausel, MIT, Room 1-271, Cambridge, MA 02139			*
%*      kausel@mit.edu							*
%*									*
%*      Copyright E. Kausel, MIT, Cambridge, MA				*
%************************************************************************

global PROP_HAND LOAD_HAND PCOLOR;  % Decalre some global variables

if nargin<1                         %
   option = 'initialize';
end;

clr_plts = ['r'; 'b'; 'm'; 'c'; 'g'; 'y']; %Plotting colors (in order)

%************************************************************************
%*               INITIALIZE: CREATE WINDOW WITH GUI TOOLS  	      	*
%************************************************************************

if strcmp(option, 'initialize')
  % Create window and format with plot and controls

  trans_fig=findobj('type', 'figure', 'Name', 'TRANSFER');
  if ~isempty(trans_fig)
    close(trans_fig)
  end
  dyna_fig = figure( ...
      'Name', 'TRANSFER', ...
      'Unit', 'pixels' ,...
      'UserData', 0 ,... 
      'Position', [725  25 300 400] ,...
      'NumberTitle', 'off', ...
      'Colormap', []);
  load_fig=findobj('type', 'figure', 'Name', 'LOAD');
  if ~isempty(load_fig)
    close(load_fig)
  end
  load_fig = figure( ...
      'Name', 'LOAD', ...
      'Unit', 'pixels' ,...
      'Position', [725  450 300 275] ,...
      'NumberTitle', 'off', ...
      'Colormap', []);
  data_fig = figure( ...
      'Name', 'Vibration of SDOF', ...
      'Unit', 'pixels' ,...
      'Position', [25 25 690 700] ,...
      'UserData', [load_fig dyna_fig],...
      'NumberTitle', 'off', ...
      'Colormap', []);
  set(gca, 'Position', [0.13 0.62  0.775 0.343902])
  back_frm = uicontrol(data_fig, ...
      'Style', 'frame', ...
      'Units', 'normalized',...
      'BackGroundColor', [.4 .4 .4],...
      'Position', [ .02 .02 .96 .48 ]);
  %subplot(2, 1, 1);
  grid;, hold on;
  xlabel('Time');
  ylabel('Displacement');
  %  Initialize variables
  M =  5;, K =  1000;, D =  5;
  Y0 = 0;, V0 = 0;, P0=0;, OM=0;
  TIME = 10;, PCOLOR = 1;
  F=sqrt(K/M)/(2*pi);
  delT= 1/(F*16);
  nyquist = ceil(1/(2*delT));
  
  %  ********************CREATE MENUS FOR PLOTTING  ********************
  
  option_menu = uimenu(data_fig,...
      'Position', 1,...
      'Label', 'Plot Options');
  load_fun = uimenu(option_menu,...
      'Label', 'Forcing Function',...
      'CallBack', 'sdof(''plotting'', ''load'')');
  tran_fun = uimenu(option_menu,...
      'Label', 'Transfer and Phase ',...
      'CallBack', 'sdof(''plotting'', ''transfer'')');
  resp_fft = uimenu(option_menu,...
      'Label', 'Response FFT',...
      'CallBack', 'sdof(''plotting'',''resp_fft'',get(gcbo, ''UserData''))');
  load_fft = uimenu(option_menu,...
      'Label', 'Load FFT',...
      'CallBack', 'sdof(''plotting'',''load_fft'',get(gcbo, ''UserData''))');
  impu_res = uimenu(option_menu,...
      'Label', 'Impulse Response',...
      'CallBack', 'sdof(''plotting'', ''impulse'')');
  
  %  ********************CREATE GUI'S FOR PROPERTIES********************
  % System Properties
  back_frm = uicontrol(data_fig, ...
      'Style', 'frame', ...
      'Units', 'normalized',...
      'Position', [ .03 .22 .3 .27 ]);
  prop_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', 'SYSTEM PROPERTIES', ...
      'Position', [ .05 .42 .25 .06]);
  % Editable text button for mass
  mass_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', ' Mass', ...
      'Position', [ .05 .38 .15 .05]);
  mass_inp = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'String', M, ...
      'Position', [ .2 .38 .1 .05],...
      'CallBack', [...
	'if str2num(get(gco,''String''))<=0,',...
	'errordlg(''ERROR- Mass must be positive''),',...
	'end,',...
	'h=get(gco,''UserData'');, ', ...
	'm=str2num(get(gco, ''String''));,',...
	'k=str2num(get(h(1),''String''));,',...
	'f=sqrt(k/m)/(2*pi);,',...
        'set(h(2),''String'',f);,',...
        'dt = 1/(f*16);',...
        'set(h(3),''String'',dt);'] );
  % Editable text button for Stiffness
  stif_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', ' Stiffness', ...
      'Position', [ .05 .33 .15 .05]);
  stif_inp = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'String', K, ...
      'Position', [ .2 .33 .1 .05 ],...
      'CallBack', [...
	'if str2num(get(gco,''String'')) <= 0, ', ...
	'errordlg(''ERROR- Stiffness must be positive'');,',...
	'else,',...
	'h=get(gco,''UserData'');, ', ...
	'm=str2num(get(h(1),''String''));,',...
	'k=str2num(get(gco, ''String''));,',...
	'f=sqrt(k/m)/(2*pi);,',...
	'set(h(2),''String'',f);,',...
	'dt = 1/(f*16);,',...
	'set(h(3),''String'',dt);,',...
	'end'] );
  % Editable text button for Damping Ratio
  damp_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', ' % Damping ', ...
      'Position', [.05 .28 .15 .05  ]);
  damp_inp = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'UserData', dyna_fig, ... 
      'String', D, ...
      'Position', [ .2 .28 .1 .05 ],...
      'CallBack', [...
	'if str2num(get(gco,''String'')) < 0, ', ...
	'errordlg(''ERROR- Damping Ratio must be positive''),',...
	'end'] );
  freq_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', ' Freq.(Hz) ', ...
      'Position', [.05 .23 .15 .05  ]);
  freq_inp = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'String', F, ...
      'Position', [ .2 .23 .1 .05 ],...
      'CallBack', [...
	'h=get(gco,''UserData'');, ', ...
	'm=str2num(get(h(1),''String''));,',...
	'f=str2num(get(gco ,''String''));,',...
	'k=m*(2*pi*f)^2;,',...
	'set(h(2),''String'',k);,',...
	'dt = 1/(f*16);,',...
	'set(h(3),''String'',dt);'] );
  
  %  *************** CREATE GUI'S FOR INITIAL CONDITIONS ***************
  % Initial Conditions
  back_frm = uicontrol(data_fig, ...
      'Style', 'frame', ...
      'Units', 'normalized',...
      'Position', [ .03 .03 .3 .18 ]);
  init_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', 'INITIAL CONDITIONS', ...
      'Position', [.05 .15 .25 .05]);
  % Editable text button for Initial Displacement
  idis_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', ' Displ. ', ...
      'Position', [.09 .09 .1 .05 ]);
  idis_inp = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'String', Y0, ...
      'Position', [ .2 .09 .1 .05 ]);
  % Editable text button for Initial Velocity
  ivel_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', ' Velocity ', ...
      'Position', [.05 .04 .15 .05 ]);
  ivel_inp = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'String', V0, ...
      'Position', [ .2 .04 .1 .05]);
  setic_bt = uicontrol(data_fig, ...
      'Style', 'push', ...
      'Units', 'normalized',...
      'UserData', [idis_inp ivel_inp], ...
      'String', 'Reset', ...
      'Position', [ .04 .12 .07 .04 ],...
      'CallBack', [...
	'h=get(gco,''UserData'');,',...
	'set(h,''String'', 0.0)';] );
  
  %  ********************CREATE GUI'S FOR LOADING***********************
  % Set default options for loading
  back_frm = uicontrol(data_fig, ...
      'Style', 'frame', ...
      'Units', 'normalized',...
      'Position', [ .34 .03 .31 .46 ]);
  lloc_pop = uicontrol(data_fig, ...
      'Style', 'popupmenu', ...
      'Units', 'normalized',...
      'UserData', dyna_fig, ...
      'String', 'FREE VIBRATION|FORCE ON MASS|SUPPORT MOTION', ...
      'Position', [ .37 .42 .26 .06], ...
      'CallBack', [...
	'sdof(''load'', popupstr(gco), 1);'] );
  load_pop = uicontrol(data_fig, ...
      'Style', 'popupmenu', ...
      'Units', 'normalized',...
      'String', 'Sinusoidal|Point Data|File', ...
      'Visible', 'off',...
      'Position', [ .37 .36 .26 .05], ...
      'CallBack', 'sdof(''load'', popupstr(gco), 0);');         %*******
  % --------------------CONSTANT-------------------------
  % Editable text button for Loading Magnitude
  lmag_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', ' Magitude ', ...
      'Visible', 'off',...
      'Position', [.35 .30 .15 .05  ]);
  lmag_inp = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'Visible', 'off',...
      'Position', [ .48 .30 .1 .05 ]);
  % --------------------HARMONIC-------------------------
  % Editable text button for Loading Frequency
  lfre_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', ' Freq.(Hz) ', ...
      'Visible', 'off',...
      'Position', [.35 .25 .15 .05  ]);
  lfre_inp = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'Visible', 'off',...
      'Position', [ .48 .25 .1 .05 ]);
  sine_rad = uicontrol(data_fig, ...
      'Style', 'radio', ...
      'Units', 'normalized',...
      'Visible', 'off',...
      'Value', 1,...
      'String', 'Sine',...
      'Position', [ .42 .20 .16 .04 ], ...
      'CallBack', [...
	'set(gco, ''Value'', 1);',...
	'set(get(gco, ''UserData''), ''Value'', 0);']);
  csin_rad = uicontrol(data_fig, ...
      'Style', 'radio', ...
      'Units', 'normalized',...
      'Visible', 'off',...
      'String', 'Cosine',... 
      'Position', [ .42 .16 .16 .04 ],...
      'CallBack', [...
	'set(gco, ''Value'', 1);',...
	'set(get(gco, ''UserData''), ''Value'', 0);']);
  set(sine_rad, 'UserData', csin_rad);
  set(csin_rad, 'UserData', sine_rad);
  % --------------------START/END TIME-----------------
  % Editable text button for Inititial Loading Time
  tini_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', ' To ', ...
      'Visible', 'off',...
      'Position', [.35 .2 .15 .05  ]);
  tini_inp = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'Visible', 'off',...
      'Position', [ .48 .2 .1 .05 ]);
  % Editable text button for Final Loading Time
  tfin_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', ' Tf ', ...
      'Visible', 'off',...
      'Position', [.35 .15 .15 .05  ]);
  tfin_inp = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'Visible', 'off',...
      'Position', [ .48 .15 .1 .05 ]);
  % -----------------STEP LOADING------------------------
  % Editable text button for initial and final loading
  imag_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', ' Po ', ...
      'Visible', 'off',...
      'Position', [.35 .30 .15 .05  ]);
  imag_inp = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'Visible', 'off',...
      'Position', [ .48 .30 .1 .05 ]);
  fmag_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', ' Pf ', ...
      'Visible', 'off',...
      'Position', [.35 .25 .15 .05  ]);
  fmag_inp = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'Visible', 'off',...
      'Position', [ .48 .25 .1 .05 ]);
  
  % -----------------POINT DATA -------------------------
  pd_hdl = zeros(7,2);
  
  pd_hdl(1, 1) = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', 'Time', ...
      'Visible', 'off',...
      'Position', [.42 .30 .1 .05  ]);
  pd_hdl(1, 2) = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', 'Mag.', ...
      'Visible', 'off',...
      'Position', [.52 .30 .1 .05  ]);
  
  for i=1:3
    pd_hdl(4+i, 1) = uicontrol(data_fig, ...
	'Style', 'text', ...
	'Units', 'normalized',...
	'String', i, ...
	'Visible', 'off',...
	'Position', [.37 .30-i*0.05 .05 .05  ]);
  end
  
  pd_hdl(2, 1) = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'String', 0, ...
      'Visible', 'off',...
      'Position', [.42 .25  .1 .05 ],...
      'CallBack', [...
	'h=get(gco, ''UserData'');,',...
	't1 = str2num(get(gco ,''String''));,',...
	't2 = str2num(get(h(1),''String''));,',...
	't3 = str2num(get(h(2),''String''));,',...
	'if t1 < 0,',...
	'errordlg(''ERROR- Time must be greater than zero.''),',...
	'set(gco, ''String'',0),',...
	'end,',...
	'if t1 > t2,',...
	'set(h(1), ''String'',t1),',...
	'end,',...
	'if t1 > t3,',...
	'set(h(2), ''String'',t1),',...
	'end,'] );
  pd_hdl(3, 1) = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'String', 0, ...
      'Visible', 'off',...
      'Position', [.42 .20 .1 .05 ],...
      'CallBack', [...
	'h=get(gco, ''UserData'');,',...
	't2 = str2num(get(gco ,''String''));,',...
	't1 = str2num(get(h(1),''String''));,',...
	't3 = str2num(get(h(2),''String''));,',...
	'if t2 < 0,',...
	'errordlg(''ERROR- Time must be greater than zero.''),',...
	'set(gco, ''String'',0),',...
	'end,',...
	'if t2 < t1,',...
	'set(h(1), ''String'',t2),',...
	'end,',...
	'if t2 > t3,',...
	'set(h(2), ''String'',t2),',...
	'end,'] );
  pd_hdl(4, 1) = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'String', 0, ...
      'Visible', 'off',...
      'Position', [.42 .15 .1 .05 ],...
      'CallBack', [...
	'h=get(gco, ''UserData'');,',...
	't3 = str2num(get(gco ,''String''));,',...
	't1 = str2num(get(h(1),''String''));,',...
	't2 = str2num(get(h(2),''String''));,',...
	'if t3 < 0,',...
	'errordlg(''ERROR- Time must be greater than zero.''),',...
	'set(gco, ''String'',0),',...
	'end,',...
	'if t3 < t1,',...
	'set(h(1), ''String'',t3),',...
	'end,',...
	'if t3 < t2,',...
	'set(h(2), ''String'',t3),',...
	'end,'] );
  
  for i=1:3
    pd_hdl(i+1, 2) = uicontrol(data_fig, ...
	'Style', 'edit', ...
	'Units', 'normalized',...
	'String', 0, ...
	'Visible', 'off',...
	'Position', [.52 .30-i*0.05 .1 .05  ]);
  end
  
  % ----------------FILE DATA LOADING--------------------
  % Get loading from file
  file_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', 'FILE NAME', ...
      'Visible', 'off',...
      'Position', [ .4 .3 .2 .05]);
  file_inp = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'Visible', 'off',...
      'Position', [ .4 .26 .2 .05 ],...
      'CallBack','sdof(''file_plot'',get(gco,''String''), gco);' );
  fhelp_bt = uicontrol(data_fig, ...
      'Style', 'push', ...
      'Units', 'normalized',...
      'String', 'Format Help...', ...
      'Visible', 'off',...
      'Position', [ .42 .15 .16 .05 ],...
      'CallBack','sdof(''button'',''helpfile'');' );
  
  file_btn  = uicontrol(data_fig, ...
      'Style', 'push', ...
      'Units', 'normalized',...
      'String', 'Open File...', ...
      'UserData', file_inp, ...
      'Visible', 'off',...
      'Position', [ .42 .2 .16 .05 ],...
      'CallBack', [...
	'filename=uigetfile(''*.*'', ''DATA FILE'', 100, 100);', ...
	'file_inp=get(gco, ''UserData'');',...
	'set(file_inp, ''String'', filename);',...
	'sdof(''file_plot'',filename, file_inp);' ]);
  % ---------------- PERIODIC LOADING --------------------
  perd_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'Visible', 'off',...
      'String', 'Period', ...
      'Position', [.35 .04 .15 .05  ]);
  perd_inp  = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'String', '1', ...
      'Visible', 'off',...
      'Position', [ .48 .04 .1 .05 ],...
      'CallBack', [...
	'if str2num(get(gco,''String''))< 0,',...
	'errordlg(''ERROR- Period must be greater than zero.''),',...
	'set(gco, ''String'',0),',...
	'end'] );
  ltyp_pop  = uicontrol(data_fig, ...
      'Style', 'popupmenu', ...
      'Units', 'normalized',...
      'UserData', [load_pop],...
      'String', 'Transient|Periodic|Harmonic', ...
      'Visible', 'off',...
      'Position', [ .4 .10 .2 .04 ],...
      'CallBack', [...
	'h=get(gco, ''UserData'');', ...
	'sdof(''load'', popupstr(h));']);
  
  %  ********************CREATE GUI'S FOR GRAPH*************************
  % Graph Properties
  back_frm = uicontrol(data_fig, ...
      'Style', 'frame', ...
      'Units', 'normalized',...
      'Position', [ .66 .15 .3 .34 ]);
  prop_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', 'DISPLAY PARAMETERS', ...
      'Position', [ .69 .41 .25 .06]);
  % Time lenght of plot input
  labletxt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', 'Max. Time: ', ...
      'Position', [ .67 .37 .15 .05]);
  time_inp = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'String', TIME , ...
      'Position', [ .82 .37 .1 .05],...
      'CallBack', [...
	'if str2num(get(gco,''String''))<=0,',...
	'errordlg(''ERROR- Display Time must be positive''),',...
	'end']);

  % Time step for calculations
  delt_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', ' Time Step: ', ...
      'Position', [ .67 .32 .15 .05]);
  delt_inp = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'String', delT , ...
      'Position', [ .82 .32 .1 .05],...
      'CallBack', [...
	'if str2num(get(gco,''String''))<=0,',...
	'errordlg(''ERROR- Time Step must be positive''),',...
	'end']);
  
  % Display frequency for Fourier data
  minf_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', ' Min Freq. ', ...
      'Position', [ .67 .26 .15 .05]);
  minf_inp = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'String', 0 , ...
      'Position', [ .82 .26 .1 .05],...
      'CallBack', [...
	'value = str2num(get(gco,''String''));, main = gcf;,',...
	'maxf = str2num(get( get(gco, ''UserData''), ''String''));,',...
	'if value<=0,',...
	'errordlg(''ERROR- Display Frequency must be positive''),',...
	'elseif value >= maxf,',...
	'set(gcbo, ''String'', 0);,',...
	'errordlg(''ERROR- Min. Frequency must be less than Max.''),',...
	'else,',...
	'f_fig=findobj(''type'', ''figure'', ''Name'', ''RESPONSE FFT'');,'....
	'if ~isempty(f_fig),',...
	'figure(f_fig);, set(gca, ''XLim'', [ value maxf] );,',...
	'end;,',...
	'f_fig=findobj(''type'', ''figure'', ''Name'', ''LOAD FFT'');,'....
	'if ~isempty(f_fig);,',...
	'figure(f_fig);, set(gca, ''XLim'', [ value maxf] );,',...
	'end;,',...
	'figure(main);, end;'] );
  maxf_txt = uicontrol(data_fig, ...
      'Style', 'text', ...
      'Units', 'normalized',...
      'String', ' Max Freq. ', ...
      'Position', [ .67 .21 .15 .05]);
  maxf_inp = uicontrol(data_fig, ...
      'Style', 'edit', ...
      'Units', 'normalized',...
      'String', nyquist, ...
      'UserData', minf_inp,...
      'Position', [ .82 .21 .1 .05],...
      'CallBack', [...
	'value = str2num(get(gco,''String''));, main = gcf;,',...
	'minf = str2num(get( get(gco, ''UserData''), ''String''));,',...
	'if value<=minf,',...
	'set(gco, ''String'', minf+1);,',...
	'errordlg(''ERROR- Max. Frequency must be greater than min''),',...
	'else,',...
	'f_fig=findobj(''type'', ''figure'', ''Name'', ''RESPONSE FFT'');,'....
	'if ~isempty(f_fig),',...
	'figure(f_fig);, set(gca, ''XLim'', [minf value] );,',...
	'end;,',...
	'f_fig=findobj(''type'', ''figure'', ''Name'', ''LOAD FFT'');,'....
	'if ~isempty(f_fig);,',...
	'figure(f_fig);, set(gca, ''XLim'', [minf value] );,',...
	'end;,',...
	'figure(main);, end;'] );
  deft_btn = uicontrol(data_fig, ...
      'Style', 'push', ...
      'Units', 'normalized',...
      'UserData', [minf_inp maxf_inp delt_inp time_inp freq_inp],... 
      'String', 'Reset Defaults', ...
      'Position', [ .71 .16 .19 .04 ],...
      'CallBack', 'sdof(''button'', ''default'', get(gco, ''UserData'') )');  
  %  ********************CREATE CONTROL BUTTONS*************************
  % Graph Button [ .68 .21 .25 .06 ],
  graph_bt = uicontrol(data_fig, ...
      'Style', 'push', ...
      'Units', 'normalized',...
      'String', 'Plot Response', ...
      'Position', [ .2 .51 .25 .06 ],...
      'CallBack', 'sdof(''plot'')');
  % Clear Button [ .68 .14 .25 .06 ]
  clear_bt = uicontrol(data_fig, ...
      'Style', 'push', ...
      'Units', 'normalized',...
      'String', 'Clear Screen', ...
      'Position', [ .55 .51 .25 .06 ],...
      'CallBack', 'sdof(''button'', ''clear'')');
  % Close Button
  help_btn = uicontrol(data_fig, ...
      'Style', 'push', ...
      'Units', 'normalized',...
      'String', 'Help/Info', ...
      'Position', [ .68 .09 .25 .05 ],...
      'CallBack', 'sdof(''button'', ''help'')');
  % Close Button
  close_bt = uicontrol(data_fig, ...
      'Style', 'push', ...
      'Units', 'normalized',...
      'String', 'Close', ...
      'Position', [ .68 .03 .25 .05 ],...
      'CallBack', 'sdof(''button'', ''close'')');
  % Get Points with Mouse
  
  %  ******************** SAVE OBJECT HANDLES ***************************
  %Set UserData for objects
  set(mass_inp, 'UserData', [stif_inp freq_inp delt_inp time_inp]);
  set(stif_inp, 'UserData', [mass_inp freq_inp delt_inp time_inp]);
  set(freq_inp, 'UserData', [mass_inp stif_inp delt_inp time_inp]);
  set(delt_inp, 'UserData', [mass_inp stif_inp freq_inp time_inp]);
  set(time_inp, 'UserData', [mass_inp stif_inp freq_inp delt_inp]);

  set(pd_hdl(2, 1), 'UserData', [pd_hdl(3, 1) pd_hdl(4, 1)]);
  set(pd_hdl(3, 1), 'UserData', [pd_hdl(2, 1) pd_hdl(4, 1)]);
  set(pd_hdl(4, 1), 'UserData', [pd_hdl(2, 1) pd_hdl(3, 1)]);
  
  set(resp_fft, 'UserData', [minf_inp maxf_inp]);
  set(load_fft, 'UserData', [minf_inp maxf_inp]);
  set(option_menu, 'UserData', [minf_inp maxf_inp]);
  set(minf_inp, 'UserData', maxf_inp);
  
  LOAD_HAND = [lmag_txt  lmag_inp
    lfre_txt  lfre_inp
    tini_txt  tini_inp
    tfin_txt  tfin_inp
    imag_txt  imag_inp
    fmag_txt  fmag_inp
    file_txt  file_inp
    file_btn  ltyp_pop
    perd_txt  perd_inp
    sine_rad  csin_rad
    lloc_pop  load_pop
    fhelp_bt  0.0      ];
  LOAD_HAND = [ LOAD_HAND ; pd_hdl ];
  
  PROP_HAND = [mass_inp stif_inp damp_inp idis_inp ivel_inp load_pop, ...
	time_inp delt_inp];
  
%************************************************************************
%*         CHANGE LOAD OPTION: DISPLAY PROPER INPUT OPTIONS   	      	*
%************************************************************************
  
elseif strcmp(option, 'load')   %change in load type, reset load display
  load_type=arg1;
  resp_type=popupstr(LOAD_HAND(8,2));
  current=get(LOAD_HAND(8,2), 'Value');
  %For change in load condition, set to specified laoding
  if nargin<3                         %
    arg2=0;
  end;
  
  if arg2==1 & ~strcmp(load_type, 'FREE VIBRATION')
    load_type = popupstr(LOAD_HAND(11,2));
  end
  
  % Remove Harmonic option when changing from sinusoidal
  if ~strcmp(load_type, 'Sinusoidal') & strcmp(resp_type, 'Harmonic')
    current=1;
  end;
  if strcmp(load_type, 'Sinusoidal')
    set(LOAD_HAND(8,2), 'String','Transient|Periodic|Harmonic' );
  else
    set(LOAD_HAND(8,2), 'String','Transient|Periodic' );
  end;
  set(LOAD_HAND(8,2), 'Value', current);
  resp_type=popupstr(LOAD_HAND(8,2));
  
  % Select proper displays for the type of loading selected
  set(LOAD_HAND(1,:), 'Visible','off');
  set(LOAD_HAND(2,:), 'Visible','off');
  set(LOAD_HAND(3,:), 'Visible','off');
  set(LOAD_HAND(4,:), 'Visible','off');
  set(LOAD_HAND(5,:), 'Visible','off');
  set(LOAD_HAND(6,:), 'Visible','off');
  set(LOAD_HAND(7,:), 'Visible','off');   
  set(LOAD_HAND(8,1), 'Visible','off');
  set(LOAD_HAND(8,2), 'Visible','on' );
  set(LOAD_HAND(9,:), 'Visible','off'); 
  set(LOAD_HAND(10,:), 'Visible','off');
  set(LOAD_HAND(11,2), 'Visible','on');
  set(LOAD_HAND(12,1), 'Visible','off');
  set(LOAD_HAND(13:19,:), 'Visible','off');
  
  if strcmp(load_type, 'FREE VIBRATION')
    set(LOAD_HAND(8,2), 'Visible','off');
    set(LOAD_HAND(11,2), 'Visible','off');
  elseif strcmp(load_type, 'Point Data')
    set(LOAD_HAND(13:19,:), 'Visible','on');
  elseif strcmp(load_type, 'Step Load')
    set(LOAD_HAND(1,:), 'Visible','on' );
    set(LOAD_HAND(3,:), 'Visible','on' );
    set(LOAD_HAND(4,:), 'Visible','on' );
  elseif strcmp(load_type, 'Sinusoidal')
    set(LOAD_HAND(1,:), 'Visible','on' );
    set(LOAD_HAND(2,:), 'Visible','on' );
    set(LOAD_HAND(3,:), 'Visible','on' );
    set(LOAD_HAND(4,:), 'Visible','on' );
  elseif strcmp(load_type, 'Triangular')
    set(LOAD_HAND(3,:), 'Visible','on' );
    set(LOAD_HAND(4,:), 'Visible','on' );
    set(LOAD_HAND(5,:), 'Visible','on' );
    set(LOAD_HAND(6,:), 'Visible','on' );
  elseif strcmp(load_type, 'File')
    set(LOAD_HAND(7,:), 'Visible','on' );
    set(LOAD_HAND(8,1), 'Visible','on' );
    set(LOAD_HAND(12,1),'Visible','on' );
  end;
  
  % Set period display if response is periodic
  if strcmp(resp_type, 'Periodic') & ~strcmp(load_type, 'Free Vibration')
    set(LOAD_HAND(9,:), 'Visible','on' );
  elseif strcmp(resp_type, 'Harmonic')
    set(LOAD_HAND(3,:), 'Visible','off');
    set(LOAD_HAND(4,:), 'Visible','off');
    set(LOAD_HAND(10,:), 'Visible','on');
  end;
  
%************************************************************************
%*      	PLOT GRAPH: READ INPUT AND PLOT GRAPH		      	*
%************************************************************************

elseif strcmp(option, 'plot')   %plot response with selected options
  setptr(gcf, 'watch');
  
  M   =str2num(get(PROP_HAND(1),'String'));, sdof('check', M, 0);
  K   =str2num(get(PROP_HAND(2),'String'));, sdof('check', K, 0);
  D   =str2num(get(PROP_HAND(3),'String'));, sdof('check', D, -.001);
  Y0  =str2num(get(PROP_HAND(4),'String'));
  V0  =str2num(get(PROP_HAND(5),'String'));
  TIME=str2num(get(PROP_HAND(7),'String'));, sdof('check', TIME, 0);
  delT=str2num(get(PROP_HAND(8),'String'));, sdof('check', delT, 0);
  applied_l = popupstr(LOAD_HAND(11,1));
  load_type = popupstr(PROP_HAND(6));
  load_perd = popupstr(LOAD_HAND(8,2));
  
  if strcmp(applied_l, 'FREE VIBRATION')
    load_type = 'Free Vibration';
  end
  
  %    Calculate system properties
  D= 0.01*D;
  C =D*2*sqrt(K*M);
  W = sqrt(K/M);
  Wd=W*sqrt(1-D^2);
  T=(2*pi)/W;
  %    Set matrices for using lsim function
  A1=[ 0 1; -K/M -C/M];
  A2=[ 0 ; 1/M];
  A3=[1  0];
  A4=0;
  
  if (delT <= 0)
    delT=T/16;
  end;
  t1 = (0:delT:TIME);
  s=size(t1,2);
  true_s=s;
  if strcmp(load_perd, 'Periodic')
    P =str2num(get(LOAD_HAND(9,2),'String'));, sdof('check', P, .001);
    p=round(P/delT);  % make sure p is greater than one 
    sdof('check', p, 1);
    if s<=p, s=p+1;, t1 = (0:delT:P+delT);, end;
  end;
  %*************FORM LOAD VECTOR***************************
  if strcmp(load_type, 'Free Vibration')
    LOAD = zeros(1,s);
    
  elseif strcmp(load_type, 'Sinusoidal')
    P0  =str2num(get(LOAD_HAND(1,2),'String'));
    OM  =str2num(get(LOAD_HAND(2,2),'String'));, sdof('check', OM, 0);
    if ~strcmp(load_perd, 'Harmonic')
      To  =str2num(get(LOAD_HAND(3,2),'String'));, sdof('check', To, 0);
      Tf  =str2num(get(LOAD_HAND(4,2),'String'));, sdof('check', Tf, To);
    else
      To = 0;, Tf = 0;
    end
    OM  = OM*(2*pi);
    i=round(To/delT)+1;, f=round(Tf/delT);
    if (f>s), f=s;, end;
    LOAD = zeros(1,s);
    LOAD(i:f) = P0*sin(OM*t1(1:(f-i+1)));
    
  elseif strcmp(load_type, 'Point Data')
    data = zeros(2,3);
    for i=1:3
      for j=1:2
	data(j,i) = str2num(get(LOAD_HAND(13+i,j),'String'));
      end
    end
    LOAD = zeros(1,size(t1,2));
    time = data(1,:);
    load = data(2,:);
    npts = 3;
    i=1; Po=0;, Pf=0;
    for j=1:(npts)
      f=round(time(j)/delT)+1;
      ft =f;
      if (f>s), f=s;, end;
      Po=Pf;
      LOAD(i)= Po;
      Pf= load(j);
      for c=(i+1):f, LOAD(c)= LOAD(c-1)+(Pf-Po)/(ft-i);, end;
      i=f;
    end
    
  elseif strcmp(load_type, 'File')
    data = get(LOAD_HAND(7,2),'UserData');
    LOAD = zeros(1,size(t1,2));
    time = data(1,:);
    load = data(2,:);
    npts = size(time,2);
    i=1; Po=0;, Pf=0;
    for j=1:(npts)
      f=round(time(j)/delT)+1;
      ft = f;
      if (f>s), f=s;, end;
      Po=Pf;
      LOAD(i)= Po;
      Pf= load(j);
      for c=(i+1):f, LOAD(c)= LOAD(c-1)+(Pf-Po)/(ft-i);, end;
      i=f;
    end
  end
  
  %For support motion, acc, multiply by mass to obtain rel dis.
  if strcmp(applied_l, 'TEST')
    LOAD=LOAD*(-M);
  end;
  
  %************FOR PERIODIC LOADING, CYCLE LOADING*******************
  if (~strcmp(load_type, 'Free Vibration') & strcmp(load_perd, 'Periodic'))
    Load_P = LOAD(1:p);
    cycles=floor(s/p);, a=1;, b=1;
    if cycles<2, b=p;, end;
    if cycles < 100	
      for c=2:cycles
	a=a+p;, b=a+p-1;
	LOAD(a:b)=Load_P;
      end
    else
      errordlg('Please select a larger period');
      error;
    end
    for c=(b+1):(s-1)
      LOAD(c)=Load_P(c-b);
    end
    %SET INITIAL CONDITIONS FOR PERIODIC LOADING
    if strcmp(applied_l, 'SUPPORT MOTION')
      n=p+1;
      t1_P=t1(1:n);,L_P=LOAD(1:n);,L_P(1)=0;
      G1=zeros(1,n);, G2=zeros(1,n);
      C1=W*W/Wd*(1-2*D^2);
      C2=W*2*D;
      C3=-W*W*D*W/Wd*(3-4*D*D);
      C4=W*W*(1-4*D*D);
      for c=1:n
	G1(c)=exp(-D*W*t1(c))*(C1*sin(Wd*t1(c))+C2*cos(Wd*t1(c)));
	G2(c)=exp(-D*W*t1(c))*(C3*sin(Wd*t1(c))+C4*cos(Wd*t1(c)));
      end;
      Disp=conv(L_P, G1)*delT;, Y=Disp(n);
      Velo=conv(L_P, G2)*delT;, V=Velo(n);
      s1=exp(-D*W*P)*sin(Wd*P);
      c1=exp(-D*W*P)*cos(Wd*P);
      mat1=[ 1-c1-D*(W/Wd)*s1		-s1/Wd
	W^2*s1/Wd 	1-c1+D*(W/Wd)*s1];
      initc=mat1\[Y; V];
      Y0=initc(1);, V0=initc(2);
      set(PROP_HAND(4),'String', Y0);
      set(PROP_HAND(5),'String', V0);
      
    else
      t1_P=t1(1:p+1);,L_P=LOAD(1:p+1);,L_P(1)=0;
      [D1,V1] = lsim(A1, A2, A3, A4, L_P, t1_P, [0, 0]);
      
      s1=exp(-D*W*P)*sin(Wd*P);
      c1=exp(-D*W*P)*cos(Wd*P);
      mat1=[ 1-c1-D*(W/Wd)*s1		-s1/Wd
	W^2*s1/Wd 	1-c1+D*(W/Wd)*s1];
      initc=mat1\[V1(p+1,1); V1(p+1,2)];
      Y0=initc(1);, V0=initc(2);
      set(PROP_HAND(4),'String', Y0);
      set(PROP_HAND(5),'String', V0);
    end
  end
  
  %**************SINUSOIDAL- PHASE AND TRANSFER******************
  if strcmp(load_type, 'Sinusoidal')
    r=OM/W;
    
    if strcmp(applied_l,'SUPPORT MOTION')
      m=1/sqrt( (1-r^2)^2+(2*D*r)^2 )*sqrt(1+(2*D*r)^2);
      UO=1; 
      ang=(2*D*r)^3/((1-r^2)+(2*D*r)^2);
      if ang<0, theta = atan ( ang ) + pi;
      else,     theta = atan ( ang );
      end
    else
      m=1/sqrt( (1-r^2)^2+(2*D*r)^2 );
      UO=P0/K;
      if r==1,    theta=pi/2;
      elseif r<1, theta= atan( (2*D*r)/( (1-r^2) ) );
      else,       theta= atan( (2*D*r)/( (1-r^2) ) )+pi;
      end
    end;
  else
    m=0;, theta=0;
  end
  set(LOAD_HAND(2,2), 'UserData', [ m theta ]);
  
  %********************SOLVE FOR RESOPONSE***********************
  if strcmp(load_type, 'Free Vibration')
     xplt1 = (Y0*cos(Wd*t1)+((V0+Y0*D*W)/Wd)*sin(Wd*t1)).*exp(-D*W*t1);
    
    %---------------SINUSOIDAL HARMONIC- --------------------
  elseif strcmp(load_type, 'Sinusoidal') & strcmp(load_perd, 'Harmonic')
    sine=get(LOAD_HAND(10,1), 'Value');
    if sine==1
      xplt1= m*UO * sin(OM*t1-theta);, LOAD=P0*sin(OM*t1);
    else
      xplt1= m*UO * cos(OM*t1-theta);, LOAD=P0*cos(OM*t1);
    end;
    
    %-----------SUPPORT MOTION----------------------------
  elseif strcmp(applied_l, 'SUPPORT MOTION')
    GIRF=zeros(1,s);
    C1=W*W*(1-2*D^2)/Wd;
    C2=W*2*D;
    GIRF(1)=0.5*C2  % use half of value to account for discontinuity
    for c=2:s
      GIRF(c)=exp(-D*W*t1(c))*(C1*sin(Wd*t1(c))+C2*cos(Wd*t1(c)));
    end;
    tsize = size(t1, 2);
    gsize = size(GIRF, 2);
    if tsize >gsize
      t1=t1(1:gsize);
    elseif gsize > tsize
      GIRF = GIRF(1:tsize);
    end 
    
    xplts=conv(LOAD,GIRF);, xplts=xplts(1:s)*delT;
    C2=(V0+D*W*Y0)/Wd;
    for c=1:s
      xplt1(c)=xplts(c)+exp(-D*W*t1(c))*(C2*sin(Wd*t1(c))+Y0*cos(Wd*t1(c)));
    end;
    
    t1=t1(1:true_s);
    xplt1=xplt1(1:true_s);
    LOAD=LOAD(1:true_s);
    
    
    %---------ALL OTHER LOADING--------------------------------
  else
    LOAD(1)=0;
    tsize = size(t1, 2);
    lsize = size(LOAD, 2);
    if tsize >lsize
      t1=t1(1:lsize);
    elseif lsize > tsize
      LOAD = LOAD(1:tsize);
    end 
    [xplt1,yplt1] = lsim(A1, A2, A3, A4, LOAD, t1, [Y0, V0]);
    t1=t1(1:true_s);
    xplt1=xplt1(1:true_s);
    LOAD=LOAD(1:true_s);
  end;
  set(PROP_HAND(6), 'UserData', [LOAD; t1]);
  clr = clr_plts(PCOLOR);
  
  %********************PLOT GRAPH OR RESPONSE********************
  if strcmp(applied_l,'SUPPORT MOTION')
    plot (t1, xplt1, clr);
    ylabel('Absolute Response');
  elseif strcmp(applied_l,'TEST')
    plot (t1, xplt1, clr);
    ylabel('Relative Displacement');
  else
    plot (t1, xplt1, clr);
    ylabel('Displacement');
  end
  xlabel('Time');
  PCOLOR=PCOLOR+1;
  if (PCOLOR >= 7), PCOLOR = 1;, end;
  
  trans_fig=findobj('type', 'figure', 'Name', 'TRANSFER');
  if ~isempty(trans_fig)
     sdof('plotting', 'transfer');
  end
  load_fig=findobj('type', 'figure', 'Name', 'LOAD');
  if ~isempty(load_fig) & ~strcmp(load_type,'Free Vibration')
    sdof('plotting', 'load');
  end
  
  lfft_fig=findobj('type', 'figure', 'Name', 'LOAD FFT');
  if ~isempty(lfft_fig) & ~strcmp(load_type,'Free Vibration')
    sdof('plotting', 'load_fft');
  end
  
  rfft_fig=findobj('type', 'figure', 'Name', 'RESPONSE FFT');
  if ~isempty(rfft_fig)
    sdof('plotting', 'resp_fft');
  end

  setptr(gcf, 'arrow');
  
%************************************************************************
%*      	       PLOT MENU OPTIONS				*
%************************************************************************
 
elseif strcmp(option, 'plotting')
   setptr(gcf, 'watch');
   main = gcf;
   if strcmp(arg1, 'resp_fft')
      %************************Respose Fourier Transform*****************
      h=get(gca, 'Children');
      
      rfft_fig=findobj('type', 'figure', 'Name', 'RESPONSE FFT');
      if isempty(rfft_fig)
	rfft_fig = figure( ...
	    'Name', 'RESPONSE FFT', ...
	    'Position', [725  40 300 200] ,...
	    'UserData', arg2,...
	    'NumberTitle', 'off', ...
	    'Colormap', []);
      else
	figure(rfft_fig);
      end
      
      s=size(h,1);
      i=1;
      while i<s & ~strcmp(get(h(i), 'type'), 'line')
         i=i+1;
      end
	 
	if ~strcmp(get(h(i), 'type'), 'line')
	  %errordlg('No response to analyze');
	else
	  data=get( h(i), 'YData');
	  tdata=get( h(i), 'XData');
	  delT = tdata(2);
	  if PCOLOR == 1, pcolor = 1;
	  else, pcolor = PCOLOR-1;, end
	  clr = clr_plts(pcolor);
	  
	  
	  
	  rsize=size(data,2);
	  if strcmp( popupstr(LOAD_HAND(8,2)), 'Periodic')
	    period = str2num( get(LOAD_HAND(9,2), 'String') );
	    points = round(period/delT); sdof('check', points, 1);
	    if rsize > points, rsize=points;, end;
	    
	    data = data(1:rsize);
	  end
	  %interpolate to get points to the power of 2
	  n=2;
	  while n<rsize
	    n=n*2;
	  end
	  response = linear_iteration(data, rsize, n);
	  delT = delT*rsize/n;
	  resp_fft=fft(response);
	  absolute=abs(resp_fft(1:floor(n/2)))*delT;
	  nyquist=1/(2*delT);
	  fft_freq=(0:(n/2-1))/(n/2)*nyquist;
	  hold on;
	  plot(fft_freq, absolute, clr);
	  h=get(gcf, 'UserData');
	  minf = str2num(get(h(1), 'String'));
	  maxf = str2num(get(h(2), 'string'));
	  set(gca, 'XLim', [minf maxf]);
	  title('Response FFT');
	  xlabel('Frequency(Hz)');
	  ylabel('Absolute Magnitude');
	  grid on;
	end
      
   elseif strcmp(arg1, 'load')
      %****************************LOADING********************************
      main=gcf;
      load_fig=findobj('type', 'figure', 'Name', 'LOAD');
      if isempty(load_fig)
         load_fig = figure( ...
            'Name', 'LOAD',...
            'Position', [725  600 300 150] ,...
            'NumberTitle', 'off', ...
            'Colormap', []);
      else
         figure(load_fig);
      end
      if PCOLOR == 1, pcolor = 1;
      else, pcolor = PCOLOR-1;, end
      clr = clr_plts(pcolor);
      %Get load vector
      data = get(PROP_HAND(6), 'UserData');
      if ~isempty(data)      
         LOAD = data(1,:);
         time = data(2,:);
	 hold on;
	 plot(time, LOAD, clr);
	 xlabel('Time');
         ylabel('Excitation Magnitude')
         grid on;
       end
       figure(main);
            
   elseif strcmp(arg1, 'load_fft')
      %*****************LOADING FOURIER TRANSFORM********************
      main=gcf;
      load_fig=findobj('type', 'figure', 'Name', 'LOAD FFT');
      if isempty(load_fig)
         load_fig = figure( ...
            'Name', 'LOAD FFT',...
            'Position', [725  600 300 120] ,...
	    'UserData', arg2,...
            'NumberTitle', 'off', ...
            'Colormap', []);
      else
         figure(load_fig);
      end
      if PCOLOR == 1, pcolor = 1;
      else, pcolor = PCOLOR-1;, end
      clr = clr_plts(pcolor);
      %Get load vector
      data = get(PROP_HAND(6), 'UserData');
      if ~isempty(data)
         load = data(1,:);
         time = data(2,:);
         delT = time(2);
	 lsize=size(load,2);
	 if strcmp( popupstr(LOAD_HAND(8,2)), 'Periodic')
	   period = str2num( get(LOAD_HAND(9,2), 'String') );
	   points = round(period/delT); sdof('check', points, 1);
	   if lsize > points, lsize=points;, end;
	   
	   load = load(1:lsize);
	   time = time(1:lsize);
	 end
         
         %interpolate to get points to the power of 2
         n=2;
         while n<lsize
            n=n*2;
         end
         LOAD = linear_iteration(load, lsize, n);
         delT = delT*lsize/n;
         load_fft=fft(LOAD);
         absolute=abs(load_fft(1:floor(n/2)))*delT;
         nyquist=1/(2*delT);
         fft_freq=(0:(n/2-1))/(n/2)*nyquist;
	 hold on;
	 plot(fft_freq, absolute, clr);
	 h=get(gcf, 'UserData');
	 minf = str2num(get(h(1), 'String'));

	 maxf = str2num(get(h(2), 'string'));
	 set(gca, 'XLim', [minf maxf]);
         title('LOAD FFT');
         xlabel('Frequency(Hz)');
	 ylabel('Absolute Magnitude');
         grid on;
      end
      
   elseif strcmp(arg1, 'impulse')
      %*****************IMPULSE RESPONSE FUNCTION*****************  
   elseif strcmp(arg1, 'transfer') 
      %*****************Transfer Function and Phase Angle*****************
      %Determine if figure exist, else create one
      trans_fig=findobj('type', 'figure', 'Name', 'TRANSFER');
      if isempty(trans_fig)
         trans_fig = figure( ...
            'Name', 'TRANSFER', ...
            'Unit', 'pixels' ,...
            'UserData', 0, ... 
            'Position', [725  25 300 400] ,...
            'NumberTitle', 'off', ...
            'Colormap', []);
      else
         figure(trans_fig);
      end
      
      %Get Values from data. 
      applied_l=popupstr(LOAD_HAND(11,1));
      M   =str2num(get(PROP_HAND(1),'String'));
      K   =str2num(get(PROP_HAND(2),'String'));
      D   =str2num(get(PROP_HAND(3),'String'));
      OM  =str2num(get(LOAD_HAND(2,2),'String'));
      ratio = get(LOAD_HAND(2,2), 'UserData');
      if isempty(ratio), ratio=[0 0];, end;
      D= 0.01*D;, OM=OM*2*pi;
      W = sqrt(K/M);
      r = (0:.01:3);
      s=size(r,2);
      trans = zeros(1,s);
      phase = zeros(1,s);
      
      if isempty(OM)
         r_v = 0;
      else
         r_v= OM/W;
      end
      
      for i=1:s
         if strcmp(applied_l,'SUPPORT MOTION') %Transmisibility
            trans(i)=sqrt(1+(2*D*r(i))^2)/sqrt((1-r(i)^2)^2+(2*D*r(i))^2);
            ang(i)=(2*D*r(i))^3/((1-r(i)^2)+(2*D*r(i))^2);
            if ang(i)<0
               phase(i) = atan ( ang(i) ) + pi;
            else
               phase(i) = atan ( ang(i) );
            end
            
         else  %Use transfer function
            trans(i) = 1/sqrt( (1-r(i)^2)^2+(2*D*r(i))^2 );
            if r(i)<1
               phase(i) = atan ( (2*D*r(i))/(1-r(i)^2) );
            elseif r(i)==1
               phase(i) = pi/2;
            else
               phase(i) = atan ( (2*D*r(i))/(1-r(i)^2) )+pi;
            end
         end
      end
      
      phase=phase*180/pi;
      
      flag=get(gcf, 'UserData');
      if PCOLOR == 1, pcolor = 1;
      else, pcolor = PCOLOR-1;, end
      clr = clr_plts(pcolor);
      
      subplot(2, 1, 1);
      hold on;
      plot(r, trans, clr);
      %grid on;
      m=ratio(1);
      x=[r_v r_v];, y=[0 m];
      plot(x,y, clr);
      if flag==0
         grid on;
         xlabel('Frequency Ratio');
         if strcmp(applied_l,'SUPPORT MOTION'), ylabel('Transmisibility');
         else, ylabel('Amplitude');, end;
         title('Transfer Function');
      end;
      
      subplot(2, 1, 2);
      hold on;
      plot(r, phase, clr)
      %grid on;
      set(gca, 'YLim', [0  180]);
      theta=ratio(2)*180/pi;
      x=[r_v  r_v];, y=[0 theta];
      plot(x,y, clr);
      if flag==0
         grid on;
         xlabel('Frequency Ratio');
         ylabel('Phase Angle(deg)');
      end;
      
      set(gcf, 'UserData', 1);   
      
   end
   figure(main);
   setptr(gcf, 'arrow');
%************************************************************************
%*      	       BUTTON CALLBACKS					*
%************************************************************************

elseif strcmp(option, 'button')
   
   if strcmp(arg1, 'close')
    fh=gcf;
    h=get(gcf, 'Userdata');
    trans=findobj('type', 'figure', 'Name', 'TRANSFER');
    load =findobj('type', 'figure', 'Name', 'LOAD');
    rfft =findobj('type', 'figure', 'Name', 'RESPONSE FFT');
    lfft =findobj('type', 'figure', 'Name', 'LOAD FFT');
    if ~isempty(trans), close(trans);, end;
    if ~isempty(load) , close(load);, end;
    if ~isempty(rfft) , close(rfft);, end;
    if ~isempty(lfft) , close(lfft);, end;
    close(fh);
  
  elseif strcmp(arg1, 'default')
    
    %min max dt t freq
    freq = str2num(get(arg2(5), 'String'));
    time = ceil(5/freq);
    dt = 1/(16*freq);
    minf = 0;
    maxf = ceil(1/(2*dt));  % nyquist
    set(arg2(1), 'String', minf);
    set(arg2(2), 'String', maxf);
    set(arg2(3), 'String', dt  );
    set(arg2(4), 'String', time);
    
  elseif strcmp(arg1, 'help')
    
    ttlStr='VIBRATION OF SDOF';
    hlpStr= ...                                            
	['                                              '  
      ' This interactive program plots the response  '  
      ' of a single degree of freedom system to the  '  
      ' specified forcing function.  Enter the system'  
      ' properties and loading conditions, then press'
      ' the PLOT button to obtain the response.      '
      ' Previous plots remain on the screen, to allow'  
      ' for comparison of the different results.  To '  
      ' clear the screen press the CLEAR button.     '];  
    helpfun(ttlStr,hlpStr);                                
    
  elseif strcmp(arg1, 'helpfile')
    
    ttlStr='Input File Format';
    hlpStr= ...                                            
	['                                              '  
      ' To input a random forcing function, such     '  
      ' as an earthquake, prepare a file with the    '  
      ' following format.  Note the "Point Data"     '  
      ' option that allows you to specify a function '
      ' consisting of up to three points.            '
      '                                              '  
      ' File Format:                                 '  
      '                                              '  
      ' number_of_points                             '  
      ' t1       P1                                  '  
      ' t2       P2                                  '  
      ' t3       P3                                  '  
      ' ...                                          '
      ' tn       Pn                                  ']; 
    helpfun(ttlStr,hlpStr);                                
    
  elseif strcmp(arg1, 'clear')
    plot_btn=findobj('String', 'Plot Response');
    set(plot_btn, 'Enable', 'off'); 
    cla,  PCOLOR=1;
    main=gcf; %, h=get(main, 'UserData');   
    trans=findobj('type', 'figure', 'Name', 'TRANSFER');
    load =findobj('type', 'figure', 'Name', 'LOAD');
    rfft =findobj('type', 'figure', 'Name', 'RESPONSE FFT');
    lfft =findobj('type', 'figure', 'Name', 'LOAD FFT');
    if ~isempty(trans), figure(trans);
           subplot(2,1,1), cla, subplot(2,1,2), cla,  end;
    if ~isempty(load) , figure(load);,  cla, end;
    if ~isempty(rfft) , figure(rfft);,  cla, end;
    if ~isempty(lfft) , figure(lfft);,  cla, end;
    set(trans, 'UserData', 0);
    figure(main)
    set(plot_btn, 'Enable', 'on');
  end
elseif strcmp(option, 'file_plot')
  filename = arg1;
  filedat  = fopen(filename, 'r');
  npts = fscanf(filedat, '%d', 1);
  time = zeros(1,npts);
  load = zeros(1,npts);
  for j=1:npts
    time(j) = fscanf(filedat, '%g', 1);
    load(j) = fscanf(filedat, '%g', 1);
  end
  main=gcf;
  load_fig=findobj('type', 'figure', 'Name', 'LOAD');
  if isempty(load_fig)
     load_fig = figure( ...
        'Name', 'LOAD',...
        'Position', [725  450 300 275] ,...
        'NumberTitle', 'off', ...
        'Colormap', []);
  else
     figure(load_fig);
  end
  if PCOLOR == 1, pcolor = 1;
  else, pcolor = PCOLOR-1;, end
  clr = clr_plts(pcolor);
  %Get load vector
  plot(time, load, clr);
  title('Loading')
  ylabel('magnitude')
  grid;
  figure(main);
  data = [time; load];
  set(arg2, 'UserData', data);
 
elseif strcmp(option, 'check')
  if isempty(arg1)
    errordlg('A required input is missing');
    error('A required input is missing');
    error;
  elseif arg1<arg2
    errordlg('Improper input specified, check data');
    error('Improper input specified, check data');
  end
end

function vec2=linear_iteration(vec1, n1, n2)
x=n1/n2;
vec = zeros(1,n2);
vec(1)=vec1(1);
for i=2:(n2-1)
   t=(i-1)*x+1;
   j=floor(t);
   e=t-j;
   vec(i)=(1-e)*vec1(j)+e*vec1(j+1);
end
vec(n2)=vec1(n1);
vec2=vec;


function time_step=find_time_step(f, time)
%function to calculate time step
dt = 1/(f*16);, s = time/dt;
n=128;, while n<s, n=n*2;, end
time_step = time/(n);


