0

I'm trying to fit some data to a nernst curve, but the fitted line has odd angles that don't fit either the data or the equation. Do you know why this is?

csv_file='031109twohemesplit.csv'
data = pd.read_csv(csv_file)
Eh=data['Eh']
Fracred=data['frac red']
tmp_Fracred=Fracred-Fracred.min()
normFracred = tmp_Fracred / tmp_Fracred.max()
plt.scatter(Eh,normFracred)
def twopot(Eh, Em1, Em2, z):
    return 0.5/(10**((Eh-Em1)/(8.14*275.15/(1*96.485)))+z)+0.5/(10**((Eh-Em2)/(8.14*275.15/(1*96.485)))+z)
guess=[-250,-325, 1]
popt, pcov=curve_fit(twopot,Eh,normFracred,guess,method='dogbox')
print(popt)
plt.plot(Eh,twopot(Eh,*popt), label='fitted')

It yields this:

enter image description here

finefoot
  • 9,914
  • 7
  • 59
  • 102
pythonoob
  • 11
  • 2

1 Answers1

2

The "odd corners" happen, because of this command:

plt.plot(Eh,twopot(Eh,*popt), label='fitted')

You're feeding the twopot function only values from your data points in Eh. So in between two points, your curve will just be a straight line which results in "odd corners".

To plot your fitted curve, instead of the values of Eh, use generated values from an appropriate np.linspace over the same range as Eh:

x = np.linspace(min(Eh), max(Eh), 200)
plt.plot(x, twopot(x,*popt), label='fitted')

The level of "detail" of your curve is determined by how many points you select to generate with the np.linspace command. In the example above, it's 200 values.

finefoot
  • 9,914
  • 7
  • 59
  • 102