pythonnumpyscipyscipy-optimize-minimize

Using scipy optimize minimize does not minimize function


im new into Python and i try to figure out how everythings work. I have a little problem with the minimize function of the scipy.optimize package. I try to minimize a given function with some start values but python gives me very high parameter values. This ist my simple code:

import numpy as np
from scipy.optimize import minimize
global array
y_wert = np.array([1,2,3,4,5,6,7,8])
global x_wert
x_wert = np.array([1,2,3,4,5,6,7,8])
def Test(x):
    Summe = 0
    for i in range(0,len(y_wert)):
        Summe = Summe + (y_wert[i] - (x[0]*x_wert[i]+x[1]))
    return(Summe)
x_0 = [1,0]
xopt = minimize(Test,x_0, method='nelder-mead',options={'xatol': 1e-8, 'disp': True})
print(xopt)

If i run this script the best given parameters are:

[1.02325529e+44, 9.52347084e+40]

which really doesnt solve this problem. Ive also try some slightly different startvalues but that doesnt solve my problems. Can anyone give me a clue as to where my mistake lies? Thanks a lot for your help!


Solution

  • Your test function is effectively a straight line with negative gradient so there is no minimum, it's an infinitely decreasing function, that explains your large results, try something like x squared instead