## Displaying complex roots of quadratic equation

### Valentina Sandberg (view profile)

on 7 Mar 2018
Latest activity Answered by Roger Stafford

### Roger Stafford (view profile)

on 8 Mar 2018
This is my code.
a = input ('enter a: ');
b = input ('enter b: ');
c = input ('enter c: ');
xmin = input ('enter xmin: ');
xmax = input ('enter xmax: ');
step = (xmax - xmin)/100;
x = xmin : step : xmax;
y = a.*x.^2 + b.*x + c;
plot (x,y);
grid on;
xlabel ('x') ;
ylabel ('y');
xtickangle (45);
title (['Plot of f(x) = ', num2str(a), 'x.^2 + ', num2str(b), 'x + ', num2str(c)]);
D = b^2 - 4*a*c;
if D >= 0
x1 = (-b + sqrt (D)) / (2*a);
x2 = (-b - sqrt (D)) / (2*a);
fprintf ('Real zeros: %g, %g, \n', x1, x2)
fprintf ('Factored Form: f(x) = (x - %g)(x - %g) \n', x1, x2)
else
x1 = (-b + sqrt (D)) / (2*a);
x2 = (-b - sqrt (D)) / (2*a);
fprintf ('Complex Zeroes: %g, %g, \n', x1, x2)
fprintf ('Factored Form: f(x) = (x - (%g))(x - (%g)) \n', x1, x2)
end
When I have a quadratic with complex roots, the last line of the code only displays the real part of the root. Why? How can I change it so it displays both the real and imaginary part?