% quad3
% solves ax^2+bx+c = 0
%
a = input ( 'Enter a: ' );
b = input ( 'Enter b: ' );
c = input ( 'Enter c: ' );
%
%  The discriminant, D, tells us if we have
%    D > 0: 2 real roots;
%    D = 0: 1 real root;
%    D < 0: 2 complex roots.
%
d = b^2 - 4 * a * c;

if ( d > 0.0 )

  r1 = ( - b - sqrt ( d ) ) / ( 2 * a );
  r2 = ( - b + sqrt ( d ) ) / ( 2 * a );
  fprintf ( '  Real root #1 = %f\n', r1 );
  fprintf ( '  Real root #2 = %e\n', r2 );

elseif ( d == 0.0 )

  r = - b / ( 2 * a );
  fprintf ( '  Real root = %10.6f\n', r );
%
%  MATLAB's sqrt function will return a complex value if
%  the argument is negative.  Our biggest problem is trying
%  to print out the result.  The complex number r1 has two
%  parts, real ( r1 ) is the real part, and imag ( r1 ) is
%  the imaginary part.
%
else

  r1 = ( - b - sqrt ( d ) ) / ( 2 * a );
  r2 = ( - b + sqrt ( d ) ) / ( 2 * a );
  fprintf ( '  Complex root #1 = (%g, %gi)\n', real ( r1 ), imag ( r1 ) );
  fprintf ( '  Complex root #2 = (%g, %gi)\n', real ( r2 ), imag ( r2 ) );

end
