6 INTEGRALS: THE BASICS

A typical calculus course (or set of courses) is broken up into a part on derivatives and related topics (“differential calculus”) followed by a course about integrals (“integral calculus”). As we saw in Chapter 1, this distinction is arbitrary since the two are just inverses. It’s sort of like teaching addition and subtraction separately and calling them “additive arithmetic” and “subtractive arithmetic.”

Part of the reason for the traditional split is that integrals can be fussier to compute than derivatives. Some integrals have to have extensive algebra done on them to get them into a form that can be integrated. You can look up formulas for many of them in “integral tables” like those in the intimidating CRC Standard Mathematical Tables and Formulas (we used the 33rd edition from 2018, edited by Zwillinger), or search “integral tables” on Wikipedia. We’re not going to derive those in this chapter or prove to you that each of these many integrals work. We will, however, try to give you intuition about what is going on so that you can understand what an integral is, geometrically, and are able to deconstruct a few interesting cases.

Like Chapters 4 and 5, which introduce derivative concepts in the first chapter and then more detail and algebra in the next, we have a pair of chapters about integrals. This chapter introduces the concepts and a few simple examples about computing area under a curve and averages. Chapter 7 (and 13) cover applications related to computing volume of a 3D object, as well as detailed discussions of some algebra-heavy tricks of the trade. If you want more formal proofs than we give here, you can find them in any standard text that covers integral calculus. We are partial to Thomas, Hass, Heil, and Weir’s 2018 text, Thomas’ Calculus, 14th Edition.

What Is an Integral?

We can think of an integral as the area under a curve, or the accumulation of a quantity as it changes over time. If we have a continuous curve, and we want to find the area underneath it, we have to decide where to start adding up the area. If we ask how much water is needed to fill a cylindrical water tank, the answer depends on how much water is in there already. In other words, we need to know the starting level of water. If we specify where we are going to start and stop keeping track of the water level or whatever else we are accumulating, we call the calculation a definite integral, and we get a single value out of it.

Sometimes, though, we just want a function that tells us how much water we might need to fill a tank starting at one arbitrary level and ending at another. We call this an indefinite integral. Since we don’t know where we plan to start and end, to show that uncertainty, we always add an arbitrary constant amount to the function we get when we compute an indefinite integral. You will see “+ C” after the formula for integrals. Because taking the integral of a derivative gives you back the original function (plus or minus a constant), indefinite integrals are sometimes called antiderivatives.

Assembling an Integral

In Chapter 1, we saw that the Fundamental Theorem of Calculus tells us that taking a derivative and integrating are inverse operations. That is, doing one and then the other gets you back to where you started (except, as we will see, for a constant). We structure this chapter largely parallel to Chapter 4, using the equivalent examples. That way, you can compare these two chapters and see how the relationships between a curve and its derivative mirror that between a curve and its integral.

Let’s explore how this constant offset might come about. Say that we start off with a LEGO brick wall that represents f(x)=x,, like the red wall in Figure 6-1. Let’s accumulate the integral behind it in yellow. The yellow wall is the cumulative sum of the columns in the red wall to that point.

Two walls of LEGO bricks are on a baseplate. The shorter, red one has columns that rise one brick taller per column, starting at zero. The columns in the second yellow row is the running sum of the red row.

FIGURE 6-1: A LEGO brick wall that rises one brick at a time (red, 0, 1, 2, 3, 4, 5) and its integral (yellow, 0, 1, 3, 6, 10, 15)

If we were to add up all the bricks in the red curve, we would get a column equal to the last column of the integral wall, as we would expect (Figure 6-2).

LEGO bricks on a baseplate as in the previous figure. All of the red bricks have been moved into a single column, which is the same height as the final column in the yellow wall.

FIGURE 6-2: Sum of the red wall in Figure 6-1 (15 bricks), same as the highest column of the integral

Now suppose we cut off the first three points of the red curve and started it at x = 3 instead of at x = 0. What does the integral look like now? The first point of the curve we are adding up is 3; then we add 4 to that to get 7, and add 5 to get 12 (Figure 6-3).

LEGO bricks on a baseplate as in Figure 6-1. The first two columns of each wall have been set aside. 3 bricks in total have been removed from the red wall, and so each of the remaining columns in the yellow wall has been shortened by three. The removed bricks are shown in the background.

FIGURE 6-3: Red wall (3, 4, 5) cutting off the first two nonzero points. Integral (yellow, 0, 3, 7, 12) has been trimmed by offsets in back.

