我目前使用Measurements.jl进行错误传播,使用LsqFit.jl将函数拟合到数据。有没有一种简单的方法来使一个函数适合有错误的数据?如果使用其他包可以让事情变得更容易,那么使用其他包也没有问题。
提前感谢您的帮助。
发布于 2021-07-28 02:10:44
虽然原则上应该可以使这些包协同工作,但LsqFit.jl的实现似乎不能很好地处理Measurement
类型。然而,如果一个人直接写一个简单的最小二乘线性回归
# Generate test data, with noise
x = 1:10
y = 2x .+ 3
using Measurements
x_observed = (x .+ randn.()) .± 1
y_observed = (y .+ randn.()) .± 1
# Simple least-squares linear regression
# for an equation of the form y = a + bx
# using `\` for matrix division
linreg(x, y) = hcat(fill!(similar(x), 1), x) \ y
(a, b) = linreg(x_observed, y_observed)
然后
julia> (a, b) = linreg(x_observed, y_observed)
2-element Vector{Measurement{Float64}}:
3.9 ± 1.4
1.84 ± 0.23
这应该能够与x
不确定性和/或y
不确定性一起工作。
如果需要非线性最小二乘拟合,还可以将上面的方法扩展到非线性最小二乘--尽管对于后者,只需找到LsqFit.jl中不兼容的地方并进行PR可能会更容易。
https://stackoverflow.com/questions/68543537
复制