Calculus asks how fast something changes and how much it has added up to. In this course the computer answers both from numbers. You find slopes with the forward and the central difference and with np.gradient, and learn why the best step is not the smallest. You find areas with rectangles and trapezoids, integrate with SciPy, including functions with no antiderivative and infinite limits, and find the work of a changing force. You follow a motion in small steps with Euler's method, see that it is first order, and use solve_ivp. A project follows a ball through the air with drag, stops it at the ground with an event, and finds that the best angle is lower than 45 degrees. The practice runs your code and tells you which results are wrong.
5 lessons, about 3 h 10 min5 simulations10 problems10 practice questionsPython in every lesson
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.
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.
scipy.integrate.quad integrates a function of one variable between two limits and returns the value and an estimate of its error, usually to about 10 digits, by choosing its own pieces. It works for functions that have no formula for their antiderivative, such as e to the minus x squared, and for infinite limits written with np.inf, so the integral of e to the minus x squared over the whole line is the square root of pi. The work of a variable force is the integral of the force over the distance, for a spring from a to b it is one half k times b squared minus a squared, and for a stiffer spring the same call gives the work at once. quad takes a function of one number, and the answer should be checked against a case with a known value.
A differential equation gives the rate of change of a quantity, for example the acceleration of a falling ball as g minus k times the speed. Euler's method follows it in small steps: the new value is the old value plus the rate of change times a small step dt, and the new position uses the old speed. For a ball that falls freely, 3 seconds in steps of 0.1 give the exact speed but a position that is too small, 42.63 metres against 44.1. With air drag the speed grows towards the terminal speed g over k. Euler's method is first order: half the step makes the error half, and a big step makes the curve wrong. scipy.integrate.solve_ivp solves the same equation with steps that it chooses, for a function of the time t and the state y that returns the rates of change.
A project that uses the whole chapter: a ball is thrown at 40 metres per second at 45 degrees, and air resistance makes the force on it proportional to the square of its speed. The state is the position and the velocity, four numbers, and solve_ivp follows them with a rate function. An event function that returns the height, with terminal true and direction minus 1, stops the solution when the ball reaches the ground. Without air the range is 163.3 metres, equal to v squared over g, and with the drag constant 0.0089 per metre the range is only 82.5 metres, the flight is shorter and the height is lower. A scan of the angles shows that the best angle without air is 45 degrees and with air it is lower, 40 degrees.
Log in to join the discussion.
Loading…