1

I'm trying to simulate an exoplanet transit and to determine its orbital characteristics with curve fitting. However, the intersection area between two circles needs to distinguish two cases: if the center of the smallest circle is in the biggest or not. This is a problem for scipy with the function curve_fit, calling an array in my function cacl_aire. The function transit simulates the smallest disc's evolution with time. Here's my code:

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()

I have the following error :

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:

Here is my database:

https://drive.google.com/file/d/1SP12rrHGjjpHfKBQ0l3nVMJDIRCPlkuf/view?usp=sharing

I don't see any trick to solve my problem.

TrodaroX
  • 11
  • 2
  • 1
    Which line is giving the error? – Mathieu Feb 15 '21 at 11:03
  • 1
    We don't have your data. I suggest providing a [minimal, complete, and verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). – Mr. T Feb 15 '21 at 13:30
  • Sorry I have forgotten to precise the line ! I corrected my message and added a link to my data – TrodaroX Feb 16 '21 at 10:46
  • 1
    Does this answer your question? [ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()](https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous) – mkrieger1 Feb 16 '21 at 10:53
  • A link is not sufficient. The question should be self-contained. (only provide the minimal amount of data required to reproduce the problem. Read the link [example] for more info.) – user202729 Feb 16 '21 at 11:07
  • Not really, my problem is that curve_fit apply my function calc_aire on a numpy array. So of course there is an ambigous meaning of the "if" with an array, I would like curve_fit to apply this condition to each element of the array. I tried to change my function to be compatible with the array type but it was not successful. – TrodaroX Feb 16 '21 at 11:09
  • `curve_fit` sends all data at once to the function, so any check via `if` is applied to an entire array. A simple solution usually is to check if the received object is an array e.g. via `if isinstance( , (list tuple, numpy.ndarray) )` and if so, call the function with single elements e.g. via list comprehension `returnvalue = [ myfunction( elem, *args ) for elem in ]`. So the function calls itself with the array decomposed. – mikuszefski Feb 16 '21 at 11:11
  • thanks I gonna try this – TrodaroX Feb 16 '21 at 11:13
  • As suggested by the error message, you can test if all elements of the array `D2` fulfill the condition with `(D2 >= (r + R)**2).all()` – Mr. T Feb 16 '21 at 11:27

0 Answers0