1
votes

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

1
Please include your best attempt at the SciPy implementation. This is likely to get you more help, and will help you show up on searches.Prune

1 Answers

0
votes

It would be helpful to post the data somewhere and to give the nature of the failure you get. That is, does Python give an exception, does the fit give an error message, or does the fit run to completion but the fit is simply "not good"?

You definitely want to give initial values for fit parameters. It is beyond comprehension that scipy.optimize.curve_fit() allows users to not specify starting values -- it should be an error to not specify initial values. Non-linear curve fitting problems are generally not global optimizations, work by refinement of the starting values, and are often sensitive to initial values (especially when an exponential decay is involved). FWIW, the initial values used when you do not explicitly state the initial values is "1" for all parameters. Is this a good default value? No, it is not.

I also think you have a potentially more serious issue. Your model for "y" is transcendental: "y" depends on "y". I don't recognize the formula you're using, but I can believe the I-V curves for diodes are transcendental. Unless your value for your d parameter is << 1, I think your model will be unstable. You may want to ensure that d is bounded to be << 1, and almost certainly do not want to start with d=1.

That is probably not the sort of answer you were looking for, but I hope it helps get you on the right path.