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

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 this form, 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?

-
There's Quiet[], but it's cheating in a certain sense... – J. M. May 26 '12 at 14:05

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}}

-

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!

-

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

-
Rationalize was also used in the accepted answer on this page given by Szabolcs. You may have missed that. – Sjoerd C. de Vries Apr 9 '14 at 13:38

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:

.

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

-
May I propose: inexactSolve[expr_Solve] := Block[{Identity}, Unevaluated[expr] /. a_?InexactNumberQ :> Identity[a] ] – Mr.Wizard Dec 17 '12 at 0:17
@Mr.Wizard That doesn't seem to work. It yields OP's original error message, at least on my system. – Sjoerd C. de Vries Dec 17 '12 at 13:30
@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. – Sjoerd C. de Vries Dec 17 '12 at 13:36

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.

-