【发布时间】:2018-10-10 16:36:01
【问题描述】:
我正在用 Python 模拟一个球在流体中落下,并使用阻尼系数(a 和b)和流体密度将模型函数拟合到一组数据点,但拟合值为流体密度返回负值,我不知道代码有什么问题。我的代码如下:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from scipy.optimize import curve_fit
##%%Parameters and constants
m = 0.1 #mass of object in kg
g = 9.81 #acceleration due to gravity in m/s**2
rho = 700 #density of object in kg/m**3
v0 = 0 #velocity at t=0
y0 = 0 #position at t=0
V = m / rho #volume in cubic meters
r = ((3/4)*(V/np.pi))**(1/3) #radius of the sphere
asample = 0.0001 #sample value for a
bsample = 0.0001 #sample value for b
#%%Defining integrating function
##fuction => y'' = g*(1-(rhof/rho))-((a/m)y'+(b/m)y'**2)
## y' = v
## v' = g*(1-rhof/rho)-((a/m)v+(b/m)v**2)
def sinkingball(Y, time, a, b, rhof):
return [Y[1],(1/m)*(V*g*(rho-rhof)-a*Y[1]-b*(Y[1]**2))]
def balldepth(time, a, b, rhof):
solutions = odeint(sinkingball, [y0,v0], time, args=(a, b, rhof))
return solutions[:,0]
time = np.linspace(0,15,151)
# imported some experimental values and named the array data
a, b, rhof = curve_fit(balldepth, time, data, p0=(asample, bsample, 100))[0]
print(a,b,rhof)
【问题讨论】:
-
请阅读How to create a Minimal, Complete, and Verifiable example。可验证的示例包括重现问题的示例输入。有没有原因,为什么
sinkingball中没有使用time? -
除了数据之外,您对发布的示例源代码的问题具有我分析所需的一切。请发布数据链接。
标签: python scipy curve-fitting