python函数拟合编程_通过Scipy&Numpy使用Python将数据拟合到ODE系统

我通过Scipy& amp将我的MATLAB代码翻译成Python时遇到了一些麻烦. NumPy的.我坚持如何找到我的ODE系统的最佳参数值(k0和k1),以适应我的十个观察数据点.我目前对k0和k1有一个初步猜测.在MATLAB中,我可以使用一种叫做“fminsearch”的东西,它是一个接受ODE系统,观察数据点和ODE系统初始值的函数.然后,它将计算一对新的参数k0和k1,它们将适合观察到的数据.我已经包含了我的代码,看看你是否可以帮助我实现某种“fminsearch”来找到适合我数据的最佳参数值k0和k1.我想将任何代码添加到我的lsqtest.py文件中.

我有三个.py文件 – ode.py,lsq.py和lsqtest.py

ode.py:

def f(y,t,k):

return (-k[0]*y[0],k[0]*y[0]-k[1]*y[1],k[1]*y[1])

lsq.py:

import pylab as py

import numpy as np

from scipy import integrate

from scipy import optimize

import ode

def lsq(teta,y0,data):

#INPUT teta,the unknowns k0,k1

# data,observed

# y0 initial values needed by the ODE

#OUTPUT lsq value

t = np.linspace(0,9,10)

y_obs = data #data points

k = [0,0]

k[0] = teta[0]

k[1] = teta[1]

#call the ODE solver to get the states:

r = integrate.odeint(ode.f,args=(k,))

#the ODE system in ode.py

#at each row (time point),y_cal has

#the values of the components [A,B,C]

y_cal = r[:,1] #separate the measured B

#compute the expression to be minimized:

return sum((y_obs-y_cal)**2)

lsqtest.py:

import pylab as py

import numpy as np

from scipy import integrate

from scipy import optimize

import lsq

if __name__ == '__main__':

teta = [0.2,0.3] #guess for parameter values k0 and k1

y0 = [1,0] #initial conditions for system

y = [0.000,0.416,0.489,0.595,0.506,0.493,0.458,0.394,0.335,0.309] #observed data points

data = y

resid = lsq.lsq(teta,data)

print resid

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值