25
$\begingroup$

When I solve this equation in Mathematica 8, I can get the right answer, but with some uncomfortable warnings.

Equation:

Solve[-26.81 == 194 k + k*l*32.9 && 22.2 == -74 k + k*l* 59.7, {k, l}]

Warnings:

Solve::ratnz: Solve was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result. >>

If I transform the above equation to the following, then it works fine, but this is not convenient:

{k, kl/k} /. Solve[-26.81 == 194 k + kl*32.9 && 22.2 == -74 k + kl* 59.7, {k, kl}]

So my question is, how can I get rid of such warnings?

$\endgroup$
1
  • $\begingroup$ There's Quiet[], but it's cheating in a certain sense... $\endgroup$ May 26, 2012 at 14:05

6 Answers 6

31
$\begingroup$

You can get rid of the warning by converting everything to exact numbers yourself before passing the equation to Solve (the warning message suggests that this is what Solve does itself):

In[2]:= Rationalize[-26.81 == 194 k + k*l*32.9 &&  22.2 == -74 k + k*l*59.7]
Out[2]= -(2681/100) == 194 k + (329 k l)/10 && 111/5 == -74 k + (597 k l)/10

In[3]:= Solve[%]
Out[3]= {{l -> -(2322860/2330937), k -> -(2330937/14016400)}}

In[4]:= N[%]
Out[4]= {{l -> -0.996535, k -> -0.166301}}

Solve (like all symbolic manipulation function) is meant to be used with exact numbers where roundoff errors are not an issue. For solving the equation numerically, use NSolve:

In[5]:= NSolve[-26.81 == 194 k + k*l*32.9 && 22.2 == -74 k + k*l*59.7]
Out[5]= {{l -> -0.996535, k -> -0.166301}}

Some background on exact and inexact numbers:

  • Exact and Approximate Results

  • In Mathematica, any number with a decimal point in it is considered to be inexact, i.e. known only to a certain number of digits. 2 is exact, 2.0 is inexact machine precision and 2.0`5 is inexact arbitrary precision known to 5 digits.

  • Symbolic computations (Integrate, Solve, Reduce, etc.) work best with exact numbers. Try to avoid inexact numbers with such functions.

  • See more on Numbers

$\endgroup$
11
$\begingroup$

An alternative method would be to replace all inexact values with named parameters and replace afterwards, like this:

Solve[a == 194 k + k*l*b && c == -74 k + k*l*d, {k, l}]

{{k -> (-b c + a d)/(74 b + 194 d), l -> (2 (37 a + 97 c))/(-b c + a d)}}

% /. {a -> -26.81, b -> 32.9, c -> 22.2, d -> 59.7}

{{k -> -0.1663006906, l -> -0.9965348699}}

To automatize this process I introduce a function inexactSolve that, when placed against your complaining Solve, finds all inexact numbers, converts them to an inert function, solves the resulting equation and converts back.

ClearAll[inexactSolve]
SetAttributes[inexactSolve, HoldFirst]
inexactSolve[expr_Solve] := 
   ReleaseHold@ Replace[Hold[expr], a_?InexactNumberQ :> inert[ToString[a]], Infinity] 
     /. inert[a_String] :> ToExpression[a]

Demo:

inexactSolve@Solve[-26.81 == 194 k + k*l*32.9 && 22.2 == -74 k + k*l*59.7, {k, l}]

{{k -> -0.1663006906, l -> -0.9965348699}}

How it works will be clear if I leave away the /. inert[a_String] :> ToExpression[a] part. The result then is:

Mathematica graphics.

Of course, this may be considered as shooting sparrows with a cannon, but that can be very effective ;-)

$\endgroup$
3
  • $\begingroup$ May I propose: inexactSolve[expr_Solve] := Block[{Identity}, Unevaluated[expr] /. a_?InexactNumberQ :> Identity[a] ] $\endgroup$
    – Mr.Wizard
    Dec 17, 2012 at 0:17
  • $\begingroup$ @Mr.Wizard That doesn't seem to work. It yields OP's original error message, at least on my system. $\endgroup$ Dec 17, 2012 at 13:30
  • $\begingroup$ @Mr.Wizard The closest I get when trying to work with your code is inexactSolve[expr_Solve] := ReleaseHold@ Block[{Identity}, Replace[Hold[expr], a_?InexactNumberQ :> Identity[ToString[a]], Infinity]] /. Identity[a_String] :> ToExpression[a]. Which means I get rid of inert. It seems that both the conversion to string and Replace with Infinity parameter are necessary. $\endgroup$ Dec 17, 2012 at 13:36
9
$\begingroup$

If you use NSolve[] instead of Solve[] (since indeed you are using inexact numbers like 26.81 in your equations), the warning you speak of should not show up.

Alternatively, you can wrap your Solve[] line in a Quiet[], but that is not a strategy I can recommend in good conscience.

$\endgroup$
5
$\begingroup$

I had the same issue and using NSolve didn't work. I got similar message:

NSolve::ratnz: NSolve was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result.

I found a solution here:

Error/warning when using NSolve for simple equation

It proposes using Rationalize

$\endgroup$
1
  • 1
    $\begingroup$ Rationalize was also used in the accepted answer on this page given by Szabolcs. You may have missed that. $\endgroup$ Apr 9, 2014 at 13:38
1
$\begingroup$

Simply solve as follows:

Solve[-26.81 == 194 k + k*l*32.9 && 22.2 == -74 k + k*l*59.7]

Result is:

{{l -> -0.996535, k -> -0.166301}}

No warning appears!

$\endgroup$
1
  • 1
    $\begingroup$ Warning appears with my MMA 11.0 (anyway, the warning has a reason, and this does not address this reason). $\endgroup$
    – anderstood
    Feb 26, 2017 at 1:33
0
$\begingroup$

If you're sure you don't want to ever see this warning message, you can always turn it off. First, using Solve with the message on:

Solve[-26.81==194 k+k*l*32.9&&22.2==-74 k+k*l*59.7,{k,l}]

Solve::ratnz: Solve was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result.

{{k -> -0.166301, l -> -0.996535}}

Then, turn off the message and repeat:

Off[Solve::ratnz]
Solve[-26.81==194 k+k*l*32.9&&22.2==-74 k+k*l*59.7,{k,l}]

{{k -> -0.166301, l -> -0.996535}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.