Notice what we have taken off the integral curve (offset on the left of Figure 6-3): an offset of 3, once the curve had built up to that. That is the total amount that has been taken off the function (red) curve.

Now suppose we started our red curve two points earlier, at x=2 instead of at x = 0. What does the integral look like now? The first point of the curve we are adding up is 2; then we add 1 to that to get 3. Then we add zero to that, to stay at -3. Then we add 1 to get -2; add 2 to get 0; add 3 to get 3; and add 4 to get 7 (Figure 6-4).

The LEGO brick rows of Figure 6-1, now extended in the negative direction. The red wall begins with a value of negative 2, and has a total of 3 red bricks, so once again, the bricks toward the end of the yellow wall are 3 bricks shorter than in the original.

FIGURE 6-4: The function (red, -2, -1, 0, 1, 2, 3, 4, 5) extended down to x = -2, and corresponding integral (yellow, –2, -3, -3, -2, 0, 3, 7, 12)

Now then: Let’s compare the values of the integral for our three cases (Table 6-1). You’ll notice that the difference between the integrals in the first two cases at any given point is 3.

x

-2

-1

0

1

2

3

4

5

Sum (integral) starting at x = 0

 

 

0

1

3

6

10

15

Sum (integral) starting at x = 3

 

 

 

 

 

3

7

12

Sum (integral) starting at x = -2

-2

-3

-3

-2

0

3

7

12

TABLE 6-1: Cumulative sum for f(x)=x

Remember that the last column is the accumulated sum of all the values to that point, depending on where we started. So for instance in the first case, the integral is equal to 15 at x = 5. In the second case, it is equal to 12.

Suppose we wanted to see what the sum between x = 0 and x = 5 was for the third case. Just subtract off the sum at x = 0 (the negative part of the curve we added in the third example) from the sum at x = 5. That works out to 12(3)=15, just like we got in the original case.

The Second Part of the Fundamental Theorem of Calculus

You might be thinking that this is obvious, and it is! But it can feel a little subtle. The fact that you can get the integral from a starting point to an ending point by subtracting one from the other is otherwise known as the second part of the Fundamental Theorem of Calculus, and it looks much scarier in calculus notation than in LEGO bricks.

As we just saw, where we start and end our integral matters, if we are trying to come up with a number for the integral of a function. In Chapter 1, we learned a lot about the first part of the Fundamental Theorem of Calculus — that derivatives and integrals are inverses of each other. The second part says we can find out the value of the integral of a function over a particular range. To do that, we evaluate the integral of the function at the upper and lower endpoints and subtract one from the other, giving us the value of the integral over the desired range. We will see examples of this a little later in the chapter, and at the same time learn the limitations of our LEGO experiments.

Meanwhile, a practical analogy is that if you want to know how much water you put into a tank between 2 and 3 PM, you will need to subtract off what was there at 2 PM from the final value at 3 PM. If we don’t know when we are starting and ending, we use an indefinite integral, about which more in a little bit.

Computing Integrals

Integrals often look a little strange upon first glance. Integrals are shown as a lengthened letter “S”, which you can think of as reminding you that you are summing everything inside. (Historically, it was just how the letter was written in Newton and Leibniz’s time — check out handwritten items from the 1600s.) For example, the definite integral

abf(x)x

would be read aloud as “the integral from a to b of f(x) with respect to x.” Or, to think about it another way, we want to sum up the area under the curve f(x), adding up from one endpoint (x=a) to another (x=b).

The "dx" is usually written at the end, to act as sort of a bracket along with the integral sign. Some disciplines, though, like high-energy physics, like to write the dx first. This isn’t “wrong,” but reflects a slightly different way of thinking about it. Sometimes, instead of just “a” and “b”, people will write out x = a to be clearer, but it can get cluttered and that’s not the most common convention. People talk about what is between an integral sign and the dx (or dt, or d-any-other-variable) as being “in the integral” or “under the integral sign” although that isn’t what one sees looking at the symbol. (“Under the integral” can also mean an integral inside another integral.)

Now let’s see how we would write what we did with our LEGO bricks in the last section, where the red curve represented f(x)=x. In all three cases, b = 5. The lower limit b was 0 for the first case, 3 for the second, and -2 for the third. The integrals for the three cases would be written like this:

05xx35xx25xx

Indefinite Integrals (Antiderivatives)

