leveberg_marquardt algorithm

45 views
Skip to first unread message

Leona

unread,
May 5, 2020, 10:48:13 AM5/5/20
to lmfi...@googlegroups.com
I created leveberg_marquardt algorithm and I would like to know the parameters of the function => a,b,c,d
It is working until the row: 
# do fit, here with leastsq model
minner = Minimizer(fcn2min, params, fcn_args=(x, data))

Here it stops with the sentence: TypeError: 'Parameter' object is not callable
result = minner.minimize()

Please can someone help me with this? :-)

My code:

import numpy as np
import pandas as pd
from lmfit import Minimizer, Parameters, report_fit

# intialise data of lists. 
dt = {'t':[15, 45, 75,105,135,165,195,225,255,285,315,345,375,405], 'my_test':[66,83,87,90,92,91,90,88,85,81,78,76,75,71]} 

# Create DataFrame 
df = pd.DataFrame(dt) 
df["my_kg1"] = df["my_test"]-50
df["my_kg2"] = df["my_test"]-30

#df1 = df[["my_test", "t"]]
df1 = df[["my_kg1", "t"]]
#df1 = df[["my_kg2", "t"]]
  
# Print the output. 
#print(df1)

#creating array
dat = df1.values
#print(dat)

x = dat[:, 1]
data = dat[:, 0] 
print(x)
print("--------")

print(data)
print("--------")


def fcn2min(params, x, data):
    a = params['a']
    b = params['b']
    c = params['c']
    d = params['d']
    e = params['e']
    f = params['f']
    g = params['g']
  
    model = a*e(-d*x)*(f - e((c - x)/b)/g)
    #'float' object is not callable: a*2.718(-d*x)*(1 - 2.718((c - x)/b)/2)
    return model - data

# set parameters incluing bounds
params = Parameters()
params.add('a', value=21.2, min=1.0, max=75.0)
params.add('b', value=17.9, min=0.0, max=60.0)
params.add('c', value=3.4, min=-5.0, max=6.0)
params.add('d', value=0.00, min=0.00, max=0.01)
params.add('e', value=2.718, vary=False)
params.add('f', value=1, vary=False)
params.add('g', value=2, vary=False)

# do fit, here with leastsq model
minner = Minimizer(fcn2min, params, fcn_args=(x, data))

result = minner.minimize()

# calculate final result
final = data + result.residual

# write error report
report_fit(result)

# try to plot results
try:
    import matplotlib.pyplot as plt
    plt.plot(x, data, 'k+')
    plt.plot(x, final, 'r')
    plt.show()
except ImportError:
    pass
<end of examples/doc_parameters_basic.py>

Matt Newville

unread,
May 5, 2020, 12:40:23 PM5/5/20
to lmfit-py
On Tue, May 5, 2020 at 9:48 AM Leona <lenka.kr...@gmail.com> wrote:
I created leveberg_marquardt algorithm and I would like to know the parameters of the function => a,b,c,d
It is working until the row: 
# do fit, here with leastsq model
minner = Minimizer(fcn2min, params, fcn_args=(x, data))

Here it stops with the sentence: TypeError: 'Parameter' object is not callable

It's always good to include and read the error messages.  In fact, it will stop with the two lines of 

      File "<f.py>", line 43, in fcn2min

        model = a*e(-d*x)*(f - e((c - x)/b)/g)
    TypeError: 'Parameter' object is not callable
The Parameter 'e' is not a callable function, but your expression has `e` being a used as a function: `e(thing)`.  

Perhaps you meant
    model = a*e*(-d*x)*(f - e*((c - x)/b)/g)
 
Or perhaps you meant for `e` to be Euler's number and to use exponentiation, and not have `e` ~= 2.718 not be a parameter all, and use
    model = a*np.exp(-d*x)*(f - np.exp((c - x)/b)/g)

--Matt


result = minner.minimize()

--
You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/95d97750-526b-4672-b808-223397089302%40googlegroups.com.

Leona

unread,
May 5, 2020, 12:59:14 PM5/5/20
to lmfit-py
Thanks a lot!!! It is working :-)


Reply all
Reply to author
Forward
0 new messages