三次样条MATLAB错误结果

Cubic Spline MATLAB wrong result

我制作了 Clamped Cubic Spline 代码。 但是当我把

f_ = CubicSpline([0,1,2,3],[exp(0),exp(1),exp(2),exp(3)],exp(0),exp(3));

并通过 sym2poly(f_(1)) 获得答案 结果和我的讲义完全不同。实际上,我的三次样条结果甚至与边界素数不匹配...... 请我不明白我的代码中有什么问题。 这就是我用于算法的内容。

function [f_] = CubicSpline(x0,f0,FPO,FPN)

syms x;

n = length(x0);
h = zeros(n,1);
alpha = zeros(n,1);
l = zeros(n,1);
u = zeros(n,1);
z = zeros(n,1);

a = zeros(n,1);
b = zeros(n,1);
c = zeros(n,1);
d = zeros(n,1);

for iter = 1:n-1
    h(iter) = x0(iter+1)-x0(iter);
end

alpha(1) = 3*(f0(2)-f0(1))/h(1)-3*FPO;
alpha(n) = 3*FPN-3*(f0(n)-f0(n-1))/h(n-1);

for iter = 1:n
    a(iter) = f0(iter);
end

for iter = 2:n-1
    alpha(iter) = 3/h(iter)*(f0(iter+1)-f0(iter))-3/h(iter-1)*(f0(iter)-f0(iter-1));
end

l(1) = 2*h(1);
u(1) = 0.5;
z(1) = f0(1)/l(1);

for iter = 2:n-1
    l(iter) = 2*(x0(iter+1)-x0(iter-1)) - h(iter-1)*u(iter-1);
    u(iter) = h(iter)/l(iter);
    z(iter) = (alpha(iter)-h(iter-1)*z(iter-1))/l(iter);
end

l(n) = h(n-1)*(2-u(n-1));
z(n) = (alpha(n)-h(n-1)*z(n-1))/l(n);
c(n) = z(n);

for iter = (n-1):-1:1
    c(iter) = z(iter)-u(iter)*c(iter+1);
    b(iter) = (f0(iter+1)-f0(iter))/h(iter)-h(iter)*(c(iter+1)+2*c(iter))/3;
    d(iter) = (c(iter+1)-c(iter))/(3*h(iter));
end

for iter = 1:n-1
    f_(iter) = a(iter) + b(iter)*(x-x0(iter)) + c(iter)*(x-x0(iter))^2 + d(iter)*(x-x0(iter))^3;
end
end

您的第 4 步代码中有错字

z(1) = f0(1)/l(1);

应该是

z(1) = alpha(1)/l(1);