% This program calculates the roots of the quadratic ax^2 + bx + c ==0 % INPUT COEFFICIENTS disp(' This m-file calculates the roots of ') disp(' a*x^2 + b*x + c = 0 ') a = input(' Give a value for the leading coefficient '); b = input(' Give a value for b '); c = input(' Give a value for c '); % Check if a is zero, if it is zero, we do not have a quadratic, but a % linear equation disp(' '); if a==0 disp(' a=0 :: Equation is linear in x'); disp(' '); if (b==0 & c==0) % Additional checks on the remaining coefficients disp (' equation is undetermined '); % 0 * x = 0 has solutions all possible values of x elseif (b==0 & c~=0) disp (' equation 0*x = constant has no solution ! '); % 0 * x = k has no solutions for x else x = -c / b; t = ['The unique Solution of ', num2str(b), '*x + ', num2str(c), ' = 0 is ', num2str(x)]; disp(t); end % The else refers to what happens if the statement a==0 is false, i.e. % what happens if a is not zero (then we can solve for the two roots). else d = b^2 - 4*a*c; % Generate the discriminant and check to see if +ve or -ve if d >= 0 disp(' Two real roots '); x1 = (-b + sqrt(d))/(2*a) x2 = (-b - sqrt(d))/(2*a) else disp(' Two complex roots '); x1 = (-b + i*sqrt(-d))/(2*a) x2 = (-b - i*sqrt(-d))/(2*a) end end