带条件的曲线拟合

2024-06-16 13:57:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图模拟一颗系外行星的凌日,并通过曲线拟合来确定它的轨道特征。但是,两个圆之间的相交区域需要区分两种情况:最小圆的圆心是否在最大圆内。这是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

我看不出有什么办法可以解决我的问题


Tags: 函数importreturnvaluenppltd2vx