Fixing curve with python error

I am trying to put my data into (cos(x))^n

. A dollar is n

in theory 2, but my data should give me about 1.7. When I define a fitting function and try curve_fit

, I get the error

def f(x,a,b,c):
   return a+b*np.power(np.cos(x),c)

param, extras = curve_fit(f, x, y)

      

This is my data

x   y               error
90  3.3888756187    1.8408898986
60  2.7662844365    1.6632150903
45  2.137309503     1.4619540017
30  1.5256883339    1.2351875703
0   1.4665463518    1.2110104672

      

The error looks like this:

/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:4: RuntimeWarning: Invalid value found after including cwd from sys.path.

/usr/lib/python3/dist-packages/scipy/optimize/minpack.py:690: Optimization Warning: Parameter covariance cannot be estimated
category = OptimizeWarning)

+3


source to share


1 answer


The problem is that it cos(x)

can become negative and then cos(x) ^ n

undefined. Illustration:

np.cos(90)
-0.44807361612917013

      

and for example

np.cos(90) ** 1.7
nan

      

This results in two error messages.



It works great if you modify your model eg. before a + b * np.cos(c * x + d)

. Then the graph looks like this:

enter image description here

The code can be found below with some inline comments:

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit


def f(x, a, b, c, d):

    return a + b * np.cos(c * x + d)

# your data
xdata = [90, 60, 45, 30, 0]
ydata = [3.3888756187, 2.7662844365, 2.137309503, 1.5256883339, 1.4665463518]

# plot data
plt.plot(xdata, ydata, 'bo', label='data')

# fit the data
popt, pcov = curve_fit(f, xdata, ydata, p0=[3., .5, 0.1, 10.])

# plot the result
xdata_new = np.linspace(0, 100, 200)
plt.plot(xdata_new, f(xdata_new, *popt), 'r-', label='fit')
plt.legend(loc='best')
plt.show()

      

+4


source







All Articles