# 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 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?

• There's Quiet[], but it's cheating in a certain sense... 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:= Rationalize[-26.81 == 194 k + k*l*32.9 &&  22.2 == -74 k + k*l*59.7]
Out= -(2681/100) == 194 k + (329 k l)/10 && 111/5 == -74 k + (597 k l)/10

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

In:= N[%]
Out= {{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:= NSolve[-26.81 == 194 k + k*l*32.9 && 22.2 == -74 k + k*l*59.7]
Out= {{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.05 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: .

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] ] 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. 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. 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.

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. Apr 9 '14 at 13:38

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!

• Warning appears with my MMA 11.0 (anyway, the warning has a reason, and this does not address this reason). Feb 26 '17 at 1:33

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