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?


1 Answer 1

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][[1]]

{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][[1]]

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

  • $\begingroup$ 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. >> $\endgroup$
    – BCott
    Jun 18, 2015 at 5:47
  • $\begingroup$ 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? $\endgroup$
    – Bob Hanlon
    Jun 18, 2015 at 11:16
  • $\begingroup$ 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 [[1]] 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 [[1]] 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? $\endgroup$
    – BCott
    Jun 18, 2015 at 16:56
  • $\begingroup$ 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. $\endgroup$
    – Bob Hanlon
    Jun 18, 2015 at 17:20
  • $\begingroup$ That all makes sense now. Thank you for your help $\endgroup$
    – BCott
    Jun 19, 2015 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.