Erroneous attempt to maximise the function of scipy during entry through Input



  • Code:

    import numpy
    import math
    from scipy.optimize import fmin_slsqp
    

    def f(x, sign=-1):
    return eval(str(sign) + '* (' + input() + ')')

    res = fmin_slsqp(f, [5, 5], iter = 10000)
    print(res)

    The performance reigns a mistake. failed in converting 8th argument g of _slsqp.slsqp to C/Fortran arrayExample of the function: 20x[0]**0.3x[1]**0.4 - 2x[0] - 3x[1]If the same function is indicated in the code, not through the entry and evalIt works.



  • Maybe you wanted something like that.

    import numpy
    import math
    from scipy.optimize import fmin_slsqp
    

    while True:
    s = input("Please input equation:")
    if s == 'stop':
    break

    f = lambda x, sign=-1: eval(str(sign) + '* (' + s + ')')
    
    res = fmin_slsqp(f, [5, 5], iter = 10000)
    print(res)
    

    Your equation will be:
    введите сюда описание изображения




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2