Scipy.optimize.curve_fit返回初始猜测

时间:2019-12-06 00:59:23

标签: scipy-optimize

尝试使用Scipy.optimize.curve_fit来计算二极管方程式的一些变量。该方程本身包含因变量的多个实例,因此使用Wolfram Mathematica重新排列它。我使用试错法来找到一个近似拟合,已将其作为curve_fit函数中的猜测,但它只是将猜测作为输出返回。使用的数据如下:

输入:

[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3] 

输出:

[-1e-06, -1.e-06, -8.e-07, 1.e-06, 2.e-05, 2.e-04, 3.e-03, 3.e-02, 2.e-01, 1.0, 2.0, 5.0, 7.0, 7.0]

代码:

#####LIBRARIES
import numpy as np
import matplotlib.pyplot as plt
import pandas
from scipy import optimize
from scipy import special
from scipy import constants

#####DEFINITIONS
dataFile = r"C:\Users\Paul\Documents\University\Year 3\EEE381 - Individual Project\Documents\Test 
data PN 2019.csv"

dataframe = pandas.read_csv(filepath_or_buffer = dataFile, sep = ',', header = 0)
diode_voltage = dataframe["Bias"]
diode_current = dataframe["Id-RT"]

rows, columns = dataframe.shape

series_resistance_guess = 5e-2
shunt_resistance_guess = 1e8
ideality_factor_guess = 1.35
photocurrent_guess = 1e-7
temperature = 300
dark_current = 1e-10

guess = np.array([series_resistance_guess, shunt_resistance_guess, ideality_factor_guess, 
photocurrent_guess])

#####MAIN
 def include_constants(temperature, dark_current, rows):

    def my_curve_function(diode_voltages, series_resistance, shunt_resistance, ideality_factor, 
    photocurrent):
        currents = np.empty(rows)

        for diode_voltage in diode_voltages:

            term_a = diode_voltage - shunt_resistance * (photocurrent + dark_current)
            term_b = series_resistance + shunt_resistance
            term_c = constants.Boltzmann * ideality_factor * temperature
            term_d = constants.elementary_charge * series_resistance
            term_e = dark_current * constants.elementary_charge * series_resistance * shunt_resistance
            term_f = constants.Boltzmann * ideality_factor * temperature * (series_resistance + shunt_resistance)
            term_g = constants.elementary_charge * shunt_resistance * (dark_current * series_resistance * (dark_current + photocurrent) + diode_voltage)
            term_f = (constants.Boltzmann * ideality_factor * temperature) * (series_resistance + shunt_resistance)

           # if ((term_g / term_f) <= 43):
            current = ((term_a / term_b) + (term_c / term_d)*np.real(special.lambertw((term_e / term_f) * np.exp(term_g / term_f))))
           # else:
                #current = ((term_a / term_b) + (term_c / term_d)*special.lambertw((term_e / term_f) * np.exp(43)))

            np.append(currents, current)

        return currents

    return my_curve_function

popt, pcov = optimize.curve_fit(include_constants(temperature, dark_current, rows), diode_voltage, 
diode_current, guess)
optimised_series_resistance, optimised_shunt_resistance, optimised_ideality_factor, 
optimised_photocurrent = popt

0 个答案:

没有答案