开发者社区 问答 正文

使用lmfit拟合两个变量

如何在python中使用lmfit将函数与两个输入变量拟合?函数f(x)= a * x-b)* 2`,a和b是变量,它们可以是随机数。

问题来源:stackoverflow

展开
收起
is大龙 2020-03-24 00:01:45 1099 分享 版权
1 条回答
写回答
取消 提交回答
  • 你可以试试这个吗?

    import matplotlib.pyplot as plt
    import numpy as np
    from lmfit.models import ExpressionModel
    
    # synthetic data
    x = np.linspace(-10, 10, 100)
    a,b = 0.9,1.9
    y = a\*x-b)\*2
    
    # fit y using lmfit; with "guesses": a=1, b=2
    gmod = ExpressionModel("a\*x-b)\*2")
    result = gmod.fit(y, x=x, a=1, b=2)
    
    print(result.fit_report())
    
    plt.plot(x, y, 'bo')
    plt.plot(x, result.init_fit, 'k--', label='initial fit')
    plt.plot(x, result.best_fit, 'r-', label='best fit')
    plt.legend(loc='best')
    plt.show()
    

    输出:

    回答来源:stackoverflow

    2020-03-24 00:01:51
    赞同 展开评论
问答分类:
问答地址: