def record_answers(func, seq):
def f_(*args, **kargs):
result = func(*args, **kargs)
seq.append(result)
return result
return f_
def fixed_point_or_closest(func, *args, **kargs):
results = []
try:
return optimize.fixed_point(record_answers(func, results), *args, **kargs)
except RuntimeError:
return results[-1]
from scipy import optimize
def func(x, c1, c2):
return np.sqrt(c1/(x+c2))
c1 = np.array([10,12.])
c2 = np.array([3, 5.])
result = fixed_point_or_closest(func, [1.2, 1.3], args=(c1,c2))