04-27-2016, 09:47 AM
Post: #1
 drkosse Junior Member Posts: 2 Joined: Apr 2016
Hi,

this is probably a stupid question but why does not the following equation give me an answer?

Solve(0.04*x^2-2.4*x+36=0)

Solving it on paper gives x=30
Wolfram Alpha solves this just fine

Thanks 04-27-2016, 01:16 PM
Post: #2
 Nigel (UK) Senior Member Posts: 361 Joined: Dec 2013
RE: Solve() gives wrong answer (r10077)
(04-27-2016 09:47 AM)drkosse Wrote:  Hi,

this is probably a stupid question but why does not the following equation give me an answer?

Solve(0.04*x^2-2.4*x+36=0)

Solving it on paper gives x=30
Wolfram Alpha solves this just fine

Thanks It's a rounding problem. The exact equation has two equal real roots which the CAS finds perfectly well with 4/100 and 24/10 in place of 0.04 and 2.4. With the approximate numbers in place I'm assuming that b^2-4*a*c is - just! - negative and so no real root exists.

Keeping the approximate numbers and changing 36 to 35.99999999 also works, as does using csolve instead of solve.

What is more annoying is that taking the equation 0.04*x^2-2.4*x+30=0 (which has two real roots that solve finds without difficulty) and multiplying each term by 1e-16 produces an equation that solve (or fsolve or csolve) won't solve. The CAS has a particular aversion to small approximate numbers which other systems do not share. As a physicist I find this frustrating, although once aware of the problem there are work-arounds.

Nigel (UK)
04-27-2016, 01:48 PM
Post: #3 Han Senior Member Posts: 1,817 Joined: Dec 2013
RE: Solve() gives wrong answer (r10077)
The CAS uses base-2 representation for numbers for speed. However, this also means that floating point calculations are seemingly inaccurate for many cases (what you see is not what you get) because there are certain decimal values that cannot be represented exactly using the base-2 representation (as an example: 0.2). I believe this is not an issue with BCD representations. What you see is in fact what you get.

http://www-fourier.ujf-grenoble.fr/~pari...n/#htoc558
https://users.cs.duke.edu/~raw/cps104/TW...ating.html

Converting all non-integer values to their rational form, however, will fix this issue if you insist on using the solve() command. The solve() command attempts to solve equations exactly and will return an empty list if it cannot. Using fsolve() in the case of "approximate" expressions should, however, give the correct (approximate) solution.

Graph 3D | QPI | SolveSys
04-27-2016, 02:14 PM
Post: #4
 parisse Senior Member Posts: 1,093 Joined: Dec 2013
RE: Solve() gives wrong answer (r10077)
The majority of my regressions tests are with exact data, this explains these kinds of quirks. I believe I can fix
Code:
fsolve(0.04e-16*x^2-2.4e-16*x+30e-16);

For the other example, it's intrinsically not possible to solve it, as explained by Han. Binary floats can only represent exactly rationals with a denominator dividing a power of 2, that's not the case for 0.04=1/25, therefore rounding errors might transform a 0 discriminant to something negative. csolve will return complex roots in this situation.
04-27-2016, 02:19 PM (This post was last modified: 04-27-2016 02:20 PM by toml_12953.)
Post: #5
 toml_12953 Senior Member Posts: 1,405 Joined: Dec 2013
RE: Solve() gives wrong answer (r10077)
(04-27-2016 01:16 PM)Nigel (UK) Wrote:
(04-27-2016 09:47 AM)drkosse Wrote:  Hi,

this is probably a stupid question but why does not the following equation give me an answer?

Solve(0.04*x^2-2.4*x+36=0)

Solving it on paper gives x=30
Wolfram Alpha solves this just fine

Thanks It's a rounding problem. The exact equation has two equal real roots which the CAS finds perfectly well with 4/100 and 24/10 in place of 0.04 and 2.4. With the approximate numbers in place I'm assuming that b^2-4*a*c is - just! - negative and so no real root exists.

Keeping the approximate numbers and changing 36 to 35.99999999 also works, as does using csolve instead of solve.

What is more annoying is that taking the equation 0.04*x^2-2.4*x+30=0 (which has two real roots that solve finds without difficulty) and multiplying each term by 1e-16 produces an equation that solve (or fsolve or csolve) won't solve. The CAS has a particular aversion to small approximate numbers which other systems do not share. As a physicist I find this frustrating, although once aware of the problem there are work-arounds.

Nigel (UK)

csolve gets {30-5.96046447754E-6*i,30+5.96046447754E-6*i}

Tom L[/b]

Tom L
...other than that, Mrs. Lincoln, what did you think of the play?
04-27-2016, 04:16 PM
Post: #6
 slawek39 Member Posts: 74 Joined: Jun 2014
