Weird behavior when solving a simple physics problem

I have the following code in Mathematica that represents two balls being thrown, one with a velocity v10 (6 in this case) and another ball thrown with a velocity v20 (its just dropped) one second later:

v10 = 6
v20 = 0
a = -9.8
t0 = 0

v1[t_] = Integrate[a, t] + v10
v2[t_] = Integrate[a, t] + v20
x1[t_] = Integrate[v1[t], t] + h
x2[t_] = Integrate[v2[t], t] + h

eq1 = (t /. Solve[0 == x2[t], t]) + 1
eq2 = t /. Solve[0 == x1[t], t]

eq4 = Solve[eq1[[2]] == eq2[[2]]]


{{h -> 0.410596}}

h is the height the balls begin at in order to meet the ground at the same time. All is good. But as soon as I plug in a variable, V, for the velocity v10 everything falls apart:

v10 = V
v20 = 0
a = -9.8
t0 = 0

v1[t_] = Integrate[a, t] + v10
v2[t_] = Integrate[a, t] + v20
x1[t_] = Integrate[v1[t], t] + h
x2[t_] = Integrate[v2[t], t] + h

eq1 = (t /. Solve[0 == x2[t], t]) + 1
eq2 = t /. Solve[0 == x1[t], t]

eq4 = Solve[eq1[[2]] == eq2[[2]]]


{h -> 1.61298*10^-61 (5.40298*10^92 - 1.10265*10^92 V + 5.62576*10^90 V^2 - 1. (-2.32443*10^46 + 2.37187*10^45 V) Sqrt[ 5.40298*10^92 - 1.10265*10^92 V + 5.62576*10^90 V^2])}, {h -> 1.61298*10^-61 (5.40298*10^92 - 1.10265*10^92 V + 5.62576*10^90 V^2 + (-2.32443*10^46 + 2.37187*10^45 V) Sqrt[ 5.40298*10^92 - 1.10265*10^92 V + 5.62576*10^90 V^2])}

Then if I go further and try to plug 6 back into the equation for V:

x[V_] = h /. eq4[[1]]
x[6]


2.62065*10^31

This is obviously the wrong answer. Am I missing something here?

• I cannot replicate the indicated results. I guess some input is missing. By eyeball I do not see 'V' anywhere, for example. Jan 22, 2015 at 0:45
• You're right, Daniel. It had a typo. It should work now. Jan 22, 2015 at 0:54
• Still nope, I think the problem is Solve[x10 == x2[t], t]. Note the undefined variable x10. Jan 22, 2015 at 0:55
• I thought I got rid of that variable. I had two edits - now it should work. Jan 22, 2015 at 0:58

You've encountered floating-point roundoff error. Note the combination of very large and very small numbers in eq4.

Let's try something different. I'll evaluate just your integrals, without the numbers:

v1[t_] = Integrate[a, t] + v10
v2[t_] = Integrate[a, t] + v20
x1[t_] = Integrate[v1[t], t] + h
x2[t_] = Integrate[v2[t], t] + h


Now we'll just Solve for t and h.

FullSimplify@Solve[x1[t] == 0 && x2[t-t0] == 0, {h, t}]


{{h -> -((t0 (a t0 + 2 v10) (a t0 - 2 v20) (a t0 + 2 v10 - 2 v20))/( 8 (a t0 + v10 - v20)^2)), t -> (t0 (a t0 - 2 v20))/(2 (a t0 + v10 - v20))}}

Now let's simply substitute our values:

% /. {v10 -> 6, v20 -> 0, a -> -9.8, t0 -> 1}


{{h -> 0.410596, t -> 1.28947}}

I've noticed that Mathematica often has trouble dealing with mixed symbolic/machine-precision expression. It was your intermediate Solve that gave it difficulty.

I almost always evaluate symbolically first. If your equation has no analytical solution, substitute your numbers as the last step before Solve.

• I would also suggest using DSolve instead of Integrate, it will let you handle more complex cases easily. Jan 22, 2015 at 1:30
• Thanks for the help. I had to use my original equations since your FullSimplify line didn't work for me (I got h->0, t->0), but substituting the numbers at the end worked like a charm. Jan 22, 2015 at 2:29
• @Mardymar Oops, the reason you got {h->0,t->0} was that I forgot the time offset. Check the Solve again now. Jan 22, 2015 at 2:38