Adding Minimization Arguments
Allowing Arguments in the Minimization¶
It would not be quite so convient if you had to have every function only take one variable as an input. Thankfully, we can very easily pass any additional arguments in the optimization. First, let’s see how the 1D parabola function could be modified with a parameter a.
In [49]:
#Modified function
def parabola_modified(x, a):
y = (x-5*a) ** 2 + 100
return y
X = np.linspace(-15,15,31)
Y = [parabola_modified(x, 2) for x in X]
plt.plot(X, Y)
plt.ylim([0,800])
plt.show()
To pass in arguments, simply give the minimzation argument args a tuple with all values to be passed in.
In [50]:
#Pass in the argument 2 for a
x0 = 0
print(minimize(parabola_modified, x0, args=(2)))
fun: 100.00000000000023
hess_inv: array([[0.49999992]])
jac: array([-9.53674316e-07])
message: 'Optimization terminated successfully.'
nfev: 15
nit: 4
njev: 5
status: 0
success: True
x: array([9.99999953])