Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have various data points which want to fit all of them with different differential equations. For example :

t = [0.01, 6, 12, 18, 24, 30, 36, 42, 48, 57, 75]
Ca_data = [64.8, 68.4, 48.0, 36.8, 33.7, 33.4, 29, 33.5, 32.0, 32.2, 0.01]

This is one set of data which I have and I want to fit it with following function:

def fitfunc(t, a, b):
    def myode(Ca, t):
        return a *(Ca ** (2/3)) - b * Ca

    Ca0 = Ca_data[0]
    Casol = odeint(myode, Ca0, t)
    return Casol[:,0]

The following code is the script which I wrote to find the initial values using genetic algorithm and fit the function to data points which is seems that does not work.

import numpy as np
from scipy.optimize import curve_fit
from scipy.integrate import odeint

def generate_Initial_Parameters_genetic(t, d):
    # min and max used for bounds
    maxX = max(t)
    maxY = max(d)
    maxXY = max(maxX, maxY)

    parameterBounds = []
    parameterBounds.append([-maxXY, maxXY])
    parameterBounds.append([-maxXY, maxXY])

    result = differential_evolution(sumOfSquaredError, parameterBounds,         
             seed=3,updating='deferred',workers=2)

    return result.x

def sumOfSquaredError(parameterTuple):
    warnings.filterwarnings("ignore") 
    val = fitfunc(tspan, *parameterTuple)
    return np.sum((Ca_data - val) ** 2.0)

geneticParameters = generate_Initial_Parameters_genetic(tspan, Ca_data)
k_fit, kcov = curve_fit(fitfunc, tspan, Ca_data, p0 = geneticParameters, maxfev = 20000)
                
k_fit, kcov = curve_fit(fitfunc, tspan, Ca_data, p0 = [0,0])

tfit = np.linspace(0,75)
fit = fitfunc(tfit, k_fit[0], k_fit[1])

plt.plot(tspan, Ca_data, 'ro', label='data')
plt.plot(tfit, fit, 'b-', label='fit')
plt.legend(loc='best')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
314 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...