RE: Solve() gives wrong answer (r10077)
solve(4/100*x^2-24/10*x+36=0,x) returns {30}
04-27-2016, 04:20 PM
Post: #7
 drkosse Junior Member Posts: 2 Joined: Apr 2016
RE: Solve() gives wrong answer (r10077)
Ok, so I will simply convert the decimals to ratios (manually or with function exact()) and then use solve(), like:

solve(1/25*x^2-12/5*x+36=0) which gives {30}

Thank you all for the explanations 04-28-2016, 03:24 AM
Post: #8 mandresve Member Posts: 92 Joined: Mar 2015
RE: Solve() gives wrong answer (r10077)
(04-27-2016 01:16 PM)Nigel (UK) Wrote:  ...I find this frustrating, although once aware of the problem there are work-arounds.

Nigel (UK)

I totally agree with you Success is the ability to go from one failure to the next without any loss of enthusiasm.
04-28-2016, 04:52 AM
Post: #9
 douganc Junior Member Posts: 34 Joined: Sep 2014
RE: Solve() gives wrong answer (r10077)
04-28-2016, 06:54 AM
Post: #10
 retoa Member Posts: 168 Joined: Jan 2015
RE: Solve() gives wrong answer (r10077)
Same problem with proot([0.04,-2.4,36])

so we have an error in the order of 1e-6 with thew first coefficient in the order of 1e-2.

I find this error very big, also considering binary approximation.
04-28-2016, 09:11 AM
Post: #11
 Nigel (UK) Senior Member Posts: 361 Joined: Dec 2013
RE: Solve() gives wrong answer (r10077)
(04-28-2016 06:54 AM)retoa Wrote:  Same problem with proot([0.04,-2.4,36])

so we have an error in the order of 1e-6 with thew first coefficient in the order of 1e-2.

I find this error very big, also considering binary approximation.

Actually, this error is about the correct size. It comes from $$b^2-4ac$$ being very slightly negative instead of zero. If $$b^2-4ac=-\epsilon$$ then the imaginary part of the roots is $$\pm{\rm i}\sqrt\epsilon/2a$$ and an error of $$\sim10^{-6}$$ is understandable if $$\epsilon\sim10^{-12}$$.

Nigel (UK)
04-28-2016, 12:34 PM (This post was last modified: 04-28-2016 12:59 PM by informach.)
Post: #12 informach Member Posts: 109 Joined: Jul 2015
RE: Solve() gives wrong answer (r10077)
Hi!:
This equation, is very easy, for Solve.

1) Press App.
2) Select Solve.
3) Write, the equation, in E1.
4) Press, Num and Solve.
5) You can see, the result.

Note: See, the images and comparison, with Wolfram Alpha.
Kind Regards.
informach.

Attached File(s) Thumbnail(s)     04-28-2016, 12:55 PM
Post: #13
 retoa Member Posts: 168 Joined: Jan 2015
RE: Solve() gives wrong answer (r10077)
(04-28-2016 12:34 PM)informach Wrote:  Hi!:
This equation, is very easy, for Solve.

1) Press App.
2) Select Solve.
3) Write, the equation, in E1.
4) Press, Num and Solve.
5) You can see, the result.

Note: See, the images and comparison, with Wolfram Alpha.
Kind Regards.
informach.

The problem is not with the Solve App, it's with the solve command in CAS
04-28-2016, 01:11 PM (This post was last modified: 04-28-2016 01:33 PM by informach.)
Post: #14 informach Member Posts: 109 Joined: Jul 2015
RE: Solve() gives wrong answer (r10077)
Hi!, retoa:

Don't worry !. Be Happy !.

Kind Regards.
informach.

Attached File(s) Thumbnail(s) 04-28-2016, 02:23 PM (This post was last modified: 04-28-2016 02:24 PM by retoa.)
Post: #15
 retoa Member Posts: 168 Joined: Jan 2015
RE: Solve() gives wrong answer (r10077)
(04-28-2016 01:11 PM)informach Wrote:  Hi!, retoa:

Don't worry !. Be Happy !.

Kind Regards.
informach.

Hi informarch,

this is not new, it was already proposed yesterday by drkosse and it obviously works as you don't have any binary approximation.
The problem comes only if you use solve() or proot() with approximated values. Please read the whole thread.
No problem with integers, nor with fractions.
04-28-2016, 02:34 PM
Post: #16
 DrD Senior Member Posts: 1,124 Joined: Feb 2014
RE: Solve() gives wrong answer (r10077)
For the [CAS] solve(0.04*x^2-2.4*x+36 = 0) test case:

When only the first coefficient is exact: solve(1/25*x^2-2.4*x+36 = 0,x) ==> {30}

When only the second coefficient is exact: solve(0.04*x^2-12/5*x+36 = 0,x) ==> {30,30}

-Dale-
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)