48 Data-driven functions
As early as Chapter 3 of this book, we noted that a function can be described as a table where each row stands for one set of input values together with a corresponding output value. We did not, however, make much use of the table-as-function concept. Instead, we used data tables to motivate the choice of parameters, as in linear combinations of the basic modeling functions. We called this function fitting: constructing a function that stays near the data values. (We will say more about function fitting in Block 5 where we introduce new tools for treating functions as geometrical objects.)
This chapter introduces yet another important method for constructing functions that match with data. What’s different here is that each data point will be a mandate; the function is required to go through each and every data point exactly.
48.1 Generating smooth motion
As a motivating example, consider the programming of robotic arms as in the video:
Since this isn’t a robots course, we will simplify. The arm has a resting position. When a car frame comes into place, the arm moves so that its welding electrodes are at a specific, known place in space near the car body. Then it moves in sequence to other places where a weld is required, perhaps passing through waypoints to avoid obstacles.
The problem of converting the discrete list of weld and waypoints into a continuous signal for the actuator is an instance of a mathematical process called interpolation. In real robot arms, there are multiple joints that need to be controlled simultaneously. For our illustration, we will use a simple setup where the robot hand rolls along a set of rails in the y-direction and another x-rail running crosswise to the y direction.
The task for our example robot will be to visit the points shown in Figure 48.1 in order, taking 15 seconds to traverse the whole path.
## Warning in (nudge_x != 0) || (nudge_y != 0): 'length(x) = 16 > 1' in coercion to
## 'logical(1)'
Figure 48.1 shows a continuous path in
Even so, functions can be a useful way of describing the Robot_stations
.
t | x | y |
---|---|---|
1 | 496 | 191 |
2 | 1037 | 138 |
3 | 1251 | 191 |
... 16 rows in total ... | ||
15 | 928 | 432 |
16 | 737 | 240 |
The
One strategy is to construct the functions as piecewise linear functions of
It can be difficult at first glance to see the relationship between the
Each functions in Figure 48.2 is an interpolating function. You’re entitled to think of the
For instance, let’s look at
The speed of the robot arm maxes out at about 600 mm-per-second. You can get a sense for this by moving your finger two feet in 1 second: a normal human speed of movement.
Since the original
Think about what it is that causes the change from one velocity step to another. There is a motor that is spinning and changing its rate of spin, perhaps using a pulley and a belt to move the robot hand to the right position at any instant of time. Changing the velocity requires a force to create an acceleration. We can differentiate the velocity to see what the acceleration must be to create the simple piecewise linear function shown in Figure 48.2.
Mathematically, the second derivatives
As an accommodation to the physical existence of the robot hand, we’ve softened the transition between consecutive velocity segments to allow it to take 0.2 seconds, ramping up from zero force 0.1 second before the hand reaches the station, to maximum force at the station, then back down to zero 0.1 second after the hand reaches the station. Consequently the actual motion is smoother and the maximum acceleration is about half that of gravity. Figure 48.5 shows the resulting trajectory which can be likened to that of a baseball player rounding a base.
A consequence of smoothing the trajectory is that the robot hand comes near, but does not touch the station. It misses by about 2 mm. For many human tasks that might be good enough, but for precision manufacturing a miss by 2 mm is about 1000 times more than allowed.
If you like working with practical problems, you might find a simple solution to the problem. For instance, we could have aimed the robot hand 2 mm further to the right than the actual station. In falling short by 2mm, the hand would miss the new target but cross right over the originally intended station.
Solutions like this are sometimes called ad hoc, meaning that they are so specifically tailored to one situation that they do not generalize well to slightly different problems. The next section introduces a superior approach that is much more general.
48.2 Piecewise but smooth
The approach we will take to smoothly connect the points on the path is based on ideas of derivatives and on the construction of low-order polynomials. In Block 2, we emphasized low-order polynomials up to the square term, and we will pick that up again here for demonstration purposes. For this example, we will construct only the
Our task is to find a function
The piecewise linear interpolating function is easily constructed and is shown as a dotted curve. As we saw in the previous section, such a function has a discontinuous first derivative. We would like something smoother, with a continuous first derivative. A curve such as the one we seek is shown as the multi-colored function.
The framework we will adopt for the smooth interpolating function is piecewise quadratic segments between adjacent knots. There are four knots, requiring three segments. We will call the segment
t | y |
---|---|
1 | 0.0 |
2 | 2.0 |
3 | 0.5 |
4 | 1.7 |
Constructing the interpolating function is a matter of making good choices for
We require these things of each of the interpolating polynomials:
- It passes exactly through the two knots marking the segment’s endpoints. That is
and and and, finally, . Note that at the interior knots where two polynomials join, the left-hand polynomial and the right-hand polynomial should exactly match the function value and each other. - The derivative (with respect to
) should match where the segments join. That is, and . Thus, the function we want to build will be , that is, have a continuous first derivative.
How to accomplish (1) and (2)?
Notice first that because we wrote each of the polynomials in the style of Taylor polynomials, we can read the values of
We can find other coefficients from the requirement that the right side of each segment pass through the knot on that side. This gives:
Another two conditions are that the derivatives of the polynomials from either side of each interior knot point must match at the knot point. Finding the derivatives of the segments is a simple exercise:
$$_t p_1(t) = b_1 + 2 c_1 \
_t p_1(t) = b_2 + 2 c_2 \ _t p_1(t) = b_3 + 2 c_3 $$ Matching these derivatives at the
Plugging in the specific values
b_1 + c_1 = x_2 - x_1 = 2\ b_2 + c_2 = x_3 - x_2 = -1.5\ b_3 + c_3 = x_4 - x_3= 1.2\ b_1 + 2 c_1 - b_2 = 0\ b_2 + 2 c_2 - b_3 = 0$$
This is not the place to go into the details of solving the five equations to find the six unknowns. (Block 5 introduces the mathematics of such things, which turns out to the same math used to find model parameters to “fit” data.) But there are some simple things to say about the task.
First, you may recall being told in high-school mathematics that to find six unknowns you need six equations. We have only five equations to work with. But it is far from true that there is no solution for six unknowns with five equations. There are in fact an infinite number of solutions. (Again, Block 5 will show the mathematics behind this statement.) Essentially, all we need to do is make up a sixth equation to identify a particular one of the infinite number of solutions. It is nice if this made-up equation reflects something interpretable about the curve.
We will choose to have the sixth equation specify what the derivative of the interpolating function should be at the far right end of the graph. That right-most derivative value will be
Keeping in mind the piecewise nature of the interpolating polynomial, it may seem surprising that changing the slope at
Quadratic spline functions can be created with the R/mosaic qspliner()
function. The second argument is a data frame giving the knot locations. The first argument is a tilde expression specifying the variables to use from the data frame.
<- qspliner(x ~ t, data = Robot_stations)
xfun <- qspliner(y ~ t, data = Robot_stations) yfun
Putting together the
## Warning in (nudge_x != 0) || (nudge_y != 0): 'length(x) = 16 > 1' in coercion to
## 'logical(1)'
Quadratic splines are rarely used in practice. (A cubic spline provides helpful flexibility. See Chapter Section 48.3.) In Figure 48.8 you can see one of the reasons: the quadratic form is so stiff that the interpolating function tends to shift from concave up to concave down (or vice versa) at each knot point. This results in the interpolating function tending to have a local minimum or maximum between adjacent knots, even if the data themselves to not indicate such a structure.
48.3 C2 smooth functions
In the previous section, we arranged the functions
To arrange
With the ability to match piecewise cubic polynomials to a set of knots, we can easily construct the smooth path to connect the knots in Figure 48.1. Figure 48.10 shows a
Cubic spline functions can be created with the R/mosaic spliner()
function. The second argument is a data frame giving the knot locations. The first argument is a tilde expression specifying the variables to use from the data frame.
<- spliner(x ~ t, data = Robot_stations)
xfun <- spliner(y ~ t, data = Robot_stations) yfun
## Warning in (nudge_x != 0) || (nudge_y != 0): 'length(x) = 16 > 1' in coercion to
## 'logical(1)'
48.4 Bézier splines
The sort of interpolating functions described in the previous two sections were designed to be smooth at the
Not all path-related design problems require such smoothness. Indeed, in some settings, non-smoothness is called for. For instance, Figure 48.12 shows the outline of a familiar shape
For both quadratic and the more commonly used cubic splines, matching the derivatives on either side of a knot is essential to constructing the function. For Bézier splines, each segment is mathematically independent from every other segment. It is up to the human designer of the curve to determine whether the curves derivative should be continuous or discontinuous at the knot point between two segments.
The shape of a Bézier spline segment is established by four independent points. The first and last points determine the endpoints of the segment. Each endpoint is associated with a control point that sets the angle and “speed” with which the path leaves or enters the endpoint. You can interact with the graph in Figure 48.13 to develop an intuition.
Link to the Bezier app for PDF version
The curve for a given segment is gratifyingly smooth. The real power of Bézier splines stems from how segments can be connected in various ways. Figure 48.14 shows two Bézier segments that have been initialized to have a smooth junction at endpoints 4 and 5. The smoothness is set by the corresponding control points (marked 3 and 6). So long as those four points (3, 4, 5, 6) are colinear and in order, the junction will be smooth. You can alter control points 2 and 7 in any way you like; the junction will remain smooth.
WHAT TO PUT HERE FOR PDF version?
Consider the path followed by a Bézier curve as it leaves one of the endpoints. Figure 48.14 has been initialized so that the tangent to the curve is horizontal at the right endpoint and almost vertical at the left endpoint. The further the control point is from the endpoint, the longer the Bézier curve will stay close to the tangent line. Another way to think of this is that the position of a control point has little impact on the shape of the curve near the opposite endpoint. You can observe this on the canvas by, say, moving control point 2 and observing the relatively little change near endpoint 4.
Provide link to bezier app
Algebraically, each Bézier segement is a pair of cubic functions,
The
48.5 Exercises
Exercise 48.01
These questions are just about R/mosaic code reading skills. They refer to the chapter example about Kepler’s analysis of the orbit of Mars.
Part A The data are stored in an object named Kepler
. What kind of object is Kepler
?
A data frame A matrix A vector A function
Part B There are two variables from Kepler’s data used in the code. What are their names?
time
andangle
distance
andangle
kepler.radius
andkepler.angle
kepler.distance
andkepler.angle
Part C The object connector
is defined on lines 2 and 3 of the sandbox code. What kind of R object is connector
? (Hint, you may need to read further along in the code to figure this out.)
A data frame A number A vector A function
Part D What kind of R object is kepler.radius ~ kepler.angle
?
- A tilde expression
- An arithmetic expression
- A function
- A character string
Part E The R function gf_point()
as used here has how many arguments?
1 2 3 4
Part F What’s the give away that polynomial
(as used here) is a function?
- The name
polynomial
is followed by open and close parentheses. - The name
polynomial
begins with ap
. - The name
polynomial
is an English verb.
Part G Why are there quotation marks in 'red'
?
- Because the color is only sort of red-ish, not pure red.
- Because Mars is the “Red Planet”
- Because the word is a literal set of characters, not the name of a function or other R object.
Exercise 48.03
There can be many ways to describe a given path using
To start, draw a pair of coordinate axes: one for
Task A
On your axes, sketch the graphs of
Task B
On the same coordinate axes, graph new functions
Task C
Again on the same coordinate axes (or new ones, if your graphs have become too crowded), graph new functions
Exercise 48.05
Referring to the quadratic spline interpolating functions in Figure 48.7, note that the different functions all go through the knots. The difference between them is the derivative at
Part A What is
-9 -5 -2 -1
Part B What is
-9 -5 -2 -1
Part C What is
1 2 5 9
Part D What is
1 2 5 9
Exercise 48.07
The drawing canvas has been initialized with two Bezier segments. Your task is to re-arrange the endpoints and control points to create a single stroke that resembles the lower-case character “e”. On your homework paper, sketch out the resulting curve and mark the locations of the control points that generated it.
Exercise 48.09
The two graphs below show the same set of knot points. The interpolating function in one graph is a cubic spline; in the other it is a quadratic spline.
- Which plot shows the cubic spline?
- What is it about the shapes of the two interpolating functions that motivated your answer to (1)?
Exercise 48.11
Referring to Figure 48.11, Explain what it means to say, “The cubic spline respects the monotonicity of consecutive knot points.”
Exercise 48.13
The two graphs below show the same set of knot points. The interpolating function in each graph is either a quadratic or a cubic spline. ::: {.cell layout-align=“center” fig.showtext=‘false’} ::: {.cell-output-display} ::: :::
- There are three possibilities: both function A and function B are quadratic splines, both functions are cubic splines, or one is a quadratic and the other a cubic spline. Which of these is the case in the above graphs?
- Explain what led you to your conclusion.
Exercise 48.15
The two graphs below show the same set of knot points. The interpolating function in each graph is either a quadratic or a cubic spline.
- There are three possibilities: both function A and function B are quadratic splines, both functions are cubic splines, or one is a quadratic and the other a cubic spline. Which of these is the case in the above graphs?
- Explain what led you to your conclusion.
Exercise 48.17
The two graphs below show the same set of knot points. The interpolating function in each graph is either a quadratic or a cubic spline.
- There are three possibilities: both function A and function B are quadratic splines, both functions are cubic splines, or one is a quadratic and the other a cubic spline. Which of these is the case in the above graphs?
- Explain what led you to your conclusion.
Exercise 48.19
Consider a set of
Under what conditions, if any, can a linear combination of
Exercise 48.25
These equations describe the Bézier curve
and
A. Cross out the terms that go to zero at
B. Similarly, Cross out the terms that go to zero at
Exercise 48.27
In this exercise, you’re going to show that each of the Bezier spline functions
and
have a function graph that leaves the first value (
You already know that
A. Calculate the initial slopes
B. Calculate the slope of a straight-line reaching from
Called such possibly because the curves are tied together at each of the knots.↩︎