【问题标题】:RuntimeWarning: divide by zero encountered in true_divide W = 1/sigma**2/s_sq symfitRuntimeWarning:在 true_divide W = 1/sigma**2/s_sq symfit 中遇到除以零
【发布时间】:2023-03-26 12:28:02
【问题描述】:

我尝试过运行此代码,但无论我如何简化它,我总是遇到同样的错误。

/home/runner/.site-packages/symfit/core/fit.py:1046: RuntimeWarning: 在 true_divide W = 1/sigma**2/s_sq[:, np.newaxis]

/home/runner/.site-packages/symfit/core/fit.py:1783:> RuntimeWarning:double_scalars 遇到无效值返回 1 SS_res/SS_tot

我们将不胜感激。

x1, ya = sf.variables('x1, ya')
I1, I2, I3, A, B, C, D = sf.parameters('I1, I2, I3, A, B, C, D')


I1.value = 46.483
I2.value = 5.916
I3.value = 21.90
A.value = -3.828*10**(-5)
B.value = 0
C.value = 0
D.value = 0

# Making the equation
ya = (A*x1**3 + B*x1**2 + C*x1 + D + (q_subm*x1**4)/(24*EI))  #q_sub and EI are constants

#model = Model({y: Piecewise((ya, x1 <= I1), (yb, x2 <= I2), (yc, x3 <= I3))})
model = Model({ya})

# As a constraint, we want cable to be at 0 at start and and at "-p" height at x=60
# also no angle in pipe ate those points
constraints = [
    Eq(ya.subs({x1: 0}), 0),
    Eq(ya.subs({x1: 60}), -p),
    Eq(ya.diff(x1).subs({x1: 0}), 0),
    Eq(ya.diff(x1).subs({x1: 60}), 0)
]


x1data = np.linspace(0, 60., 60)
y1data = model(x1=x1data, A = -3.828*10**(-5), B = 0, C = 0, D = 0)
np.random.seed(2)
y1data = np.random.normal(y1data, 0.005) 

plt.plot([60], [0.4], 'ro')
plt.scatter(x1data, y1data)
plt.savefig('plot.png')

print ('Done plotting fig')

#fit = Fit(model, x=xdata, y=ydata, constraints=constraints)
fit = Fit(model, x1=x1data, constraints=constraints)

print ('Done fitting model')
fit_result = fit.execute()
#print(fit_result)

【问题讨论】:

    标签: python python-3.x curve-fitting symfit


    【解决方案1】:

    我有几个关于你的代码的 cmets,也许其中一个可以解决问题。

    • model = Model({ya}) 错误地尝试从集合中制作模型,因为 {} 制作了集合。尝试改用model = Model({y: ya})model = Model(ya)。 (我会推荐第一个)
    • 注释行fit = Fit(model, x=x1data , y=y1data, constraints=constraints) 基本正确,请注意我更改了数据数组的名称以包含1。由于您没有提供 ydata,因此上述示例中的行无法正常工作。
    • q_submEI 中的type 是什么?只要它们是标准的 Python 数字类型就可以了,但如果它们是奇异的,这可能会导致问题。
    • 请记住,您在问题中列出的“错误”是Warnings,只要结果正确,拟合过程中的一些警告就没有错。

    我希望这能解决问题,如果不告诉我。

    x1, y = sf.variables('x1, y')
    I1, I2, I3, A, B, C, D = sf.parameters('I1, I2, I3, A, B, C, D')
    
    
    I1.value = 46.483
    I2.value = 5.916
    I3.value = 21.90
    A.value = -3.828*10**(-5)
    B.value = 0
    C.value = 0
    D.value = 0
    
    # Making the equation
    ya = (A*x1**3 + B*x1**2 + C*x1 + D + (q_subm*x1**4)/(24*EI))  #q_sub and EI are constants
    model = Model({y: ya})
    
    # As a constraint, we want cable to be at 0 at start and and at "-p" height at x=60
    # also no angle in pipe ate those points
    constraints = [
        Eq(ya.subs({x1: 0}), 0),
        Eq(ya.subs({x1: 60}), -p),
        Eq(ya.diff(x1).subs({x1: 0}), 0),
        Eq(ya.diff(x1).subs({x1: 60}), 0)
    ]
    
    
    x1data = np.linspace(0, 60., 60)
    y1data = model(x1=x1data, A=-3.828*10**(-5), B=0, C=0, D=0)
    np.random.seed(2)
    y1data = np.random.normal(y1data, 0.005) 
    
    plt.plot([60], [0.4], 'ro')
    plt.scatter(x1data, y1data)
    plt.savefig('plot.png')
    
    print ('Done plotting fig')
    
    fit = Fit(model, x=x1data, y=y1data, constraints=constraints)
    fit_result = fit.execute()
    
    print ('Done fitting model')
    print(fit_result)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 2014-03-03
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多