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[67]:= Clear[v0, v, dist, t, a, vInit, vFinal]
In[68]:= dist = Quantity[700, "Meters"]
Out[68]= Quantity[700, "Meters"]
In[69]:= t = Quantity[30, "Seconds"]
Out[69]= Quantity[30, "Seconds"]
In[70]:= a = Quantity[.05, "Meters per second^2"]
Out[70]= Quantity[0.05, ("Meters")/("Seconds")^2]
In[79]:= vInit = NSolve[dist == v0*t + 1/2 a*t^2, v0]
Out[79]= {{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[80]:= vFinal = NSolve[{a (dist) == 1/2 (v^2 - v0^2), v >= 0}, v] /. vInit
During evaluation of In[80]:= 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[80]= {{}}
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[81]:= vInit = v0 /. NSolve[dist == v0*t + 1/2 a*t^2, v0]
Out[81]= {Quantity[22.5833, ("Meters")/("Seconds")]}
In[82]:= vFinal = NSolve[{a (dist) == 1/2 (v^2 - vInit^2), v >= 0}, v]
Out[82]= {{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[88]:= vFinal = NSolve[a (dist) == 1/2 (v^2 - v0^2), v] /. vInit
Out[88]= {{{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?