Post Reply 
The Two Train Problem
02-23-2018, 05:09 AM
Post: #1
The Two Train Problem
Blog Entry: https://edspi31415.blogspot.com/2018/02/...train.html

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?

[Image: Two%2Btrain%2Bdiagram.jpg]


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.

In general the distance equations for both trains are:

Train I: x = v1*t + a1*t^2
Train II: x = D - v2*t - a2*t^2

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

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
Code:

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+
4*d*(a1+a2)))/(2*(a1+a2));
x:=v1*t+a1*t^2;
ELSE
t:=d/(v1+v2);
x:=v1*t;
END;

// results
RETURN {"Time",t,
"Position",x};


END;
Visit this user's website Find all posts by this user
Quote this message in a reply
02-25-2018, 02:04 PM
Post: #2
RE: The Two Train Problem
(02-23-2018 05:09 AM)Eddie W. Shore Wrote:  Blog Entry: https://edspi31415.blogspot.com/2018/02/...train.html

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?

END;[/code]
Hi Eddie:

This encounter problems are familiar to me, what's more, I heard about them in elementary school. Two requests in this regard: a developed example and the possibility of a program for Hp 67 or Hp 35s. Thank you very much for your attention. Pedro
Find all posts by this user
Quote this message in a reply
02-25-2018, 06:19 PM (This post was last modified: 02-25-2018 06:58 PM by Dieter.)
Post: #3
RE: The Two Train Problem
(02-25-2018 02:04 PM)PedroLeiva Wrote:  Two requests in this regard: a developed example

Two examples (with and without acceleration) can be found on the linked website with Eddie's blog.

(02-25-2018 02:04 PM)PedroLeiva Wrote:  and the possibility of a program for Hp 67 or Hp 35s.

Well, this is the HP Prime Software library. ;-)
But you have mail.

Dieter
Find all posts by this user
Quote this message in a reply
02-25-2018, 10:55 PM (This post was last modified: 02-25-2018 11:33 PM by PedroLeiva.)
Post: #4
RE: The Two Train Problem
(02-25-2018 06:19 PM)Dieter Wrote:  
(02-25-2018 02:04 PM)PedroLeiva Wrote:  Two requests in this regard: a developed example

Two examples (with and without acceleration) can be found on the linked website with Eddie's blog.

(02-25-2018 02:04 PM)PedroLeiva Wrote:  and the possibility of a program for Hp 67 or Hp 35s.

Well, this is the HP Prime Software library. ;-)
But you have mail.

Dieter
Hello Dieter, I had not visited the Eddie page, but I already found the two examples. In addition I found an interesting deduction of formulas to calculate the time, in cases of zero and non-zero acceleration. The first situation is what I remembered for this type of problem.

You're right, this is the section for the Prime calculator, sorry. I only use HP 67 and Hp 35s. For the second device maybe it will be a good idea to program the equation solver, or integrate EQ SOL in a program

Pedro
Find all posts by this user
Quote this message in a reply
02-26-2018, 10:59 AM (This post was last modified: 02-28-2018 08:22 PM by Dieter.)
Post: #5
RE: The Two Train Problem
(02-23-2018 05:09 AM)Eddie W. Shore Wrote:  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?

...

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.

Here actually v is v0, the initial train speed. This means you assume that the trains are on their track, running at a speed of "v" and then they accelerate to even higher speeds from there. BTW the acceleration "a" can also be negative, in this case the train is slowing down.

Let me add a variation that is closer to real life: Let's assume both trains start at their respective stations, i.e. they initially have a speed of v0=0. Then each train is accelerating with a rate of "a" until it has reached its final velocity "v". From that point on it is running at a constant speed "v".

The acceleration phase happens for t ≤ v/a := T
For t > T the train runs at constant speed v.

So with T = v/a the train's position x can be given as follows:

t ≤ T:  x = 1/2 a t²
t > T:  x = 1/2 a T² + v·(t–T)

This can also be written as

x = 1/2 · a · min(t, T)^2 + v · (max(t, T)-T)
or
x = 1/2 · a · min(t, T)^2 + v · max(t–T, 0)

where T = v/a.

You can also do this for the second train where the position is D – x.

For the example data in your blog this yields a solution of

t =   13,093
x = 128,571

Check:

For the first 40/1,5 = 26,667 seconds train I is accelerating. Since 13,093 falls within this interval the train has reached a position of x = 1/2 · 1,5 · 13,093² = 128,57.

The second train accelerates during the first 35/2 = 17,5 seconds. 13,093 is also less than this, so train II is still accelerating as well. It has travelled a distance of 1/2 · 2 · 13,093² = 171,43 from its starting point D, which again is 300 – 171,43 = 128,57 relative to the starting point of train I.

Since both trains are still accelerating the calculation is easy here:

1/2 a1 t² = D – 1/2 a2 t²
t = √ [ 2D / (a1+a2) ]

For the example with D=300, a1=1,5 and a2=2 this yields t = 13,093.

A program that calculates a solution for this scenario has to handle three cases:

1. Both trains are accelerating
2. One of the train is accelerating while the other one runs at constant speed.
3. Both trains are running at constant speed

What about such a program?

Dieter
Find all posts by this user
Quote this message in a reply
02-28-2018, 10:27 PM
Post: #6
RE: The Two Train Problem
I think I am going to have to make corrections. I may not get to it today.

I think I forgot the 1/2. Embarrassing
Visit this user's website Find all posts by this user
Quote this message in a reply
03-01-2018, 02:35 AM
Post: #7
RE: The Two Train Problem
The program covers the following scenarios:

1. The trains are going at a constant velocity, with no acceleration

2. Either or both of the trains have both velocity and acceleration. Here I'm assuming the acceleration is a constant (or an average acceleration).

I should be posting a corrected program shortly. My position equation should be:
x = x0 + v*t + 1/2*a*t^2
Visit this user's website Find all posts by this user
Quote this message in a reply
03-01-2018, 02:54 AM
Post: #8
RE: The Two Train Problem
CORRECTED PROGRAM:

Code:

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+a1*t^2;
ELSE
t:=d/(v1+v2);
x:=v1*t;
END;

// results
RETURN {"Time",t,
"Position",x};


END;
Visit this user's website Find all posts by this user
Quote this message in a reply
03-01-2018, 08:53 AM
Post: #9
RE: The Two Train Problem
(03-01-2018 02:54 AM)Eddie W. Shore Wrote:  CORRECTED PROGRAM:

Eddie, I think the calculation of the position is still incorrect:

Code:
...
x:=v1*t+a1*t^2;
...

I think this should be   x:=v1*t+0.5*a1*t^2;

So for the example in your blog I get the same time but a different position:

t = 3,87018...
x = 158,552...

Dieter
Find all posts by this user
Quote this message in a reply
03-01-2018, 01:03 PM
Post: #10
RE: The Two Train Problem
One more revision:

Code:
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;


Next time I'm not going to go so fast before I post. Thanks.
Visit this user's website Find all posts by this user
Quote this message in a reply
03-01-2018, 02:00 PM
Post: #11
RE: The Two Train Problem
I don't want to sound pedantic, but if you want to have your example precissely correct then you should distinguish v1 and v2 (a1 and a2, too) in your picture, otherwise this task is simple: trains meet themselves exactly in the middle, i.e. x= D/2. :-)

Prime, 15C CE
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)