如何解决有条件的拟合曲线
我正在尝试模拟系外行星凌日并通过曲线拟合确定其轨道特征。但是,两个圆之间的相交区域需要区分两种情况:最小圆的中心是否在最大。对于函数curve_fit 的scipy 来说,这是一个问题,在我的函数cacl_aire
中调用一个数组。函数transit 模拟最小圆盘随时间的演变。
这是我的代码:
import numpy as np
from matplotlib import pyplot as plt
from scipy.optimize import curve_fit
import xlrd
dt = 0.1
Vx = 0.08
Vy = 0
X0 = -5
Y0 = 0
R = 2
r = 0.7
X = X0
Y = Y0
doc = xlrd.open_workbook("transit data.xlsx")
feuille_1 = doc.sheet_by_index(0)
mag = [feuille_1.cell_value(rowx=k,colx=4) for k in range(115)]
T = [feuille_1.cell_value(rowx=k,colx=3) for k in range(115)]
def calc_aire(r,x,y):
D2 = x * x + y * y
if D2 >= (r + R)**2:
return 0
d = (r**2 - R**2 + D2) / (2 * (D2**0.5))
d2 = D2**0.5 - d
if abs(d) >= r:
return min([r * r * np.pi,R * R * np.pi])
H = (r * r - d * d)**0.5
As = np.arccos(d / r) * r * r - d * H
As2 = R * R * np.arccos(d2 / R) - d2 * H
return As + As2
def transit(t,r,X0,Y0,Vx,Vy):
return -calc_aire(r,X0 + Vx * t,Y0 + Vy * t)
best_vals = curve_fit(transit,T,mag)[0]
print('best_vals: {}'.format(best_vals))
plt.figure()
plt.plot(T,mag)
plt.draw()
我有以下错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() with the line 28 :
if D2 >= (r + R)**2:
这是我的数据库:
https://drive.google.com/file/d/1SP12rrHGjjpHfKBQ0l3nVMJDIRCPlkuf/view?usp=sharing
我看不出有什么技巧可以解决我的问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。