
% Lab 8 Solutions
% CS 1109, Summer 2011
% July 25

function [root1 root2] = quadraticSolver(a, b, c)
%
% quadraticSolver: This function solves the equation ax^2+bx+c = 0
%
%   This function accepts three inputs a, b, and c which are the
%   co-efficients to the quadratic equation ax^2+bx+c = 0. It solves this
%   equation for the real roots and returns them. If the given equation is
%   not quadratic, or if the equation has no real roots, then an error is
%   thrown.
%
% USAGE: [r1 r2] = quadraticSolver(1, 2, 1);
%

% Compute the determinant
determinant = b^2 - 4*a*c;

% Is the given equation legal?
if (determinant < 0)
    error('Roots are imaginary');
elseif (a == 0)
    error('Not a valid quadratic equation');
end

% It is legal --- use the quadratic formula to compute the two roots
root1 = (-b + sqrt(determinant)) / 2*a;
root2 = (-b - sqrt(determinant)) / 2*a;

end