In Chapter 4’s section on derivatives of powers of x, we learned that the derivative of f(x)=x2 is just 2x. Since taking a derivative and integrating are inverses of each other, it stands to reason that if we integrate f(x)=2x, we will get x2. Or alternatively, if we integrate f(x)=x, our integral will be x22.

We can write this in general as

xx=x22+c

You can see that we added a “+ C” to our answer, and we have not specified a beginning or endpoint. This is the indefinite integral, or antiderivative. Why do we need the + C? If we take the derivative of a constant, it adds zero to the total. We lose information when we take a derivative, since it just tells us how a curve is changing between two points. If we take the integral of f(x)=x2 or f(x)=x2+10, we get the derivative equal to 2x in both cases.

To put it in a more concrete way, if we are filling a tank so that the level of water rises one inch a minute, we can’t tell if we are filling an empty tank or one with 100 inches in it already just by knowing the derivative (rate of filling).

However, we do have the values at the end of the integrals for our cases in the last section. Let’s try computing our three brick cases with the formula for the integral of f(x)=x.

05xx=5220=12.535xx=522322=825xx=522(2)22=10.5

What is going on here? That’s not the answer we got with our bricks (Table 6-1). The problem is that our little brick demonstration is really approximating the integral between the two bricks of the original (red) curve. You may have noticed that we always place our brick curve and derivative offset by half a brick from each other. There isn’t any good way to show fractional bricks. What we are really seeing is the integral at a value halfway between the number represented by each brick (Figure 6-5), or

5.522(0.5)22=155.5222.522=125.522(2.5)22=12
The same walls as Figure 6-1, as viewed from above. An axis along the base of the columns of bricks has been drawn in, with numbers centered on the center of each red brick. Columns in the yellow wall representing the integral fall between numbers, so that they begin at one number and end at the next, with their centers aligned to half values.

FIGURE 6-5: Top view of the case in Figure 6-3, showing the middle of each brick on the original curve (red) lining up with x0.5 on the left and x+0.5 on the right

In this particular case, we used a point 0.5 below the first value and 0.5 above the last value to compute a numerical value. Figure 6-5 shows why that is. This example shows the curve and integral in Figure 6-1, with the beginning and end of the integral marked. The curve is going from x = 0 to x = 5. You can see that, including the “stack” of zero bricks centered at zero on the number line, we are actually counting up bricks starting from -0.5 and going to 5.5 on the number line in Figure 6-5.

You may recall that in earlier chapters, we warned that we had created special cases that happened to work out to whole numbers for our brick models. Because our original curve was a straight line, our approximate value falls exactly between our whole-number original curve values, but this will not be the case for all functions. In general, brick results will only be approximate because of these issues, but the overall concept remains valid. In Chapter 8, we’ll show you some ways to narrow this window down further when calculating derivatives and integrals numerically.

If you recall our discussion of the Mean Value Theorem in Chapter 4, you will be able to see why we need to use these values that are offset by 0.5. This is also why we offset the integral stacks by half a brick from those representing the original curve when we build the brick models. Of course, as the Mean Value Theorem tells us, the offset won't always be exactly 0.5, but the answer will be correct at some point between the two values.

If we were to graph the integral of f(x)=2x, as we did in Chapter 1, we can use an offset of 0.5 to graph the odd-numbered points on the curve (1, 3, 5, 7) in Figure 6-6, and then the integral values will fall on the even boundaries and indeed be squares (1, 4, 9, 16) since their centers also fall on integer values, as labeled in Figure 6-6.

A new pair red and yellow walls with another overlaid number line. This time the centers of the red columns fall on the half values of the number line, and the yellow columns' centers are aligned with the whole numbers.

FIGURE 6-6: Curve of f(x)=2x[red,1,3,5,7] and its integral (yellow, 1, 4, 9, 16)

Area Under a Curve

It is probably obvious, but still worth saying, that our brick examples are telling us that an integral is the area under the original curve. If we have only part of a curve, we can still get the area under part of it by subtracting the integral values at the beginning and end of a curve, as the second part of the Fundamental Theorem tells us.

Area of a Region

Suppose, though, that we have a situation not quite as tidy as the one in the previous section, where we could construct cross-sections that were rectangles that we could neatly add up. Suppose instead that we wanted to evaluate the area bounded by two curves, like f(x)=x2[red] and f(x)= 52x2 (blue) for 1x1 (Figure 6-7). You can see the overlap (which extends beyond where we are going to compute it).

