Course 2.2Lesson 3 of 5
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.
Try the code and the simulations beside the lesson.
After this lesson you can
scipy.integrate.quad,With trapezoids, you choose the number of pieces, and the answer is as good as the number. SciPy has a method that chooses its own pieces, finer where the function changes quickly and coarser where it is smooth, until the answer is accurate to about ten digits.
Predict first. What is the area under from 0 to ? Then run it.
quad(f, a, b) integrates the function f from a to b. It gives two numbers: the value and an estimate of the error. The error estimate is tiny here, below , which is shown by the True.f must take one number and give one number. NumPy functions such as np.sin are fine. A lambda is the easy way for your own formulas: lambda x: x ** 2.quad finds it at once: 0.746824. This is the kind of integral for which numerical methods are indispensable.quad can fail if the integral does not exist. For , the area is infinite, and quad gives a warning and a wrong number. If a result looks strange, think about whether the integral exists.
When a force is constant, the work over a distance is . When the force changes with the position, the work is the integral of the force over the distance: the area under the force graph.
Predict first. A spring with N/m is stretched from 5 cm to 15 cm. The force goes from 2.5 N to 7.5 N. What is the work, roughly?
The method does not care how hard the formula is.
The red box of height 1 and width 1/k has exactly the area under the whole curve: what the tail adds far out is what the box adds near the start.
quad.np.trapezoid(values, x).quad gives two numbers. value, error = quad(...).quad calls the function with one number at a time.quad a function that needs a list. It calls the function with one number at a time.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:
quad and check them.quad(f, a, b) gives the value and an error estimate. It chooses the pieces by itself.np.inf).quad for a formula and np.trapezoid for measured data. Always test on a known case.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
quad gives?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.