# from scipy optimize import minimize_scalar def 350 f0 110 return sum f

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49``` ```from scipy.optimize import minimize_scalar def f(x, a = 350, b = 2, f0 = 110): return sum([a*(x[i]**2 - x[i+1])**2 + b*(x[i] - 1)**2 for i in range(0, len(x)-1)]) + f0 def next_step(x, step, index): x[index] += step return x def next_value(h): return f(next_step(x, step, index)) def search(f, x0, e): x = x0 while True: for i in range(len(x)): x_old = x x = next_step(x, minimize_scalar(next_value).x, i) if sum(x - x_old) * 1.0/len(x) < e: break return x def pattern_search(f, x0, e, l): while True: x1, x2, x3, x4 = x0, exploring_search(f, x0), x1 + l*(x2-x1), exploring_search(f, x3) if np.linalg.norm((x2 - x4))/len(x0) < e: break else: x0 = x2 return x2, f(x2) def gradient(x, a = 350, b = 2): return np.array([2*a*x[0], a + b*x[1]*2 - 2*b]) def gradient_descent(x, nm, eps, max_step): def reverce(a): return f(x - a*gradient(x)) result = minimize_scalar(reverce(a), bounds = (-max_step, max_step), method = nm) new_x = x - result.x*gradient(x) if f(x) - f(new_x) < eps: return (f(x), x) return gradient_descent(new_x, nm) ```