An upward-opening parabola is filled in red. A downward-facing one is filled in blue. The overlap interior area, roughly shaped like an oval with points on the long sides where the curves cross, is shaded in purple.

FIGURE 6-7: Area between two curves

We could integrate each of these separately, and subtract one from the other, being careful about signs. Or we could just integrate f(x)=52x2 x2=53x2. Try this out and see if you get the same answer each way. Note that for this example, we have to be careful to keep x within a range in which the two curves do not cross. Beyond those points, the difference we calculate will give us a negative number, so we would be adding negative area to our total. Depending on what you are trying to accomplish by thinking about the area, this negative area might not make any sense, and you might need to just ignore those sections instead.

Computing an Average

One application of integrals is finding an average of a complicated curve. If the integral adds up the area under a curve, then to find the average value of the entire curve over some interval, we just have to divide the definite integral by the length of that interval. The average value of f(x) can be written this way:

Averageoff(x)=1baabf(x)x

Let’s try it out with f(x)=x from x=0 to x=4. The answer should be that the average is 2.

Averageoff(x)=1baabf(x)x=14004f(x)x=14(4220)=2

If this is confusing, think about how you would find the average of a list of numbers, like 1, 2, 3. If we add them up and then divide by the number of numbers, we would get 1+2+3 = 6, and dividing by 3 gives us an average of 2, which is correct. This works because each number in the list counts as one value, but when you average a continuous function, you have to sample the function in some systematic way. You get a bigger set of samples and a higher total, so you need to divide by the number of samples.

We are just doing the same with our integral, but we are breaking the curve into a (nearly) infinite number of (nearly) infinitely short segments, each with a length of dx. As usual, dx is a value that is so small that the value of the curve over that length can be considered to be constant. So if we are averaging the values of f(x) from x=a to x=b, we are totaling up badx pieces and dividing the total by badx. In the Mean Value Theorem, Reprised sidebar (earlier in this chapter), we can see an application of this as well.

3D Printing Integrals

In the last two chapters, we looked at 3D printed models that were pairs of either a curve and its derivative, or a curve and its indefinite integral. If we think of it as a curve and its integral, we had to pick one value of the + C constant to be able to print it. For the derivativeintegral.scad model shown in Figure 6-8, the curve, f(x)=x, is parallel to the ground. Its integral, shown vertically, is x2/2+ + C. We picked the constant equal to zero, so that the integral value would start at zero. If you think of the curve increasing steadily with x, with positive f(x) toward you, you would expect the area under it to be proportional to x2.

A 3D printed right-angle model showing a curve and its integral. The curve is a diagonal line with a positive slope, and its integral is an open-upward parabola.

FIGURE 6-8: Curve and integral

Suppose, though, that the curve we are integrating has negative values. We can take area away just like we can add area; you can think of it as cumulative assets over time. Sometimes you might make money, and sometimes have a loss; the integral would be the sum of your profits and losses over some defined time period.

Let’s say our function is a negative-slope straight line that starts in positive territory (Figure 6-9). In this model, the cumulative area under the function starts out at zero (implying a + C offset since our function started off above zero). The integral reaches a maximum where the function goes through zero, and turns downward when it crosses into negative values. If this continued, the integral would eventually go negative as well, but we ended our model before this point to keep it easy to print.

Another right angle derivative-integral model. This one has a diagonal line sloping downward, and its integral is a downward-open parabola.

FIGURE 6-9: Integral of curve with negative slope

The model in Figure 6-8 is the same as that in Figure 4-2, and the model in Figure 6-9 is the same as that in Figure 5-2. This helps make our point that you can use the same models to think about function/integral pairs and derivative/function pairs. Parameters to create these models where they appear in earlier chapters. These models can be constructed with paper instead of 3D printed, as described in Chapter 4.

If you are creating an integral/derivative pair with derivativeintegral.scad, the integrated function (with an appropriate + C) will be f(x), and the original function, d(x).

Integrals of Powers of x

What about all powers of x, though? In Chapter 4 we found that

df(xn)dx=nxn1

Think about how to take this backwards to start at a derivative and find what function would generate it (or, in other words, to integrate). If taking the derivative of xn involves dropping the power of x by 1 and multiplying by n, it follows that integrating (to get back to the same place) would raise the power of x by 1 and divide by the new exponent. Indeed, this is the case:

xn1dx=1nxn+c

