英文:
How to perform the B-spline interpolation in Julia?
问题
我想要插值以下的2x1矩阵(为了问题更清晰的示例):
x=collect(300:50:900)
y=rand(13)
查询点'xq'如下:
xq=collect(290:4:910)
在这种情况下,我如何获得y的查询点'yq'?
我尝试了一些插值方法(比如插值和Diercks..),但都没有成功(或者我做错了..)。我非常困惑。请分享您的专业知识。
英文:
I want to interpolate the following 2x1 matrix (example for clarity of question):
x=collect(300:50:900)
y=rand(13)
the query points 'xq' are:
xq=collect(290:4:910)
in this case, how can I get the query point of y 'yq'?
I tried some interpolation methods (such as interpolations and Diercks..), but nothing worked (or I did wrong way..). I'm so confused. please share your know-how.
答案1
得分: 3
你尝试过什么?什么没有起作用?
在查看插值之前,请注意通常不必使用collect
- 它会创建不必要的数组,从而分配内存,抵消了使用轻量级的StepRange
类型的好处,这个类型可以通过类似300:50:900
的表达式创建。
以下是在文档的第一个示例之后使用BSplineKit
的示例:
julia> using BSplineKit
julia> x = 300:50:900; y = rand(length(x));
julia> itp = interpolate(x, y, BSplineOrder(4))
SplineInterpolation 包含 13 个元素的 Spline{Float64}:
basis: 13 个顺序为 4 的 BSplineBasis,域为 [300.0, 900.0]
order: 4
knots: [300.0, 300.0, 300.0, 300.0, 400.0, 450.0, 500.0, 550.0, 600.0, 650.0, 700.0, 750.0, 800.0, 900.0, 900.0, 900.0, 900.0]
coefficients: [0.164315, 0.399353, 0.866037, 0.325909, 1.16957, 0.990489, 0.74577, 0.882066, 0.698501, 0.684713, 1.27336, -0.49067, 0.985016]
interpolation points: 300:50:900
julia> itp.(290:4:910)
156 个元素的 Float64 向量:
0.0
0.0
0.0
0.1785050188621375
0.8996276319427731
0.0
0.0
0.0
我从文档中唯一更改的是将itp
函数广播到查询点范围。以下是结果的样子:
英文:
What have you tried? What did not work?
Before looking at the interpolation, note that you usually don't have to use collect
- it creates unnecessary arrays and thereby allocations and negates the benefits of working with the lightweight StepRange
type created by an expression like 300:50:900
.
Here's an example with BSplineKit
following the first example in the docs:
julia> using BSplineKit
julia> x = 300:50:900; y = rand(length(x));
julia> itp = interpolate(x, y, BSplineOrder(4))
SplineInterpolation containing the 13-element Spline{Float64}:
basis: 13-element BSplineBasis of order 4, domain [300.0, 900.0]
order: 4
knots: [300.0, 300.0, 300.0, 300.0, 400.0, 450.0, 500.0, 550.0, 600.0, 650.0, 700.0, 750.0, 800.0, 900.0, 900.0, 900.0, 900.0]
coefficients: [0.164315, 0.399353, 0.866037, 0.325909, 1.16957, 0.990489, 0.74577, 0.882066, 0.698501, 0.684713, 1.27336, -0.49067, 0.985016]
interpolation points: 300:50:900
julia> itp.(290:4:910)
156-element Vector{Float64}:
0.0
0.0
0.0
0.1785050188621375
0.8996276319427731
0.0
0.0
0.0
The only thing I've changed from the docs is to broadcast the itp
function over the range of query points.
Here's what the result looks like:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论