'How to minimize a difference between two functions, one of which takes several parameters
I am currently doing a Python script. My goal is to display a curve (known and unchangeable) and to find a curve with 4 arguments that looks like it.
(I want to find the concentrations of a material2 that allows me to have a macroscopic cross-section curve as close as possible to a macroscopic reference cross-section)
To do this, I created a "difference" function that returns the difference between these two lists. This function takes 4 arguments as input. I know that Python allows me to minimize this difference and to find the parameters that best satisfy this minimization (scipy.optimize). I try to use scipy.optimize.minimize with the 'COBYLA' method, but I see that the parameters are incremented indefinitely...
Would you have an idea? Thanks in advance
Code
import numpy
import matplotlib.pyplot
import scipy.optimize
from scipy.optimize import Bounds
def diff(conc):
c1, c2, c3, c4 = conc
print(c1, c2, c3, c4)
material2 = material([args],[c1, c2, c3, c4]) # list of data
t = []
for i in range(energy):
t.append(material1.cross_section_macro[i] - material2.cross_section_macro[i])
return(t)
###############################################################################
#
# minimization
# """"""""""""
#
material1 = material([args],[4.63484E-03, 9.87732E-03, 2.88900E-02, 1.44415E-02])
initial_guess = [1.15E-03, 0, 2.9E-02, 2.9E-02]
bounds = Bounds([1E-03, 0, 2.5E-03, 2.5E-03], [1.5E-03, 1.5E-03, 3.5E-03, 3.5E-03])
result = scipy.optimize.minimize(diff, initial_guess, args=(), method='COBYLA', bounds=bounds, tol=0.01, options={'rhobeg': 0.00001, 'maxiter': 10000, 'disp': True, 'catol': 0.0002})
fitted_params = result.x
if result.success:
fitted_params = result.x
print(fitted_params)
else:
raise ValueError(result.message)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
