% 
% A quadratic equation solver
% by Daniel T. Valentine.....December 2006
% 
% REMARKS:
% (1) Exercise: This code NEEDS CHECKING! Do 
% cases that you can check by hand in your 
% evaluation of the list of commands below. 
% (2) This is a code that you can revise by 
% clarifying and re-writting the structure plan 
% in the text and, then translate into your own 
% code for execution.
%
% Step 1: read doefficients to quadratic equation
%
%              a*x^2 + b*x + c = 0
% 
a = input(' Input a = ');
b = input(' Input b = ');
c = input(' Input c = ');
%
if a==0 & b==0 & c==0
    disp(' Solution indeterminate')
elseif a==0 & b==0
    disp(' There is no solution')
elseif a==0
    x = -c/b
    disp(' x (only one root: equation is linear)')
elseif b^2 < 4*a*c 
    x1 = (-b + sqrt(b^2 - 4*a*c))/(2*a);
    x2 = (-b - sqrt(b^2 - 4*a*c))/(2*a);
    disp(' x1, x2 are complex roots '),disp([x1 x2])
elseif b^2 == 4*a*c
    x = -b/(2*a)
    disp('equal roots')
else
    x1 = (-b + sqrt(b^2 - 4*a*c))/(2*a);
    x2 = (-b - sqrt(b^2 - 4*a*c))/(2*a);
    disp('Two roots are [x1 x2] = '),disp([x1 x2])
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Example of MATLAB used as a scratch pad:
%
% This was a first cut at writing a program to examine roots of 
% a quadratic equation after a quick reading of the text. The 
% second cut is provided above; it followed the "structure plan"
% originally written by Hahn.
%
%disp(' ')
%disp(' FINDING THE ROOTS ')
% Step 2: compute roots 
%if a == 0
%    x =-c/b;
%    disp (' x (only one root: equation is linear); x = '),disp(x)
%else
%            x1 = (-b + sqrt(b^2 - 4*a*c))/(2*a);
%            x2 = (-b - sqrt(b^2 - 4*a*c))/(2*a);
%            disp('Two roots are [x1 x2] = '),disp([x1 x2])
%end
% Step 3: If there are two roots, then comment on results.
%if a ~= 0
%disp(' ')
%disp(' A COMMENT ON THE ROOTS IS NEXT ')
%disp(' ')
%    if c == 0 & a~=0
%        xr1 = 0; xr2 = -b/a;
%        disp('One zero root, [xr1 x2] = '),disp([xr1,xr2])
%    end
%    if a~=0 & b ~=0 & c~=0 
%        if b^2 < 4*a*c
%            disp(' Complex roots ')
%        end
%        if b^2 == 4*a*c
%            x  = -b/(2*a);
%            disp(' equal roots ')
%        end
%        if b^2 > 4*a*c
%            disp(' roots are real and distinct ')
%        end
%    end
%end
% Step 4: Stop