Slarti Bart Fass
Slarti Bart Fass

Reputation: 11

Linear fit with scipy.optimize.curve_fit

I just started programming last week, so please be gentle ;)

What I try to do is a linear fit with curve_fit to determine the two contributions to the slope. I tried:

import os
from os import listdir
from os.path import isfile, join
from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.axes as ax 
from scipy import asarray as ar,exp

freq_lw_tuple = [('10.0', 61.32542701946727), ('10.5', 39.367147015318501), ('11.0', 58.432147581817077), ('11.5', 68.819336676144317), ('12.0', 71.372078906193025), ('12.5', 73.113662907539336), ('13.0', 75.855316075062603), ('13.5', 76.798724771266322), ('14.0', 79.065657225891329), ('14.5', 81.637345805693897), ('15.0', 82.407248034320034)]
def func_lw(x_lw,alpha,g):
    return (alpha/g)*x_lw

#define constants
m_e = 9.109383*(10**(-31)) #mass of electron in [kg]
q_e = 1.602176*(10**(-19)) #value of electron charge
pi = 3.14159               #pi
#unzip the list of tuples
unzipped = list(zip(*freq_lw_tuple))

xval_list = []
yval_list = []
for k in range(0,len(unzipped[0])):
    x_value = (8/np.sqrt(3))*((2*pi*m_e*float(unzipped[0][k])*10**9)/q_e)   #calculate x values
    xval_list.append(x_value)
    y_value = unzipped[1][k]*10**(-4)*4*pi*10**(-7)    #transform unit of y values
    yval_list.append(y_value)


start_params2 = [0.01,2]
fitted_params, pcov = curve_fit(func_lw, xval_list, yval_list, start_params2)

And it actually gives some results, but when I want to

print(func_lw(xval_list,*fitted_params))

I just get an empty list and probably that is the reason why I can't

plt.plot(xval_list, func_lw(xval_list, *fitted_params))

(This gives an error like: x and y must have same first dimensions)

[Edit: added some data for freq_lw_tuple and imports]

Upvotes: 0

Views: 4913

Answers (1)

Hugues Fontenelle
Hugues Fontenelle

Reputation: 5435

Python cannot multiply lists and scalars. That's why you imported from scipy import asarray as ar. You could have used numpy as well. So when you call func_lw you should give it an array, not a list.

func_lw(ar(xval_list), fitted_params[0], fitted_params[1] )
plt.plot(ar(xval_list), func_lw(ar(xval_list), *fitted_params))
plt.scatter(ar(xval_list), ar(yval_list))

Regarding the optimisation function, you really only have one parameter (the slope) if you use (alpha/g)*x_lw in the formula. I'd use instead

def func_lw(x_lw, slope, offset):
    return slope*x_lw + offset

Edit: By the way, you should use "list comprehension", and do something like

xval_list = [(8/np.sqrt(3))*((2*pi*m_e*float(x)*10**9)/q_e) for x, y in freq_lw_tuple]
yval_list = [y*10**(-4)*4*pi*10**(-7) for x, y in freq_lw_tuple]

Upvotes: 1

Related Questions