Course 2.2Lesson 2 of 5
The area under a curve is found by cutting it into narrow pieces and adding them. Rectangles at the left end give an area that is too small for a rising curve, rectangles at the right end give an area that is too big, rectangles at the middle give a much better result, and trapezoids, which join the two ends by a straight line, give nearly as good a result. For the trapezoid rule, doubling the number of pieces makes the error four times smaller. np.trapezoid of the values and the x values adds the trapezoids and works for data at uneven times, and scipy.integrate.cumulative_trapezoid gives the running total. The distance travelled is the area under a speed graph.
Try the code and the simulations beside the lesson.
After this lesson you can
np.trapezoid.In chapter 2, you found a distance from a speed graph as the area under the graph. This idea is very general. The area under a force graph is work. The area under a power graph is energy. The area under a curve of the rate of flow is the volume that flowed. In calculus, such an area is a definite integral. We learn to find it by calculation.
The area under from 0 to 1 is (you can find it by integration). Cut the interval into equal pieces. Then replace the curved top of each piece by something easy.
Predict first. Which sum will be too small, and which too big? Which will be the nearest?
The output, for :
The left and right sums are off by about 5 percent. The middle and trapezoid sums are about 30 times better with the same ten pieces. The last line shows that np.trapezoid(values, edges) gives the same as our trapezoid sum. That function adds the trapezoids for you.
Predict first. When the number of pieces doubles, by how much will the error change?
(The left and right rectangle sums are only first order: their error is halved when doubles. That is why trapezoids are the better first choice.)
With 7 pieces the error is already below 0.04. The code below lets you try 100 or 1000 pieces.
The trapezoid rule has a big advantage: it needs no formula, only the pairs of values and positions. And the pieces need not be equal.
Notice how this is the reverse of the last lesson. The slope of the position graph is the speed. The area under the speed graph is the distance. Differentiation and integration undo each other.
np.trapezoid only the values. Give the x values too, unless they are one apart.np.trapz. In the new NumPy, the function is called np.trapezoid.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
np.trapezoid(values, x) works for uneven gaps. cumulative_trapezoid gives the running total.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
cumulative_trapezoid give for a speed graph?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.