Minimization of a function in 3D
Source code name: "func_jmunuit_minimum2d.py"
Programming language: Python
Topic: Data fitting/minuit
DMelt Version 1.4. Last modified: 02/22/2017. License: Free
https://datamelt.org/code/cache/func_jmunuit_minimum2d_1350.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


from java.lang import Math
from jhplot import *

class MyFunc(FNon):  # define a function a*(x-2)^2 + (y+1)^2 
     def value(self, x):
              return (x[0]-2)*(x[0]-2)*self.p[0]+(x[1]+1)*(x[1]+1)

c1 = HPlot3D()                   # plot data and the fit function
c1.visible()
c1.setAutoRange()
pl = MyFunc("2D function",2,1) # 2 variables, 1 parameter     
print pl.numberOfParameters()
print pl.dimension()
print pl.parameterNames().tolist()
pl.setParameter("par0",2)
print pl.parameters()
c1.draw( F2D(pl,-10,10,-10,10) ) # create plottable function and show it 

from hep.aida.ref.optimizer.jminuit import *
op=JMinuitOptimizerFactory().create()
op.setFunction(pl)                                 # set function 
op.variableSettings("x0").setValue(10)  # set initial value
op.variableSettings("x1").setValue(-10) # set initial value
op.optimize()                                         # perform optimization 
res =op.result()
print "Minimization results=",res.parameters(), " status=",res.optimizationStatus()