Course 5.1Lesson 3 of 3
Real counts are scattered, so a model is fitted to them. For exponential growth, a straight line through against gives the growth rate as its slope, and np.polyfit finds it. For logistic growth, scipy's curve_fit searches for the r and K that make the curve pass closest to the counts.
Try the code and the simulations beside the lesson.
In a laboratory, you do not know or . You count cells at regular times and then ask: which curve fits these counts? The model you fit tells you the growth rate, the doubling time and the carrying capacity.
Here is a culture counted every minutes:
| Time (min) | Cells |
|---|---|
| 0 | 1200 |
| 30 | 2310 |
| 60 | 4650 |
| 90 | 8800 |
| 120 | 18100 |
| 150 | 35400 |
Each count is about twice the one before, so the doubling time is near minutes. A fit makes "about" precise.
Take the natural log of :
This is a straight line in . Its slope is , and its intercept is . So fit a straight line to the points :
np.polyfit(x, y, 1) finds the straight line of degree that passes closest to the points. For the counts above, the slope is per minute, so the doubling time is minutes. The intercept gives .
A fit chooses the line that makes the sum of squared misses as small as possible. For each point, the miss is the count minus the value of the curve at that time. Squaring makes every miss positive and punishes large misses most. This is the method of least squares, and both np.polyfit and curve_fit use it.
A logistic curve is not a straight line, even on a log axis. So we let scipy search. First write the model as a Python function. The first argument is the time, and the others are the numbers to find:
curve_fit(model, t, N, p0=[...]) returns the best values in the order of the model's arguments, here r and then K;cov, which tells how uncertain those values are;p0 is a starting guess. Without a sensible guess, the search can wander off and fail.Here is fixed at the first count, cells, so only and are searched. For the yeast counts the fit gives per hour and cells.
Read them off the data. is a little above the last counts, where they level off. For , use the doubling at the start: here the count roughly doubles every hours, so is near per hour.
The program fits a straight line to for the culture, then a logistic curve to the yeast counts, and plots the fit.
np.log10 and then . With np.log10, the slope is . Use np.log throughout.curve_fit, the time must be the first argument.p0, curve_fit starts every number at , and the search may fail.np.polyfit(t, np.log(N), 1) gives the slope and the intercept .curve_fit(model, t, N, p0=guess) returns the best parameters, in order, and their uncertainties.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
def model(r, K, t):. Why does curve_fit give nonsense with it?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.