However, a new problem arises that we didn’t have in the derivative formula. What happens when n = 0? Or to put it another way, what is the integral of x1, otherwise known as 1/x? Anything raised to the zero power is 1, so if we try to think about inverting the process of taking the derivative of x0=1, we get a derivative of 0. So that doesn’t get us to an integral of x–1. It turns out that this is an exception, and we can more correctly write a pair of equations:

xn1dx=1nxn+c,forn0,andx1dxIn(|x|)+c,x0

Where ln(x) is the natural log of x, the logarithm of x based on Euler’s number, e, which we introduced in Chapter 4. The derivative of ln(x) is indeed 1x so the Fundamental Theorem remains valid. Note that since ln(x)is only defined for x>0, and in addition we can’t divide by zero, we need to have additional constraints on the n = 0 case. The vertical lines around x in the second integral mean absolute value of x.

One thing that might feel a little strange is that you need to think about an equals sign a little differently in calculus than you might have in algebra. Although the two sides of an equation are in fact equal, the intent is that you read them from left to right. It might help to think about an equals sign here as the word “becomes” rather than as something that shows two sides are equal and that you can do the same things to both sides. It is showing transformation more than equality. Sometimes you lose information during this transformation, as when you take a derivative and need to add a constant if you reverse the process by taking an integral. The process is one-way, like baking, sort of, in that original ingredients are there and in principle you could extract them, but information has been lost or gained.

Note that this formula for integrals applies to powers that are not whole numbers. A square root, for instance, can be expressed as raising the quantity under the square root to the 1/2 power. Since you are adding or subtracting 1 to the power (depending on whether you are integrating or differentiating) you will always have a square root in your answer, too. Less-predictable things happen for other non-integer powers, and you will see pages of entries for special square-root cases that come up in integrals. When we talk about integrating by parts in Chapter 13, we will talk about ways to wrangle our way around some of these special cases.

Integrals of Sine and Cosine

As we saw in Chapter 4, sines and cosines are derivatives of each other (with signs to keep track of). If we were to create the model shown in Figures 4-8 and 4-9, and just rotate it the opposite way (Figures 6-10 and 6-11) we would be integrating.

A right-angle cosine and sine model, with the cosine visible.

FIGURE 6-10: A cosine function

Same model as in Figure 6-10, rotated to make sine visible.

FIGURE 6-11: Same model as Figure 6-10 rotated 90 degrees toward the viewer

Turning the top of the model towards you 90 degrees at a time gives subsequent integrals of integrals. There is a catch for integrals, though. If we start off with a cosine curve and turn the model 90 degrees toward us, we get a sine curve. That is as it should be, because

cos(x)=sin(x)+c

However, if we were to turn the model once more in the same direction, to integrate the sine wave, we would get a negative cosine wave.

sin(x)=cos(x)+c

That curve starts at -1. If we add up the area under a sine curve, though, it starts out at zero. We solve this by adding -1 to the integral to sync things up. This will take care of itself if we evaluate a definite integral, but one has to be careful of the + C term when figuring out an indefinite integral to analyze a real-world problem.

We printed the sine and cosine curves shown in Figures 6-9 and 6-10 with the same amplitude, but if we were plotting sin(ax) (multiplying x by a constant, amounting to changing the frequency of the wave), the integral would be

sin(ax)=1acos(ax)+c

and similarly, the integral of cos(ax) is

cos(ax)=1asin(ax)+c

You would need to scale the model appropriately. As in Chapter 4, we printed out the model shown in this section with a=1 to enable rotation in both directions without scaling. See Chapter 4 for the details on printing the model, and experiment with turning it both directions to get an intuition for both the rate of change of the curves and their integrals.

Integrals of Exponentials

In Chapter 4, we saw that the exponential function, f(x)=ex, is its own derivative. Therefore, it is also its own integral (with, as always, a constant offset).

exx=ex+c

If the exponent is multiplied by a constant, it multiplies the result, as you might expect. If the constant is negative, the curve is decreasing steeply, and there will probably be some constant offset that reflects the initial value of the system before it started changing.

eaxx=1aeax+c

If we make a = 0.5, we have exactly the same case as Figure 4-13.

It is a different story, though, when a is negative. That is a decaying exponential: as x gets bigger, the value of the function gets smaller. The integral accumulates progressively more gradually. You can see this in derivativeintegral.scad by using the Customizer pulldown menu to select "e(2*x)/2+.5". This will then (automatically) use the functions:

