0

What condition should I put in code of matlab so that get the exactly solutions of a quadratic with these formulas:

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

Directly implementing these formulas I don't get the correct solution in certain cases such x^2-1000001x+1

Thank you very much for your help

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
Oriol Prat
  • 1,017
  • 1
  • 11
  • 19
  • roots will give you a better answer. It uses a matrix based method. – Joe Serrano Mar 14 '14 at 20:50
  • If you don't want to go completely symbolic (your tags suggest you're interested in numerical methods), you might try [variable precision arithmetic](http://www.mathworks.com/help/symbolic/vpa.html). – horchler Mar 14 '14 at 21:54
  • I am doubtful about the formula you are using for roots. Should it not be `(-b+sqrt(b^2-4ac)) / 2a` and `(-b-sqrt(b^2-4ac)) / 2a` – Sourabh Bhat Mar 16 '14 at 11:18
  • Yes, but I wanted to analyze the decimal difference that matlab causes between the classical formula – Oriol Prat Mar 16 '14 at 11:23

2 Answers2

1

The correct set of formulas is

w = b+sign(b)*sqrt(b^2-4*a*c)

x1 = -w/(2*a)

x2 = -(2*c)/w

where sign(b)=1 if b>=0 and sign(b)=-1 if b<0.

Your formulas as well as the standard formulas lead to catastrophic cancellation in one root of b is large wrt. a and c.


If you want to go to the extremes, you can also guard against over- and underflow in the computation of the term under the square root.

Let m denote the maximum size of |a|, |b| and |c|, for instance the maximum of the exponent in their floating point representation, or of their absolute value... Then

w = b+sign(b)*m*sqrt( (b/m)*(b/m)-4*(a/m)*(c/m) )

has a term between -10 and 10 below the root. And if this term is zero, then that is not caused by underflow.

Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51
0

You're dealing with floating point arithmetic in matlab, so exact solutions are not guaranteed. (I.e. it's possible that every single floating point value will cause round-off error that gives non-zero answer when you plug into your original quadratic equation). A better way to check whether you have found a solution to an equation in floating point is to use a tolerance, and check whether the absolute value of your answer is less than the tolerance.

user2566092
  • 4,631
  • 15
  • 20
  • So, Can I correct something of program so that the rounding be correct? – Oriol Prat Mar 14 '14 at 20:33
  • @OriolPrat The answer that you are computing with your formula is already "correct" to within working precision, it's just that floating point arithmetic rounding off is causing the quadratic equation to be slightly non-zero when you plug in the solution you found. So, there's nothing you can do to make the "rounding correct" for the solutions, but when you check solutions you can certainly set a tolerance like 10^(-50) and return that the solution is correct if the absolute value of the equation is less than 10^(-50) when you plug in your solution. – user2566092 Mar 14 '14 at 22:58