我正在编写代码来计算三次样条插值,使用介于-1和2之间的n + 1个等间距插值点(取n = 6和n = 10),绘制图形与f(x)并找出最大误差。我没有得到n = 6的代码错误,虽然我不确定最大错误是否正确。当我使用相同的代码更改为n = 10时,它似乎不起作用。帮助将不胜感激。
这是我的代码: N = 6。
restart;
f := proc (x) options operator, arrow; (1-x^2)*sin(2*Pi*x);
Xlist := [-1, -.5, 0, .5, 1, 1.5, 2];
k := nops(Xlist);
for i to k-1 do
S[i] := a[i]+b[i]*(x-Xlist[i])+c[i]*(x-Xlist[i])^2+d[i]*(x-Xlist[i])^3;
dS[i] := diff(S[i], x);
ddS[i] := diff(dS[i], x;
end do;
for i from 1 to k-1 do
EQ[i] := subs(x = Xlist[i], S[i]) = f(Xlist[i]);
end do;
for i from 1 to k-1 do
EQ[k-1+i] := subs(x = Xlist[i+1], S[i]) = f(Xlist[i+1]);
end do;
for i from 1 to k-2 do
EQ[2*k-2+i] := subs(x = Xlist[i+1], dS[i]) = subs(x = Xlist[i+1], dS[i+1]);
end do;
for i from 1 to k-2 do
EQ[3*k-4+i] := subs(x = Xlist[i+1], ddS[i]) = subs(x = Xlist[i+1], ddS[i+1]);
end do;
Clamp[1] := subs(x = Xlist[1], dS[1]) = (D(f))(Xlist[1]);
Clamp[2] := subs(x = Xlist[k], dS[k-1]) = (D(f))(Xlist[k]);
ClampedEqs := {Clamp[1], Clamp[2], seq(EQ[i], i = 1 .. 4*k-6)};
ClampedCoeffs := op(1, solve(ClampedEqs, [seq(a[i], i = 1 .. k-1), seq(b[i], i = 1 .. k-1), seq(c[i], i = 1 .. k-1), seq(d[i], i = 1 .. k-1)]));
assign(ClampedCoeffs)
ClampedSpline := proc (x) options operator, arrow; piecewise(x < Xlist[2], S[1], S[2]);
plot([f(x), ClampedSpline(x)], x = -1 .. 2);
print(Max*Error*is = numapprox[infnorm](f(x)-ClampedSpline(x), x = -1 .. 2));
给出最大误差5.146099863
然而,当我改为n = 10时,'ClampedEqs @ op我得到'错误,不正确的操作或下标选择器“错误,我以前没有。