-1

Suppose you write a Python program to calculate the real roots of ax2 + bx + c = 0, where the coefficients a, b and c are given, real numbers. The traditional formulas for the two solutions are

x1 = (-b - sqrt(b*b-4*a*c)) / 2a, 
x2 = (-b + sqrt(b*b-4*a*c)) / 2a.

I need to identify situations (values ​​of the coefficients) where the formulas do not make sense or lead to large rounding errors, and propose alternative formulas that can be used in these cases in order to avoid a problem.

What do I need to do?

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
orange127
  • 1
  • 2
  • 3
    I fixed the equations again; they were still wrong. – Dmitry Brant Sep 17 '13 at 20:01
  • You may want to start your research here: http://en.wikipedia.org/wiki/Quadratic_equation#Solving_the_quadratic_equation – PM 77-1 Sep 17 '13 at 20:04
  • I'm not sure I understand the question. The only time when there may be issues is when `a` is equal to (or very close to) zero. In such cases, the equation becomes linear. – Dmitry Brant Sep 17 '13 at 20:04
  • 1
    This is not a python question, but one about error propagation in calculations. Hint: subtracting two big numbers is usually bad. Please study the relevant theory a bit more before asking us to do your homework. – Bas Swinckels Sep 17 '13 at 20:05

1 Answers1

2

There are two situations where problems arise. The first is when the term inside the square root (the "Discriminant") goes negative, i.e.

if(b*b - 4*a*c < 0 ):
  # do something. This doesn't have real roots

The second is more subtle. When you subtract two large numbers that are almost the same size, there is a possibility of rounding error. This will happen for the smaller root when 4*a*c << b*b. You could do a series expansion:

b - sqrt(b*b - 4*a*c)
= b * ( 1 - sqrt(1 - 4 * a * c / (b * b)))
~ b * ( 1 - 1 + 2 * a * c / (b * b))        # when 4*a*c << b*b

This term becomes

2 * a * c / b

So the final root would be

x1 = - c / b

Which is an interesting result. Of course the other root is still

x2 = (b + sqrt( b * b - 4 * a * c)) / (2 * a)

Not much error propagation there - although you could say that it will tend to

x2 = - b / a

when c becomes very small.

All this has nothing to do with Python - it is basic math. And I may have made errors - go ahead and see if you can find them.

For more help you might want to look at http://en.wikipedia.org/wiki/Quadratic_equation#Floating-point_implementation

Which provides a treatment of this issue in terms of numerical stability. You will find, among others, that the equations I (re)derived above are called "Vieta's formula".

Floris
  • 43,828
  • 5
  • 63
  • 112