# Unexpected ReplaceAll behavior with NSolve

I ran into some unexpected behavior using ReplaceAll with an equation of motion using NSolve (or Solve, also). First solving for the initial velocity v0:

In:= Clear[v0, v, dist, t, a, vInit, vFinal] In:= dist = Quantity[700, "Meters"] Out= Quantity[700, "Meters"] In:= t = Quantity[30, "Seconds"] Out= Quantity[30, "Seconds"] In:= a = Quantity[.05, "Meters per second^2"] Out= Quantity[0.05, ("Meters")/("Seconds")^2] In:= vInit = NSolve[dist == v0*t + 1/2 a*t^2, v0] Out= {{v0 -> Quantity[22.5833, ("Meters")/("Seconds")]}}

Then I use ReplaceAll to substitute the v0 solution in the equation to solve for vFinal. However, in this case, I get an error and null results:

In:= vFinal =  NSolve[{a (dist) == 1/2 (v^2 - v0^2), v >= 0}, v] /. vInit

During evaluation of In:= 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. >>

Out= {{}}


If I change the vInit NSolve command to output a value for vInit instead of a rule, NSolve then successfully calculates vFinal, as follows:

In:= vInit = v0 /. NSolve[dist == v0*t + 1/2 a*t^2, v0]
Out= {Quantity[22.5833, ("Meters")/("Seconds")]}
In:= vFinal = NSolve[{a (dist) == 1/2 (v^2 - vInit^2), v >= 0}, v]
Out= {{v -> Quantity[24.0833, ("Meters")/("Seconds")]}}


The error only occurs if I constrain the NSolve equation with v>=0. If I leave out this constraint, NSolve works correctly, generating both solutions to the equation:

In:= vFinal = NSolve[a (dist) == 1/2 (v^2 - v0^2), v] /. vInit
Out= {{{v -> Quantity[-24.0833, ("Meters")/("Seconds")]}, {v ->
Quantity[24.0833, ("Meters")/("Seconds")]}}}


As I said, I've used ReplaceAll for other similar calculations with no problem. This is the first time ReplaceAll hasn't worked as I expected. Am I misusing ReplaceAll for these type operations, or is this abnormal behavior for ReplaceAll and NSolve?

Clear[v0, v, dist, t, a, vInit, vFinal];
dist = Quantity[700, "Meters"];
t = Quantity[30, "Seconds"];
a = Quantity[.05, "Meters per second^2"];
vInit = NSolve[dist == v0*t + 1/2 a*t^2, v0][]


{v0 -> Quantity[22.5833, ("Meters")/("Seconds")]}

Put the ReplaceAll inside of the NSolve (in both instances you could use Solve rather than NSolve).

vFinal = NSolve[{a dist == 1/2 (v^2 - v0^2) /. vInit, v >= 0}, v][]


{v -> Quantity[24.0833, ("Meters")/("Seconds")]}

• thanks for the response. However, when I do that, I get the following error: NSolve::naqs: {275.(m)^2\[NegativeMediumSpace]/(s)^2==1/2 (v^2+-1083.51(m)^2\[NegativeMediumSpace]/(s)^2)}&&v>=0 is not a quantified system of equations and inequalities. >> – BCott Jun 18 '15 at 5:47
• I just copied the code from my post and it executed without a problem. I ran the code on Mma v10.1 with MacOS 10.10.3. Have you tried with a fresh kernel? – Bob Hanlon Jun 18 '15 at 11:16
• My original code did not have a Part specification in the calculation of vInit (since it only produces a single value for vInit) nor in vFinal (since the v>=0 constraint forces only the positive solution). When I add [] at the end of the vInit line as you did, I don't get the error in the calculation of vFinal (even if I don't also use [] in the vFinal line). While that answers the original question, it begs another: why is the part specification in vInit necessary for a calculation that only produces a single value? – BCott Jun 18 '15 at 16:56
• Your original code doesn't produce a single value, it produces a List containing a single List containing a single rule. Using this with the ReplaceAll results in a List structure different from the form {eqn, constraint} needed as the argument to Solve. – Bob Hanlon Jun 18 '15 at 17:20
• That all makes sense now. Thank you for your help – BCott Jun 19 '15 at 2:29