I want to solve two simultaneous equations using the scipy.optimize.minimize
function in Python, specifically with the dog-leg trust-region algorithm. This requires me to specify the Jacobian of the problem by using scipy.optimize.approx_fprime
, as suggested in one solution to my other post.
My MWE is:
import numpy as np
from scipy.integrate import quad
from scipy.optimize import minimize,approx_fprime
def myfunc(guess,a,b,c,d):
# initial guesses
x0 = guess[0]
x1 = guess[1]
# functions
z0 = lambda x: c*np.sqrt(a**3*x0)*np.sin(x)/x0**b
z1 = lambda x: np.cos(b*x0*x)/x1**a
# numerical integration with 'quad'
z0int = quad(z0,-200,200,epsabs=1e-8,epsrel=1e-6)[0] - 3.2*d
z1int = quad(z1,-200,200,epsabs=1e-8,epsrel=1e-6)[0] + c
return (z0int,z1int)
# constants
a = 0.2
b = 1.1
c = 2.5
d = 0.9
guess = np.array([0.3,0.02]) # initial guesses
myJac = approx_fprime(guess,myfunc,1e-9,a,b,c,d) # Jacobian
# minimisation, want to find x0 such that z0int=0 and z1int=0
xopt = minimize(myfunc,guess,args=(a,b,c,d),method='dogleg',jac=myJac)
print(xopt)
However I get an error TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'
. I'm not really familiar with the Python optimization functions so could you please explain what is wrong and how to correct the code?