0

Hi So I am fairly used to python but this is the first time I am using python for data analysis and I was wondering if you could shed some light on an issue I am having.

I need to fit about 4000 different plots to the following function : b+e*A*(1.19104*(10**-16))*((x*(10**-9))**-5)*((-1+np.exp(0.0143878/(T*x*(10**-9))))**-1) in this function I want to restrict b, e and A to certain values for each plot and the value of the variable T to shift according to the data.

I tried using scipy optimize but I couldn't figure out how to hold parameters with that one.

I tried using pyAstronomy funcfit but for one it is extremely inefficient (or maybe my code is idk) and I wasn't getting data that looked what I had approximated the data should look like.

Finally I am currently trying to use lmfit but my code here seems to have all of the parameters stay the same as the guess and not vary at all. I am confused on how to proceed. my current code looks like this...

def bbnm(x,b,e,T,A):
   y = b+e*A*(1.19104*(10**-16))*((x*(10**-9))**-5)*((-1+np.exp(0.0143878/(T*x*(10**-9))))**-1)
   return y     

params = bbmodel.make_params(b=0,e=.99,T=5100,A=wgeometry)
params['b'].vary = False
params['e'].vary = False
params['A'].vary = False
params['T'].vary = True
bb = bbmodel.fit(power[1465:2510],params,x=wavelength[1465:2510])
1
  • 1
    3 comments: a) post a complete, minimal example, including output, b) plot your data and initial model to see if the model and starting parameter values are even close to reasonable, and c) avoid having to scale params and data by many orders of magnitude in the fit -- rescale the variables and data if necessary. Commented Jul 12, 2018 at 2:23

1 Answer 1

1

Here is code to restrict scipy's curve_fit parameters to within specified bounds. In this example, the first parameter's bounds are +/- infinity (unbounded), the second parameter's bounds are +/- 100 but the fitted parameter is within the bounds and fitted normally, and the third parameter is restricted by its bounds.

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

xData = numpy.array([5.0, 6.1, 7.2, 8.3, 9.4])
yData = numpy.array([ 10.0,  18.4,  20.8,  23.2,  35.0])


def standardFunc(data, a, b, c):
    return a * data + b * data**2 + c


# some initial parameter values - must be within bounds
initialParameters = numpy.array([1.0, 1.0, 1.0])

# bounds on parameters - initial parameters must be within these
lowerBounds = (-numpy.Inf, -100.0, -5.0)
upperBounds = (numpy.Inf, 100.0, 5.0)
parameterBounds = [lowerBounds, upperBounds]

fittedParameters, pcov = curve_fit(standardFunc, xData, yData, initialParameters, bounds = parameterBounds)

# values for display of fitted function
a, b, c = fittedParameters

# for plotting the fitting results
xPlotData = numpy.linspace(min(xData), max(xData), 50)
y_plot = standardFunc(xPlotData, a, b, c)

plt.plot(xData, yData, 'D') # plot the raw data as a scatterplot
plt.plot(xPlotData, y_plot) # plot the equation using the fitted parameters
plt.show()

print('fitted parameters:', fittedParameters)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.