How to get rid of warnings when using Solve on an equation with inexact coefficients?

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


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 ;-)


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.