Jabbar
Jabbar

Reputation: 99

fitting a tanh curve and estimating parameters

I have two sets of data (say x and y). I want to fit a tanh curve (hyperbolic tangent) for y=x-a-btanh[(x-a)/b)] and return a and b parameters. I tried the follwing code, but get an error, TypeError: 'numpy.ufunc' object is not subscriptable. Also, I want to know if the following function is a right way to fit a tanh curve. Thanks!

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

x=[799.0,872.0,882.0,986.0,722.0,479.0,747.0,510.0,1012.0,1114.0,801.0,662.0,853.0,878.0,779.0,987.0,707.0,756.0,730.0,523.0,802.0,1068.0,780.0,927.0,923.0,790.0,852.0,580.0,730.0,924.0,762.0,662.0,927.0,828.0,866.0,951.0,949.0,740.0,677.0,704.0,798.0,791.0,761.0]

y=[27.64,110.75,139.34,66.02,26.66,5.29,11.75,3.81,64.07,286.23,57.23,43.61,18.68,55.4,65.97,206.51,16.32,19.25,10.98,3.12,15.41,122.89,62.43,112.18,34.86,46.59,48.96,3.69,11.3,49.92,18.96,6.57,32.74,35.76,46.35,115.04,131.46,46.63,26.25,4.55,9.21,36.32,11.47]  


def myfunc(x,a,b):
    return x-a-b*np.tanh[(x-a)/b]

 popt, pcov = curve_fit(myfunc, x, y)
 plt.plot(x, myfunc(x, *popt))

Upvotes: 0

Views: 553

Answers (1)

Paul H
Paul H

Reputation: 68116

The problem is here: np.tanh[(x-a)/b]

That might be the mathematical notation, but it's not valid python syntax.

Function are called with parentheses (). So it should be: np.tanh((x-a)/b)

Upvotes: 1

Related Questions