functionf(x)=exp(2*x)/2+0.5;functiond(x)=exp(2*x);

We added a “+ C” of 0.5 to this indefinite integral to make it start at 0. Use the Customizer to set

xmin:0xmax:2

and leave other variables at their defaults. The resulting model is shown in Figure 6-12.

Right-angle model with decaying exponential, which is approaching zero, and its integral, which rises quickly from zero at first, but rises more slowly as the value of the original exponential gets smaller.

FIGURE 6-12: Decaying exponential function (pointing toward you) and its integral (pointing up)

Application: PID Controllers

It is probably clear that integrals are useful when we are adding up something, as we noted with the example of pouring something into a container. However, there are many more abstract uses as well. Many complex mechanical devices use Proportional Integral Derivative (PID) controllers. Devices like servo motors, self-balancing vehicles, and the heaters in 3D printers’ hot ends all often use PID controllers to control a position, angle, or temperature. Usually controllers are trying to keep the measured quantity near a desired value, called the setpoint.

To understand what PID controllers do and why they are so useful, you first need to understand the simpler alternative, known as a bang-bang controller. Let’s use a temperature controller as an example. A bang-bang controller would read the temperature from a sensor, then do a simple test. Is the temperature lower than the setpoint? If the temperature is lower, it turns on a heater to raise the temperature. If the temperature has reached the setpoint, the heater turns off so that the temperature will stop rising. The temperature will then begin to drop because heat is being lost to the environment, and the controller will turn the heater back on to maintain temperature. It is called that because all it can do is turn off and on, with nothing in-between.

The thermostat in your home probably uses bang-bang control. Not only that, but the controller has a “dead zone” of temperatures where it won’t turn the heater (or the air conditioner) on or off. Temperatures within this range are considered close enough, and switching the heating and cooling on and off within this range would just drive up your energy bills without being noticeable. The dead zone also protects the controller from overreacting to random fluctuations. Your thermostat’s sensor might get a bit hotter or colder momentarily if there’s a breeze from someone opening the door to come or go, but the whole house’s temperature does not change that quickly, and will even itself out in a few minutes. If you turned on the heater the moment the door opened, the temperature would overshoot, and it would get too hot.

These systems usually have some kind of transient fluctuations or noisy data. They also have what an engineer would call delay — that is, the sensors do not immediately see the effects from changing the controller’s output. When your heater turns on, it gets hot right in front of the vent quickly, but the rest of the house takes time to warm up. Putting the sensor right in front of the vent would leave the rest of the house too cold. Putting it far away means that some areas get warm faster than the sensor can detect, and the system will overshoot the temperature you set. For your house, this little bit of fluctuation is fine, but some systems need more precision than this. They need some ability to predict changes, and adjust based on expected future values. This is where PID controllers come in.

PID controllers add together three numbers to create a signal that tells a system how to adjust whatever the controller is managing. These are called the proportional, integral, and derivative terms (Figure 6-13). These are proportional to the current value of (say) temperature, its integral, and its derivative. The PID controller will tell the control system to make a change based on a weighted mix of these three terms:

Temperaturechange(future)=Kp*Temperaturedifferencefromsetpoint + Ki*Integral+Kd*Derivative
A flowchart of PID controllers. The device being controlled generates a signal, which is subtracted from a desired setpoint. This difference is then piped to three different branches. One branch multiplies the value by Kp. Another branch uses the difference to update an integral, then multiplies the integral by Ki. The third branch uses the difference to update a derivative, then multiplies that derivative by Kd. The results of these three branches are then added together. This sum feeds back to the device being controlled, and the process repeats.

FIGURE 6-13: The PID controller

Each of these terms is tweaked by multiplying it by a number called its gain (Kp, Ki, and Kd in the preceding equation) to weight the terms differently, based on experiments or experience with this sort of controller. If all goes well, they add up to make the quantity being controlled reach the desired value quickly and smoothly, while keeping overshooting and oscillation to a minimum, as the environment around them changes.

The proportional term is like a more nuanced version of the bang-bang controller. The proportional term lets the system have more than just “too hot” or “too cold” as an input. If the temperature is a lot lower than you want it to be, it turns the heater on full-blast, just like a bang-bang controller would. When the temperature gets closer to the target though, it reduces power to the heater so that the temperature will rise more slowly. For some systems this is enough to solve the problems with a bang-bang controller.

