Course 2.1Lesson 5 of 5
SciPy has ready tools for the methods of this chapter. scipy.optimize.brentq solves an equation f of x equal to 0 on a bracket with a change of sign, as fast as the best methods and as safe as bisection. scipy.optimize.curve_fit finds the numbers of any model function that fit measured data best, and returns the best values and a covariance matrix whose diagonal gives their uncertainties. With a sigma of the square root of the counts, a fit of the exponential decay n0 times e to the minus lambda t to the counts of a radioactive sample gives the decay constant, and the half-life is ln 2 over lambda, about 20 seconds in the example. The same half-life comes from a straight line through the logarithms of the counts, and brentq finds the time when the counts reach 100.
Try the code and the simulations beside the lesson.
After this lesson you can
brentq,curve_fit and read the uncertainties, andSciPy is the library of scientific tools that is built on NumPy. It has methods for integration, optimisation, statistics and much more. You use it in the same way as the other libraries, and on this site it is ready. The first time that your code imports it, the site needs a moment to load it. We use the part scipy.optimize.
Predict first. The first equation is the one that you solved by bisection. What will the answer be? Then run it.
brentq(f, a, b) finds a root of f on the bracket . Like bisection, it needs a change of sign. Unlike bisection, it uses faster methods inside, and takes few steps, but it keeps the root inside the bracket, so it cannot fly away like Newton's method.lambda is a short function without a name, written in one line: lambda x: x ** 3 - 2 * x - 5.Rule of thumb: to solve an equation in one unknown, find a bracket, and call brentq.
In the last lesson, polyfit fitted a polynomial. But many laws are not polynomials. Radioactive decay follows , where is the count at time , is the count at the start, and is the decay constant. curve_fit fits any formula to data. You write the formula as a function, and give it the data.
Predict first. The counts fall from 1000 to about half in 20 seconds. What do you expect for the half-life? Then run it.
Now we put the pieces together to answer three questions about a radioactive sample: what is its half-life, does a second method agree, and when will the count fall to 100 per second?
Predict first. The curve falls to half in about 20 seconds, so after 60 seconds it should be about one eighth of the start. When will it be at 100? Then run it.
Notice how the chapter fits together: the fit gives the model, the solver answers a question about the model, and the graph shows the result. This is how a real analysis goes.
brentq finds the time where the count is half by searching a bracket, like bisection, only faster. It agrees with ln 2 / λ.
A fit always gives numbers, even for a bad model. Always ask three questions. Does the curve go through the error bars on the graph? Are the uncertainties of the numbers small compared to the numbers? Does the result make sense for the physics (is the half-life positive and in the range that you expect)? A fit that gives a half-life of 20.2 s with an uncertainty of 0.5 s is a result. One with an uncertainty of 20 s says that the data cannot tell.
brentq an interval without a sign change. It raises a ValueError.curve_fit, the variable is the first argument.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:
brentq(f, a, b) solves on a bracket. It is safe and quick.curve_fit(model, x, y, p0=...) fits any model. The variable is the first argument of the model.np.sqrt(np.diag(cov)).Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
curve_fit and not polyfit?cov?brentq?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.