0

Im trying to minimize function(T) by scipy.optimize.minimize().

With 0 < T < 1.

My code is the following:

import scipy
import math


def function(T):
    '''
    U = (1- sqrt(T)^(-2)
    1 - T^3 *  (1 - T - U) ^ 5  * U ^ 10
    '''

    return 1 - T**3 * (1-T - (T + 1 + 2 * math.sqrt(T))/(T-1)**2) ** 5 * ((T + 1 + 2 * math.sqrt(T))/(T-1)**2)**10

# minimization

res = scipy.optimize.minimize(function, 1, bounds= [(0,1)])

w = res.x

When I run the code it shows the error messege:

RuntimeWarning: divide by zero encountered in true_divide
  return 1 - T**x[0] * (1-T - (T + 1 + 2 * math.sqrt(T))/(T-1)**2) ** x[1] * ((T + 1 + 2 * math.sqrt(T))/(T-1)**2)**x[2]
RuntimeWarning: overflow encountered in multiply
  return 1 - T**x[0] * (1-T - (T + 1 + 2 * math.sqrt(T))/(T-1)**2) ** x[1] * ((T + 1 + 2 * math.sqrt(T))/(T-1)**2)**x[2]

What is going on? Thanks.

PS: what does the error messages mean?

bibols
  • 1
  • 2
  • Your denominator T-1 is zero for T=1 (your initial guess), so you are dividing by zero. Just use another initial guess, for instance, T=0.5 should work. Note also that you can't use strict inequality constraints like 0 < T < 1. Instead, you must use something like 0 + Ɛ ≤ T ≤ 1 - Ɛ, where Ɛ > 0 is a sufficiently small number. – joni Jun 29 '22 at 10:50
  • I see. About the initial guess, it seams I'm doing something wrong because: scipy.optimize.minimize(function, INITIAL, bounds= [(0,1)]) always returns the INITIAL value.... – bibols Jun 29 '22 at 16:58

0 Answers0