**HP Prime and Casio fx-5800P: The Two Train Problem**

(updated
2/28/2018)

**Introduction**

Have
you ever heard the infamous problem “two trains are heading towards each
other…”?

Today’s
blog will cover the following question:

Two
trains are heading towards each other, on their own separate train track. Each train has going at their own speed
(velocity). The trains start out a
distance apart.

1.
When will the trains cross over the same spot, and

2.
Where will the trains cross over the same spot?

According
to the diagram above, we have two trains, labeled Train I and Train II, each
going at velocity

*v*and acceleration*a*. The trains start at distance*D*apart. We will call the point where the trains cross over the same spot,*x*.
Notes:

1.
To make our lives easier, let’s assume that Train I starts at position 0, while
Train II starts at position

*D*.
2.
Train I is going at velocity

*v*and acceleration*a*.
3.
Train II is going at velocity

*–v*and acceleration*–a*. Why negative? Train II is traveling in the opposite direction of Train I.**Setting up the Equations**

The
general distance equation is: x = x0 +
v*t + a*t^2, where x0 is the initial position.

We
are going to cover two scenarios: one
where there is no acceleration, that is the velocity of both trains is
constant. The other is where
acceleration is present for at least one of the trains. Acceleration is
assumed to be constant.

In
general the distance equations for both trains are:

Train
I: x = v1*t + 1/2*a1*t^2

Train
II: x = D - v2*t - 1/2*a2*t^2

What
this boils down to are a system of two equations, solving for both t and x.

Case
1: Constant Velocity

The
trains are moving at constant velocity in this scenario. Which means there is no acceleration, and
hence a1 = 0 and a2 = 0. Then:

Train
I: x = v1*t

Train
II: x = D - v2*t

Subtracting
equations gives us:

0
= -D + v1*t – -v2*t

0
= -D + (v1 + v2)*t

D
= (v1 + v2)*t

t
= D/(v1 + v2)

Once
t is known, we can use substitution to find x.
Train I’s equation is simpler, so:

x
= v1*t

Case
2: Non-Zero Acceleration

Start
with:

Train
I: x = v1*t + 1/2*a1*t^2

Train
II: x = D - v2*t - 1/2*a2*t^2

Subtracting
the equations and solving for t yields:

0
= -D + v1*t – -v2*t + 1/2*a1*t^2 - -1/2*a2*t^2

0
= -D + (v1 + v2)*t + 1/2*(a1 + a2)*t^2

Using
the quadratic equation:

t
= ( -(v1+v2) ± √( (v1+v2)^2 + 4*D*1/2*(a1+a2)) ) / (2*1/2*(a1 + a2))

t
= ( -(v1+v2) ± √( (v1+v2)^2 + 2*D*(a1+a2)) ) / (a1 + a2)

Note
that there are two roots for t. However,
given the problem, negative time does not make sense. We can eliminate the root for t that is
obviously negative.

Hence,
our solution for time in this problem is:

t
= ( -(v1+v2) + √( (v1+v2)^2 + 2*D*(a1+a2)) ) / (a1 + a2)

Again,
we are going to use the simpler equation for Train I to figure x:

x
= v1*t + a1*t^2

**The Program TRAINS**

The
program TRAINS will solve this problem.
Below are the codes for both the HP Prime and Casio fx-5800p. Enter each as a positive value as the
directions are accounted for in the program.

In
the program, Train I is considered the left train, while Train II is considered
the right train.

**HP Prime Program: TRAINS**

EXPORT
TRAINS()

BEGIN

//
2018-02-22 EWS

//
2 trains problem

LOCAL
d,t,x;

LOCAL
v1,a1,v2,a2;

INPUT({d,v1,a1,v2,a2},

"Two
Opposing Trains",

{"Dist:","L
Vel:","L Acc:",

"R
Vel:","R Acc:"},

{"Distance
between trains",

"Left
Train: Velocity",

"Left
Train: Acceleration",

"Right
Train: Velocity",

"Right
Train: Acceleration"});

//
calculation

IF
a1≠0 OR a2≠0 THEN

t:=(−(v1+v2)+√((v1+v2)^2+

2*d*(a1+a2)))/(a1+a2);

x:=v1*t+1/2*a1*t^2;

ELSE

t:=d/(v1+v2);

x:=v1*t;

END;

//
results

RETURN
{"Time",t,

"Position",x};

END;

**Casio fx-5800P Program: TRAINS**

“EWS
2018-02-22”

“DISTANCE”?→D

“LEFT-VEL.”?→A

“LEFT-ACC.”?→E

“RIGHT-VEL.”?→B

“RIGHT-ACC.”?→F

If
E≠0 Or F≠0

Then

(-(A+B)+√((A+B)^2

+2*D*(E+F)))÷

(E+F)→T

A*T+1/2*E*T^2→X

Else

D÷(A+B)→T

A*T→X

IfEnd

“TIME:”
◢

T ◢

“POSITION:”
◢

X

**Examples**

Assume
compatible units for each example.

Example
1: Constant Velocity

Train
I: v1 = 40 (a1 = 0)

Train
II: v2 = 35 (a2 = 0)

Distance: 300

Results:

Time: 4

Position: 160

Example
2: Acceleration

Train
I: v1 = 40, a1 = 0.5

Train
II: v2 = 35, a2 = 0.8

Distance: 300

Results:

Time: 3.87018761454

Position: 158.552092625

Thank you to Gianfranco Cazzaro, Dieter, and all who wrote to me pointing out the errors.

Eddie

This
is blog is property of Edward Shore, 2018.