Day-of-year temperature take two

The quadratic polynomial approximation for the day-of-the-year temperature fit can be improved upon. We haven't used any of the NumPy trigonometric functions until now. Those should be a good fit for this problem. So, let's try a trigonometric function and fit again using a function from the scipy.optimize module (leastsq to be precise) as follows:

  1. Set up a simple model function and an error function to be minimized, as shown in the following code snippet:
    def model(p, d):
       a, b, w, c = p
       return a + b * np.cos(w * d + c)
     
    def error(p, d, t):
       return t - model(p, d)
  2. Give the initial guess and fit the data:
    p0 = [.1, 1, .01, .01]
    params = leastsq(error, p0, args=(days, temp))[0]
    print params

    We get the following parameters: ...

Get Learning NumPy Array now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.