home

PDF (letter size)

PDF (legal size)

Mathematica notebook

Solving the 4 bugs on corners of square or rectangle problem

Nasser M. Abbasi

Fall, 2018   Compiled on February 22, 2025 at 6:30pm

1 Problem description

Four bugs on corners of a rectangle are standing at positions (0,0),(0,2),(1,2),(1,0). Bug 1 starts to move directly towards bug 2, while bug 2 moves directly towards bug 3 and bug 3 moves directly towards bug 4 and bug 4 moves directly towards bug 1. All are moving with unit speed. Motion is clockwise. Find the equations of motions and make animation of the motion.

2 Solution

2.1 Analysis of motion

The following diagram shows the initial positions of all four bugs and what happens after Δt has elapsed.

The four bugs are initially located at corners of rectangle. The width is h=1 and the height is L=2. Since each bug moves with the same speed toward the bug next to it (clockwise), then by symmetry, the four bugs will remain on the corners of a parallelogram as time passes, but the parallelogram will become smaller and rotate clockwise as the bugs spiral towards the center of the original rectangle where they all meet. The following diagram illustrates such motion after some Δt has elapsed.

We see that at each instance of time, each bug remains at the corner of a scaled down parallelogram version of the original rectangle, which is rotating and the bug’s velocity is straight toward the bug position it is chasing. What this means is that bug 1 motion is always at 900 to the path of bug 2. And bug 2 motion is at 900 to the path of bug 3 and so on.

2.2 Equations of motion

To obtain the equation of motion for each bug, we will look at each bug’s position relative to the bug it is chasing. Starting with bug 1 relative to bug 2 with the help of the following diagram

The position vector of bug 1 is r1(t) and the position vector of bug 2 is r2(t). Therefore v1=dr1(t)dt=|v1|r^ where r^ is unit vector in the direction from bug 1 to bug 2. Hence

dr1(t)dt=|v1|r2(t)r1(t)r2(t)r1(t)

But since |v1|=1 meter per seconds then

dr1(t)dt=(x2ı^+y2ȷ^)(x1ı^+y1ȷ^)(x2ı^+y2ȷ^)(x1ı^+y1ȷ^)(dx1dtı^+dy1dtȷ^)=x2x1(x2x1)2+(y2y1)2ı^+y2y1(x2x1)2+(y2y1)2ȷ^

Where x1,y1 are the coordinates of bug 1 and x2,y2 are the coordinates of bug 2. From the above, we obtain equations of motion for bug 1, where now we use x1=dxdt and y1=dydt for bug 1.

x1=x2x1(x2x1)2+(y2y1)2(3)y1=y2y1(x2x1)2+(y2y1)2

The same analysis is now done to obtain x2(t) and y2(t) expressions similar to (3) above for bug 2.

The position vector of bug 2 is r2(t) and the position vector of bug 3 is r3(t). Therefore v2=dr2(t)dt=|v2|r^ where r^ is unit vector in the direction from bug 2 to bug 3. Hence

dr2(t)dt=|v2|r3(t)r2(t)r3(t)r2(t)

Since |v2|=1 meter per seconds then

dr2(t)dt=(x3ı^+y2ȷ^)(x3ı^+y2ȷ^)(x3ı^+y2ȷ^)(x3ı^+y2ȷ^)(dx2dtı^+dy2dtȷ^)=x3x2(x3x2)2+(y3y2)2ı^+y3y2(x3x2)2+(y3y2)2ȷ^

Where x2,y2 are the coordinates of bug 2 and x3,y3 are the coordinates of bug 3. The above gives the two equations of motion for bug 2, where now we use x2=dx2dt and y2=dy2dt for bug 2.

x2=x3x2(x3x2)2+(y3y2)2(3)y2=y3y2(x3x2)2+(y3y2)2

By doing the above again for bug 3 and bug 4, it is clear the result will be similar. The final equations of motions in vector form are

x=f(x)(x1(t)=x2x1(x2x1)2+(y2y1)2y1(t)=y2y1(x2x1)2+(y2y1)2x2(t)=x3x2(x3x2)2+(y3y2)2y2(t)=y3y2(x3x2)2+(y3y2)2x3(t)=x4x3(x4x3)2+(y4y3)2y3(t)=y4y3(x4x3)2+(y4y3)2x4(t)=x1x4(x1x4)2+(y1y4)2y4(t)=y1y4(x1x4)2+(y1y4)2)

With initial conditions

x(0)=(x1(0)y1(0)x2(0)y2(0)x3(0)y3(0)x4(0)y4(0))=(00021210)