The problem with a well-tuned proportional-only controller, though, is that the temperature will get closer and closer to the setpoint, but will never quite reach it. We say well-tuned, because in addition to a target temperature, a proportional controller also needs a gain setting. If this is tuned too low, the temperature will rise too slowly (following an exponential decay curve), and take a long time to get anywhere near the target temperature. If it’s too high, the controller acts like a bang-bang controller, overshooting and oscillating around the setpoint (settling into a roughly sinusoidal pattern). The proportional gain setting in a PID controller is referred to as Kp.

The PID’s second term tries to correct for this undershoot. By adding up the error over time, the controller can track the integral of the error. If the temperature has been way too low, or a little too low for a long time, it knows that it’'s not providing enough heating power, and can raise the temperature. This requires a second gain setting, referred to as Ki, which controls how much extra heater power to add based on the integral of the error. In the case of an overshoot, of course, the integral would begin to decrease, and eventually go negative. The proportional and integral terms, multiplied by their respective gain settings, are added together, and one might partially cancel the other if one is positive and the other is negative.

A controller that only uses the P and I terms can solve the problem a P-only controller has of never reaching the target temperature. The new problem is that the integral term can get really big when you start trying to heat a cold room, or if you change the system’s target temperature before the system can catch up. This, again, can cause overshoots. You could just set Ki really low to prevent this, but then you lose most of the advantage. It still takes a long time to reach the target temperature. That’s where the derivative term comes in.

The third part of the controller is the derivative controller, and it too has its own gain setting, Kd. The derivative part of the controller monitors how quickly the temperature is changing. If the temperature is near the setpoint and rising quickly, you can predict that it’s about to overshoot. The Kd parameter can be tuned to reduce the power when the temperature is rising too quickly, and add power when it’s falling quickly, to help the temperature stabilize faster.

Other types of devices using PID controllers, like motors trying to hold a position or balance a vehicle’s rider, may not have the same delay to deal with, but they do have to contend with momentum. Once you get the motor turning or the rider tipping, you may need to stop powering the motor early or give it some power in the other direction to keep it from turning too far.

You might wonder how we would know the equations for the derivative and integral. We do not — but we can compute them based on data. Starting up a system like this obviously takes some thought, too. Philosophically, it’s not that different from how we created derivatives and integrals from LEGO bricks in Chapter 1.

Experiments to Try

Now that you have seen examples of various types of integrals, you can use derivativeintegral.scad to test out your ability to compute various integrals. First try out computing a definite integral manually. Then, you can use OpenSCAD to see if you computed the equation for the integral correctly. Remember that to check an integral, custom_d(x) is the original function, and custom_f(x) is the integral. xmin and xmax are what we were calling a and b, respectively. For example, this is what these changes would look like for the model in Figure 6-12, had we used a custom function for it.

functioncustomf(x)=0.5+(1/2)*exp(2*x);functioncustomd(x)=exp(2*x);xmin=0;xmax=2;

You will need to pick reasonable values of xmin and xmax so that the model is not tiny or enormous, if you plan to actually 3D print it. As we discussed in Chapter 4, if your calculations are correct, the blue and pink models should overlap completely (Figure 6-14). You can 3D print these models, print them on paper (Chapter 4), or just check them out in OpenSCAD.

Image

FIGURE 6-14: OpenSCAD curve and derivative models overlapping correctly

Chapter Key Points

In this chapter, we learned a lot of different ways to accumulate quantities. We introduced the idea of definite and indefinite integrals, and we focused on their intuitive meaning and a bit about how to compute them. We learned the second part of the Fundamental Theorem of Calculus, which allows us to get a numerical answer for an integral when we know the endpoints. We also saw specific examples of how to compute simple integrals (for powers of x, sine, cosine, and exponentials). We also learned about useful applications of integrals, like finding the area under a curve, and the average of part of a complicated curve. Finally we saw that the concepts in the last several chapters can be combined in PID controllers, which are used to manage many useful devices.

Terminology and Symbols

Here are some terms from the chapter you can look up for more in-depth information:

  •   Antiderivative
  •   Definite integral
  •   Indefinite integral
  •   Integral, ∫ ( . ..) dx
  •   PID controller

References

  • Thomas, G. B., Hass, J., Heil, C., & Weir, M. D. (2018). Thomas' Calculus (14th ed.). Pearson.
  • Zwillinger, D. (Ed.). (2018). CRC Standard Mathematical Tables and Formulas (33rd ed.). CRC Press.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset