Krystal
Krystal

Reputation: 43

How to fix a parameter when fitting with optimize.curve_fit?

I would like to fix a parameter using optimize.curve_fit. This is my code:

def sinefunction2(x, a, b, c, phi, omega):
    return a+ b * np.sin(omega*x + phi) + c*(np.sin(omega*x + phi))**2
phi = how to fix?
x= scan_no
y= fwhm_r
p0=[0.05, 0.1, 0.01, phi, 0.12] 
params2, params_covariance2 = optimize.curve_fit(sinefunction2, scan_no, \
           fwhm_r, p0, sigma=error_r, absolute_sigma=True)

How can I fix phi param?

Thank you.

Upvotes: 0

Views: 305

Answers (1)

Muhammad Mohsin Khan
Muhammad Mohsin Khan

Reputation: 1476

Just include it as a variable inside your function and set it to a constant value, so that every time the function is called, it will have that value. Also, remove phi as an argument from the function.

def sinefunction2(x, a, b, c, omega):
    phi = 1 # for e.g.
    return a + b * np.sin(omega*x + phi) + c*(np.sin(omega*x + phi))**2

x= scan_no
y= fwhm_r
p0=[0.05, 0.1, 0.01, 0.12] 
params2, params_covariance2 = optimize.curve_fit(sinefunction2, scan_no, fwhm_r, p0,
                                                 sigma=error_r, absolute_sigma=True)

Upvotes: 1

Related Questions