We can not write this as x=Ax since it is not linear system. These ODE’s have to solved numerically since they are nonlinear. The following is the result of running the solution for 1.5 seconds with the code listing below it. This shows the bug spiraling down to the center of the original rectangle as expected.

This problem was also solved for a square instead of a rectangle. The only change needed was to make the initial conditions as follows

x(0)=(x1(0)y1(0)x2(0)y2(0)x3(0)y3(0)x4(0)y4(0))=(00011110)

The time needed to reach the center in this case is one second.

2.3 Animations

These animations will play in HTML only

  

  

  

Code for animation is

ClearAll[t, x1, x2, x3, x4, y1, y2, y3, y4]; 
ode1 = x1'[t] == (x2[t] - x1[t])/ 
   Sqrt[(x2[t] - x1[t])^2 + (y2[t] - y1[t])^2]; 
ode2 = y1'[t] == (y2[t] - y1[t])/ 
   Sqrt[(x2[t] - x1[t])^2 + (y2[t] - y1[t])^2]; 
ode3 = x2'[t] == (x3[t] - x2[t])/ 
   Sqrt[(x3[t] - x2[t])^2 + (y3[t] - y2[t])^2]; 
ode4 = y2'[t] == (y3[t] - y2[t])/ 
   Sqrt[(x3[t] - x2[t])^2 + (y3[t] - y2[t])^2]; 
ode5 = x3'[t] == (x4[t] - x3[t])/ 
   Sqrt[(x4[t] - x3[t])^2 + (y4[t] - y3[t])^2]; 
ode6 = y3'[t] == (y4[t] - y3[t])/ 
   Sqrt[(x4[t] - x3[t])^2 + (y4[t] - y3[t])^2]; 
ode7 = x4'[t] == (x1[t] - x4[t])/ 
   Sqrt[(x1[t] - x4[t])^2 + (y1[t] - y4[t])^2]; 
ode8 = y4'[t] == (y1[t] - y4[t])/ 
   Sqrt[(x1[t] - x4[t])^2 + (y1[t] - y4[t])^2]; 
 
sol = NDSolve[{ode1, ode2, ode3, ode4, ode5, ode6, ode7, ode8, 
    x1[0] == 0, y1[0] == 0, x2[0] == 0, y2[0] == 1, x3[0] == 1, 
    y3[0] == 1, x4[0] == 1, y4[0] == 0}, {x1[t], y1[t], x2[t], y2[t], 
    x3[t], y3[t], x4[t], y4[t]}, {t, 0, 1}]; 
 
Manipulate[ 
 p1 = ParametricPlot[{x1[t], y1[t]} /. sol, {t, 0, tMax}, 
    AxesOrigin -> {0, 0}, GridLines -> Automatic, 
    GridLinesStyle -> LightGray, Frame -> True, 
    FrameLabel -> {{"y", None}, {"x", None}}, ImageSize -> 350, 
    PlotStyle -> Red] /. Line[x_] :> {Arrowheads[.04], Arrow[x]}; 
 
 p2 = ParametricPlot[{x2[t], y2[t]} /. sol, {t, 0, tMax}, 
    AxesOrigin -> {0, 0}, GridLines -> Automatic, 
    GridLinesStyle -> LightGray, Frame -> True, 
    FrameLabel -> {{"y", None}, {"x", None}}, ImageSize -> 350, 
    PlotStyle -> Blue] /. Line[x_] :> {Arrowheads[.04], Arrow[x]}; 
 
 p3 = ParametricPlot[{x3[t], y3[t]} /. sol, {t, 0, tMax}, 
    AxesOrigin -> {0, 0}, GridLines -> Automatic, 
    GridLinesStyle -> LightGray, Frame -> True, 
    FrameLabel -> {{"y", None}, {"x", None}}, ImageSize -> 350, 
    PlotStyle -> Black] /. Line[x_] :> {Arrowheads[.04], Arrow[x]}; 
 
 p4 = ParametricPlot[{x4[t], y4[t]} /. sol, {t, 0, tMax}, 
    AxesOrigin -> {0, 0}, GridLines -> Automatic, 
    GridLinesStyle -> LightGray, Frame -> True, 
    FrameLabel -> {{"y", None}, {"x", None}}, ImageSize -> 350, 
    PlotStyle -> Green] /. Line[x_] :> {Arrowheads[.04], Arrow[x]}; 
 
 Show[p1, p2, p3, p4, PlotRange -> {{0, 1}, {0, 1}}, 
  PlotLabel -> 
   Column[{"Solution to the 4 bugs on corner problem", 
     "On (0,0),(1,1) square"}], BaseStyle -> 12, ImageSize -> 300], 
 
 {{tMax, .01, "time"}, .01, 1, .01, Appearance -> "Labeled"} 
 ]