简体   繁体   English

使用 lmfit 进行曲线拟合,通过参数总和限制结果

[英]Curve fit using lmfit, limiting result by sum of parameters

I have created a curve fitting algorithm that works, up to a point.我已经创建了一个曲线拟合算法,在某种程度上是可行的。
I have six parameters: a, b, c, d, e, f.我有六个参数:a、b、c、d、e、f。
Each parameter can be between 0 and 100, but with the constraint (a+b+c+d+e+f) <= 100每个参数可以介于 0 和 100 之间,但具有约束条件 (a+b+c+d+e+f) <= 100
Is there anyway to do this?有没有办法做到这一点?

params.add('a', value=1, min=0, max = 100, vary=True)
params.add('b', value=1, min=0, max = 100, vary=True)
params.add('c', value=1, min=0, max = 100, vary=True)
params.add('d', value=1, min=0, max = 100, vary=True)
params.add('e', value=1, min=0, max = 100, vary=True)
params.add('f', value=1, min=0, max = 100, vary=True)

I did manage to find a, probably not very elegant, solution.我确实设法找到了一个可能不是很优雅的解决方案。
In the function I'm trying to solve I check if the the sum of a+b...+f is greater than 100. If it is I reset the coefficients with random numbers, forcing the algorithm to find another solution.在我试图解决的 function 中,我检查 a+b...+f 的总和是否大于 100。如果是,我用随机数重置系数,迫使算法找到另一个解决方案。

def myfunc(a, b, c, d, e, f):
    sum = a+b+c+d+e+f
    if sum>100:
        a = np.random.randint(low=0, high=10)
        b = np.random.randint(low=0, high=10)
        c = np.random.randint(low=0, high=10)
        d = np.random.randint(low=0, high=10)
        e = np.random.randint(low=0, high=10)
        f = np.random.randint(low=0, high=10)
Rest of function

Consider that (a+b+c+d+e+f) <= 100 can be written as考虑(a+b+c+d+e+f) <= 100可以写成

f = 100 - (a+b+c+d+e) - epsilon

with epsilon >= 0 .epsilon >= 0 Now, use that with epsilon as the variable and f derived from that:现在,将其与 epsilon 一起用作变量,并从中派生 f:

params.add('a', value=1, min=0, max = 100, vary=True)
params.add('b', value=1, min=0, max = 100, vary=True)
params.add('c', value=1, min=0, max = 100, vary=True)
params.add('d', value=1, min=0, max = 100, vary=True)
params.add('e', value=1, min=0, max = 100, vary=True)
params.add('epsilon', value=1, min=0, vary=True)
params.add('f', expr='100-a-b-c-d-e-epsilon')

You still have 6 variables, but now f is not independent of epsilon .您仍然有 6 个变量,但现在f不独立于epsilon

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM