% Compute roots of quadratic equation given a, b and c
% Start clean
clear
close all
a = input('input a ');
b = input('input b ');
c = input('input c ');
% Break down calculation into easier pieces. This is not
% required, but can help to reduce the possibility of formula
% translation errors.
% Compute the discriminant
d = sqrt(b^2 - 4*a*c);
% Compute the first root
root1 = (-b+d)/(2*a)
% Compute the second root
root2 = (-b-d)/(2*a)
% Alternate method of storing the solution.
% Because the two roots are related, you can choose to
% put them into a vector instead of individual scalars.
% Note: If you start putting elements into
% an undefined variable, the vector will be a row vector.
root(1) = (-b+d)/(2*a);
root(2) = (-b-d)/(2*a);
root = root' % This makes the vector a column vector
% x^2 -3x + 2 = 0 has roots 1,2 (a = 1, b = -3, c = 2)
% x^2 + 1 = 0 has roots i, -i (a = 1, b = 0, c = 1)
% input a 1
% input b 0
% input c 1
% root1 =
% 0.0000 + 1.0000i
% root2 =
% 0.0000 - 1.0000i