五次B样条曲线
MATLAB 的实现
B样条曲线有节点向量(knot vector)和控制顶点(control points)组成,在给出待插值点,先根据点的特征确定节点向量,再根据插值点以及边界条件列举等式方程从而确定控制顶点。MATLAB 中B样条插值实现的函数是spapi()
spline = spapi(knots,x,y) returns the spline f (if any) of order
k = length(knots) - length(x)
with knot sequence knots for which
f(x(j)) = y(:,j), all j.
If some of the entries of x are the same, then:
D m ( j ) f ( x ( j ) ) = y ( : , j ) D^{m(j)} f(x(j))=y(:, j) Dm(j)f(x(j))=y(:,j)
注意:此处 x ( j ) x(j) x(j)有m重,目的是为了添加约束,同理,我们可以添加边界条件。
MATLAB曲线官方实例:
spapi([0 0 0 0 1 2 2 2 2],[0 1 1 1 2],[2 0 1 2 -1])
其手动设置了节点向量,根据节点向量的节点的重复度判断是三次B样条插值,节点值为0,1,2,且在节点值1处设置其速度和加速度约束条件(注:三次B样条插值需要补充两个等式方程,否则方程欠定,解不唯一)。约束条件为:
f ( 0 + ) = 2 , f ( 1 ) = 0 , Df ( 1 ) = 1 , D 2 f ( 1 ) = 2 , f ( 2 − ) = − 1 f(0+)=2, f(1)=0, \operatorname{Df}(1)=1, D^{2} f(1)=2, f(2-)=-1 f(0+)=2,f(1)=0,Df(1)=1,D2f(1)=2,f(2−)=−1
当然可以使用
spapi(k,x,y)
令aptknt function自动确定节点向量,其中k为B样条的order(注:order = degree + 1).
五次B样条插值实例
采用边界速度和加速度均为零边界条件,即多添加了四个等式约束。待插值点为:
P = [90 0 50; 80 25 60;60 45 75;-30 80 80; -60 120 100;40 50 120; 70 20 130;90 -30 150]';
节点向量采用:
uniqueknot = [0 0 0 0 0 0 1 2 3 4 5 6 7 7 7 7 7 7]; % knot vector
全部代码为:
clc,clear
P = [90 0 50; 80 25 60;60 45 75;-30 80 80; -60 120 100;40 50 120; 70 20 130;90 -30 150]';
uniqueknot = [0 0 0 0 0 0 1 2 3 4 5 6 7 7 7 7 7 7]; % knot vector
% interpolation points and boundary conditions
x = [0 0 0 1 2 3 4 5 6 7 7 7]; % Data sites
y = [P(:,1) zeros(3,1) zeros(3,1) P(:,2:end) zeros(3,1) zeros(3,1)]; % Data values to fit
f = spapi(uniqueknot, x, y); % B-spline curve
df = fnder(f,1); ddf = fnder(f,2); % derivatives
v0 = fnval(df,0)
a0 = fnval(ddf,0)