Plot of scattered data
I need to fit (x,y)-data to an equation with two variables (the x's and the y's) and retrieve the 5 unknown parameters.
I am making a script to treat IV-data (current-voltage) from a simple .txt-file and fit it to an equation, called the non-ideal diode equation; this is an implicit non-linear function.
So far I've opened the file with python, sorted the data into numpy arrays, made a scatter plot of the raw data, and I know how the function to be fitted for should look. I tried defining the equation, and tried the SciPy functions fsolve and curve_fit, but yet without luck (maybe I'm just bad at using them).
What I need is simply to fit the data to the following equation, retrieve the parameters, and plot the actual curve:
y = a - b * (np.exp((x - y * d) / c) - 1) - (x + y * d) / e
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize
OpenFile = pd.read_csv("test.txt", sep="\t", header=0)
FileArray = np.array(OpenFile)
x = FileArray[:, 0]
y = FileArray[:, 1] * 1000.0 / 0.08
plt.scatter(x, y)
def diode(data, a, b, c, d, e):
v, j = data
return a - b * (np.exp((v - j * d) / c) - 1) - (v + j * d) / e - j
### FAILED SCIPY OPTIMIZE ATTEMPT ###
parameters, parameterscovariance = optimize.curve_fit(diode, (x,y), y,
bounds = ([0, 0, 0, 0, 0],
[np.inf, np.inf, np.inf, np.inf, np.inf]),
max_nfev=10000)
plt.plot(x, diode((x,y), parameters[0], parameters[1], parameters[2], parameters[3], parameters[4]))
plt.show()
The code plots the data points, but the diode-equation needs to be optimized, parameters retrieved, and the optimized equation plotted.
EDIT: Now inserted the attempt to make scipy optimize the implicit function