Unable to use a scipy minimize function within a for loop

Viewed 12

I am trying to use the scipy minimize function for different values of args. When I try to do it individually, it works fine but when I use a for loop to minimize the function with different values of arguments, it throws an error.

This is the code I used

import sympy as sym
import numpy as np
from scipy.optimize import minimize


a =0.295533
b = 4.84772e-06
c = 1.47333e-15
def fit_1(x,i):
    return np.abs((1-(a*(1-np.exp(-b*x))+c)))**(1/(x+i))
bnds = ((0,np.inf),)
def sweet(i):
    spot = minimize(fit_1,args =i, x0 =5000, method = 'Nelder-Mead', bounds=bnds)
    return spot.x

c = [10000,100000,1000000]
n = []
for k in c:
    s = sweet(k)
    s.append(n)
TypeError: only size-1 arrays can be converted to Python scalars

ValueError: setting an array element with a sequence.
0 Answers
Related