Slopes from numbers · Python Programming Class 11–12 | StemPy Lab
Course 2.2·Lesson 1 of 5
Slopes from numbers
The derivative of f at x is the slope of the tangent, and a computer finds it from numbers: the slope through the points at x and x plus h is the change of f divided by h. This forward difference has an error that shrinks in proportion to h, while the central difference, the slope through x minus h and x plus h, has an error that shrinks in proportion to h squared and is far better. A smaller h is not always better, because the computer rounds its numbers, and the best h for the central difference is about 1e-5. For measured data, np.gradient of the values and the x values gives the slope at every point, with the ends less exact unless edge_order is 2. A turning point of a curve is where the slope changes sign.
about 45 min1 simulation2 practice questions
Lesson 1 of 5Show all lessonsHide
Try the code and the simulations beside the lesson.
What you will be able to do
After this lesson you can
find the slope of a curve from numbers, without a formula,
tell why the central difference is better than the forward difference,
use np.gradient on measured data, and
find the turning points of a curve.
The slope of a curve
In your calculus class, the derivativef′(x) is the slope of the tangent to the graph of f at the point x. For f(x)=x3−3x, the rules give f′(x)=3x2−3. But what if you have no formula? An experiment gives you a list of numbers: the positions of a car at each second. A computer can still find the slope, from the numbers alone.
The idea is old. A tangent touches the curve at one point, but a secant line cuts it at two points, and a secant line is easy: its slope is the change of f divided by the change of x. When the two points come close, the secant becomes the tangent.
The graph of the sine of x between 0.3 and 2.9, with a black dot at x equal to 1. A black tangent line at the dot has the slope 0.540. Three dashed lines through the dot and a second point on the curve show slopes from two points: h equal to 1.2 in red has slope minus 0.027, h equal to 0.6 in orange has slope 0.264, and h equal to 0.25 in green has slope 0.430. The smaller h is, the closer the dashed line is to the tangent.
f′(x)≈hf(x+h)−f(x)
for a small h. The picture shows f(x)=sinx at x=1. The tangent has the slope cos1=0.540. The secant with h=1.2 is very different (slope −0.027), but with h=0.25 it is already close (0.430).
The forward difference
Predict first. When h is ten times smaller, how will the error change? Then run it.
The error is ten times smaller when h is ten times smaller. It is proportional to h. This is called a first-order method. To get 6 correct digits, we would need h of about 10−6.
The central difference
We can do better with the same effort. Instead of looking only to the right of x, look at both sides: the secant through x−h and x+h.
f′(x)≈2hf(x+h)−f(x−h)
Predict first. The forward error at h=0.001 is 0.00042. What do you expect for the central difference?
Remember: the central difference is nearly always the better choice.
Halve h and watch the errors: the forward error about halves, the central error falls to about a quarter.
A smaller h is not always better
If a small h is good, is the smallest h the best? Let us try.
The lesson: a numerical derivative has two kinds of error. Choose h that is small, but not tiny.
Slopes from measured data
When you have measured values, you cannot choose h: it is the gap between the measurements. NumPy has the function np.gradient, which uses the central difference at every inner point, and a one-sided rule at the two ends.
Predict first. The positions are x=2t2, so the exact speed is v=4t. Which values will be exact?
Real data has errors, and the derivative magnifies them: a small jump in the data gives a big jump in the slope. This is another reason not to use a tiny gap, and to look at the results with care.
Finding the turning points
At the top or the bottom of a curve, the slope is zero. So the slope changes sign there. This is the same sign-change test as in chapter 3.
A blue curve, f of x equal to x cubed minus 3x, rises to a top at x equal to minus 1, falls to a bottom at x equal to 1, and rises again. An orange dashed curve, the slope of f found from numbers, is a parabola that is positive on the left, crosses zero at minus 1, is negative between, crosses zero at 1, and is positive again. Red dots mark the turning points of f where the slope is zero.
This is how a computer finds the highest point of a path, the cheapest design, or the best angle, when no formula is available.
Common mistakes
Using the forward difference when a central one is possible.
Choosing h as small as possible. Rounding errors grow.
Forgetting to give the x values to np.gradient. Without them, it assumes a gap of 1.
Believing the two end values. They are less accurate.
Using the slope of noisy data without looking at it. The noise is magnified.
Practice
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:
Warm-up. Open the lab Derivative from numbers. You write the central difference as a function.
Build. Open the problem Speed from positions and the problem The top of the path.
Remember these
The slope of a curve is about hf(x+h)−f(x). It is first order: the error is proportional to h.
The central difference 2hf(x+h)−f(x−h) is second order: the error is proportional to h2.
A tiny h brings rounding errors. The best h for the central difference is about 10−5.
np.gradient(values, x) gives the slope at every point.
A turning point is where the slope changes sign.
Check yourself
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
The forward difference has an error of 0.02 at h=0.1. What do you expect at h=0.01?
The central difference has an error of 0.02 at h=0.1. What do you expect at h=0.01?
Why do the end values of np.gradient have larger errors?
How do you find the turning points of a curve from numbers?
Enroll free in Calculus by Computer
Every lesson is open to read. Enrolling also lets you:
Mark lessons done and carry on where you stopped.
Take the tests of this lesson and its course.
Count the course towards the certificate for its chapter.