以前写的,不过是三维版的,而且是分段函数,直接从定义出发,用grad提取系数,做成矩阵乘法,你扩展下怎么把分段函数搞成一个就可以了
segmentFit[path_] := Module[
{basis, A, len, i, path1, fun, coe, P},
g[k_, n_] := Subscript[P, k]*1/n! \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 0\), \(n - k\)]\(
\*SuperscriptBox[\((\(-1\))\), \(i\)]*Binomial[n + 1, i]*
\*SuperscriptBox[\((t + n - k - i)\), \(n\)]\)\);(*B样条线定义*)
len = 2;(*多项式最高次*)
basis = Table[t^i, {i, 0, len}];
coe = CoefficientList[Plus
@@ Table[g[i, len], {i, 0, len}],
t];(*B样条线:系数*)
A = Grad[coe, Table[Subscript[P, i], {i, 0, len}]];
path1 = Prepend[path, (path[[1]] - path[[2]]) + path[[1]]];
path1 = Append[path1, Last[path] + (Last[path] - path[[-2]])];
fun = Table[
basis . A . Take[path1, {i, i + len}] // N, {i, 1,
Length[path1] - len}];
Return[fun];
]
Compile[{{path}}, segmentFit]
path = {{30, 0, 0}, {30 + 14/Sqrt[249], 20/Sqrt[249], 20/Sqrt[
249]}, {32.20470703921131`, 1.3906289084343317`,
2.7671279831555884`}, {33.09192098994558`, 2.6580774094832877`,
4.034576484204544`}, {33.11954143398565`, 2.9390588704641174`,
6.014547829869958`}, {32.50729927398154`, 4.456873016879327`,
7.164069362706715`}, {33.39451322471581`, 5.724321517928283`,
8.431517863755671`}, {34.56600017798742`, 5.876252184970187`,
10.045375165603202`}, {35.56858636708187`, 7.606803929329527`,
10.048780093195823`}, {36.45580031781614`, 8.874252430378483`,
11.316228594244778`}};
v = segmentFit[path];
(*Show[Graphics3D[{{Blue,PointSize[0.01],Point[path]}},ImageSize\
\[Rule]Large],
ParametricPlot[v,{t,0,1}]]*)
ParametricPlot3D[v, {t, 0, 1}]