%   AUTHOR: Santhanam , Balu
%   DATE : 03/05/99
%   SYNOPSIS: 
%   This function computes the roots r_1 and r_2 of a 
%   quadratic equation f(x) = ax^2 + b*x + c = 0
%   given the coefficients a , b and c.  
%   s specifies the case, i.e, overdamped etc... 
%   Makes use of the switch control flow structure
%   USAGE :  [r_1,r_2,s] = quadr(a,b,c);
%   
function [r_1,r_2,s] = quadr(a,b,c)
%*************************************************************  
%  Error checking for 
%  1) nonumeric input, null input
%  2) imaginary input, insufficient input
%*************************************************************
switch nargin < 3  
  case 1; error('Insufficient input info')
end       
cond2 = isempty(a) == 1 | isempty(b) == 1 | isempty(c) == 1;
cond3 = isnumeric(a) == 0 | isnumeric(b) == 0 | isnumeric(c) == 0;
cond4 =  imag(a)~= 0 | imag(b)~= 0 | imag(c) ~= 0;
sw_var1 = cond2 == 1 | cond3 == 1 | cond4 == 1;
sw_var0 = cond2 == 0 | cond3 == 0 | cond4 == 0; 
switch sw_var1 | sw_var0
  case {cond2 == 1}
  error('Empty Input')
  case {cond3 == 1}
  error('Non-numeric Input')
  case {cond4 == 1}
  error('Imaginary Coefficients')
end 
%
%***************************************************************
%    Switch based on the value of the a coefficient
%***************************************************************
switch a > 0 | a == 0 | a < 0
  case a == 0
     error('a must be non-zero')
  case a < 0
     fprintf('a coefficient -ve\n')
     fprintf('Multiplying coefficients by -1\n')
     a = -a; b = -b; c = -c; 
end
%*************************************************************
%  Discriminant Calculation
D = sqrt(b^2-4*a*c);
r_1 = (1/(2*a))*(-b+D);
r_2 = (1/(2*a))*(-b-D);
%********************************************************************
%  Determining the damping case: switch based on D 
%********************************************************************
   switch D^2 > 0 | D == 0 | D^2 < 0
    case D^2 > 0; s = 'Over Damped Case , Roots are Real';
    case D == 0; s = 'Critically Damped case , Roots are Equal';
    otherwise
     s = 'Underdamped Case , Roots are Imaginary';
end
return 
%*******************************************************************



