Summary

Code

Executable on S2

Videos

We have seen in the last topic “Rate of Change” that a rabbit can have different speeds in different periods of time.

For example, the rabbit in the above picture starts at position 0 m then position 5 m at time = 5 s. It is then at position 30 m at time = 10 s. From this information, namely distance/space and time, we can compute that the rabbit has the speed $$1\text{ m/s}$$ in the first 5 s and then the speed $$5\text{ m/s}$$ in the second 5 s. Its acceleration during the second period is $$0.8{\text{ m/s}}^{2}$$.

Let’s consider a reverse question. Suppose we know the speeds of the rabbit in the two periods but not the distance. We want to compute the distance travelled by the rabbit for any time $$t$$. For the first 5 seconds, we know from the topic “An Application of Linear Function: Galileo’s Equations of Motion”, the distance function is:

$$d_1 = 1*t \tag{eq 1}$$

where $$t$$ is time and 1 is the speed $$1\text{ m/s}$$.

The code is:

// the distance function for the first leg
fun distance1(t : Double): Double { // t = time
val v = 1.0; // 1 m/s
val d = v * t;
return d;
}

distance1(1.0)

1.0
distance1(5.0)

5.0

This distance function is linear. It simply scales the time variable by a constant.

Likewise, the extra distance function for after the first 5 seconds is

$$d_1 = 5*t \tag{eq 2}$$

where $$t$$ is time and 5 is the speed $$5\text{ m/s}$$.

The code is:

// the distance function for the second leg
fun distance2(t : Double): Double { // t = time
val v = 5.0; // 5 m/s
val d = v * t;
return d;
}

distance2(1.0)

5.0
distance2(2.0)

10.0

This distance function is also linear. It simply scales the time variable by a constant.

Note that this is the extra distance counting from the 5th second, or from distance at 5 m, not from time 0 s and not from the starting position/origin/home.

We want to have a function that computes the distance from the starting position or time 0 s. The simplest way is to break it down into two cases: $$t \leq 5$$ and $$t > 5$$.

When $$t \leq 5$$, we can simply use $$\text{eq 1}$$.

$$d_3 = 1*t, t \leq 5$$

We know that when t = 5 s, the distance travelled is already 5m. When $$t > 5$$, we can add the extra distance travelled by the second leg to the 5 m (called an offset). The distance travelled by the second leg is described by $$\text{eq 2}$$. The total distance is therefore:

$$d_3 = 5 + 5*t, t \geq 5$$

The code is:

// the distance function for the whole process
fun distance_12(t : Double): Double { // t = time
val t1 = 5.0; // end time for the first leg
if (t <= t1) {
return distance1(t);
}
else {
val d2 = distance2(t - t1); // this must be positive
val d_12 = distance1(t1) + d2; // add the extra distance to the last point of the first leg
return d_12;
}
}

distance_12(0.0)

0.0
distance_12(2.0)

2.0
distance_12(5.0)

5.0
distance_12(6.0)

10.0
distance_12(10.0)

30.0

We can plot the function to see the distance curve.

// plot the distance graph over time
val t = (0..10)

%use plotly
Plotly.plot {
scatter {
x.set(t)
y.set(t.map { distance_12(it.toDouble()) })
}
}


It is immediately obvious that this line is not a straight line. Therefore, the function is not a linear function. We call this function a piecewise linear function because it is constructed by conjoining two linear functions together. Although this function itself is not linear, each piece or segment of it is linear, hence piecewise linear.

We can add an extra leg 3 to this process. Suppose the rabbit slows down to 3 m/s after 10 s. We have:

// the distance function for the third leg
fun distance3(t : Double): Double { // t = time
val v = 3.0; // 3.0 m/s
val d = v * t;
return d;
}


The conjoint distance is:

// the distance function for the whole process
fun distance_123(t : Double): Double { // t = time
val t2 = 10.0; // end time for the second leg
if (t <= t2) {
return distance_12(t);
}
else {
val d3 = distance3(t - t2); // this must be positive
val d_123 = distance_12(t2) + d3; // add the extra distance to the last point of the first leg
return d_123;
}
}


To plot the distance curve against time, we do:

// plot the distance graph over time
val t = (0..20)

%use plotly
Plotly.plot {
scatter {
x.set(t)
y.set(t.map { distance_123(it.toDouble()) })
}
}


This is still a piecewise linear curve or a piecewise linear function.

We note that even though the rabbit slows down, the distance curve is still increasing. In fact, the whole curve is always increasing. We call this kind of curve a (strictly) increasing curve and the function a (strictly) increasing function. This is intiutive because as time increases the more distance the rabbit travels.