三次样条程序

时间:2011-10-04 03:18:18

标签: matlab matrix interpolation spline

我正在尝试编写一个三次样条插值程序。我编写了程序,但图表没有正确显示。样条曲线使用自然边界条件(起始/结束节点处的第二个衍生物为0)。代码在Matlab中,如下所示,

clear all
%Function to Interpolate
k = 10;                    %Number of Support Nodes-1
xs(1) = -1;
for j = 1:k
    xs(j+1) = -1 +2*j/k;   %Support Nodes(Equidistant)
end;
fs = 1./(25.*xs.^2+1);     %Support Ordinates
x = [-0.99:2/(2*k):0.99];  %Places to Evaluate Function
fx = 1./(25.*x.^2+1);      %Function Evaluated at x

%Cubic Spline Code(Coefficients to Calculate 2nd Derivatives)

f(1) = 2*(xs(3)-xs(1));
g(1) = xs(3)-xs(2);
r(1) = (6/(xs(3)-xs(2)))*(fs(3)-fs(2)) + (6/(xs(2)-xs(1)))*(fs(1)-fs(2));
e(1) = 0;

for i = 2:k-2
    e(i) = xs(i+1)-xs(i);
    f(i) = 2*(xs(i+2)-xs(i));
    g(i) = xs(i+2)-xs(i+1);
    r(i) = (6/(xs(i+2)-xs(i+1)))*(fs(i+2)-fs(i+1)) + ...
           (6/(xs(i+1)-xs(i)))*(fs(i)-fs(i+1));
end
e(k-1) = xs(k)-xs(k-1);
f(k-1) = 2*(xs(k+1)-xs(k-1));
r(k-1) = (6/(xs(k+1)-xs(k)))*(fs(k+1)-fs(k)) + ...
         (6/(xs(k)-xs(k-1)))*(fs(k-1)-fs(k));

%Tridiagonal System

i = 1;
A = zeros(k-1,k-1);
while i < size(A)+1;
    A(i,i) = f(i);
    if i < size(A);
        A(i,i+1) = g(i);
        A(i+1,i) = e(i);
    end
    i = i+1;
end

for i = 2:k-1                             %Decomposition
    e(i) = e(i)/f(i-1);
    f(i) = f(i)-e(i)*g(i-1);
end

for i = 2:k-1                             %Forward Substitution 
    r(i) = r(i)-e(i)*r(i-1);
end

xn(k-1)= r(k-1)/f(k-1);
for i = k-2:-1:1                          %Back Substitution
    xn(i) = (r(i)-g(i)*xn(i+1))/f(i);
end

%Interpolation

if (max(xs) <= max(x))
    error('Outside Range'); 
end

if (min(xs) >= min(x))
    error('Outside Range'); 
end


P = zeros(size(length(x),length(x)));
i = 1;
for Counter = 1:length(x)
    for j = 1:k-1
        a(j) = x(Counter)- xs(j);
    end
    i = find(a == min(a(a>=0)));
    if i == 1
        c1 = 0;
        c2 = xn(1)/6/(xs(2)-xs(1));
        c3 = fs(1)/(xs(2)-xs(1));
        c4 = fs(2)/(xs(2)-xs(1))-xn(1)*(xs(2)-xs(1))/6;
        t1 = c1*(xs(2)-x(Counter))^3;
        t2 = c2*(x(Counter)-xs(1))^3;
        t3 = c3*(xs(2)-x(Counter));
        t4 = c4*(x(Counter)-xs(1));
        P(Counter) = t1 +t2 +t3 +t4;
    else
        if i < k-1
        c1 = xn(i-1+1)/6/(xs(i+1)-xs(i-1+1));
        c2 = xn(i+1)/6/(xs(i+1)-xs(i-1+1));
        c3 = fs(i-1+1)/(xs(i+1)-xs(i-1+1))-xn(i-1+1)*(xs(i+1)-xs(i-1+1))/6;
        c4 = fs(i+1)/(xs(i+1)-xs(i-1+1))-xn(i+1)*(xs(i+1)-xs(i-1+1))/6;
        t1 = c1*(xs(i+1)-x(Counter))^3;
        t2 = c2*(x(Counter)-xs(i-1+1))^3;
        t3 = c3*(xs(i+1)-x(Counter));
        t4 = c4*(x(Counter)-xs(i-1+1));
        P(Counter) = t1 +t2 +t3 +t4;
        else
        c1 = xn(i-1+1)/6/(xs(i+1)-xs(i-1+1));
        c2 = 0;
        c3 = fs(i-1+1)/(xs(i+1)-xs(i-1+1))-xn(i-1+1)*(xs(i+1)-xs(i-1+1))/6;
        c4 = fs(i+1)/(xs(i+1)-xs(i-1+1));
        t1 = c1*(xs(i+1)-x(Counter))^3;
        t2 = c2*(x(Counter)-xs(i-1+1))^3;
        t3 = c3*(xs(i+1)-x(Counter));
        t4 = c4*(x(Counter)-xs(i-1+1));
        P(Counter) = t1 +t2 +t3 +t4;
        end    
    end
end

P = P';
P(length(x)) = NaN;

plot(x,P,x,fx)

当我运行代码时,插值函数不对称,并且它没有正确收敛。任何人都可以对我的代码中的问题提出任何建议吗感谢。

2 个答案:

答案 0 :(得分:9)

很久以前我在Mathematica写了一个三次样条包。这是我将该包翻译成Matlab的。注意我在大约7年内没有看过三次样条函数,所以我基于我自己的文档。你应该检查我说的一切。

基本问题是我们得到n个数据点(x(1), y(1)) , ... , (x(n), y(n)),我们希望计算分段三次插值。插值定义为

   S(x) = {  Sk(x)   when x(k) <= x <= x(k+1)
          {  0       otherwise 

这里Sk(x)是形式

的三次多项式
  Sk(x) = sk0 + sk1*(x-x(k)) + sk2*(x-x(k))^2 + sk3*(x-x(k))^3

样条曲线的属性为:

  1. 样条线通过数据点Sk(x(k)) = y(k)
  2. 样条曲线在端点处是连续的,因此在插值间隔Sk(x(k+1)) = Sk+1(x(k+1))
  3. 中无处不在
  4. 样条线具有连续的一阶导数Sk'(x(k+1)) = Sk+1'(x(k+1))
  5. 样条曲线具有连续的二阶导数Sk''(x(k+1)) = Sk+1''(x(k+1))
  6. 要从一组数据点构造三次样条,我们需要求解系数 每个sk0三次多项式的sk1sk2sk3n-1。这总共有4*(n-1) = 4*n - 4个未知数。属性1提供n约束,属性2,3,4各自提供额外的n-2约束。因此,我们有n + 3*(n-2) = 4*n - 6个约束和4*n - 4未知数。这留下了两个自由度。我们通过在起始节点和结束节点处将二阶导数设置为零来修复这些自由度。

    允许m(k) = Sk''(x(k))h(k) = x(k+1) - x(k)d(k) = (y(k+1) - y(k))/h(k)。下列 三期复发关系成立

      h(k-1)*m(k-1) + 2*(h(k-1) + h(k))*m(k) + h(k)*m(k+1) = 6*(d(k) - d(k-1))
    

    m(k)是我们希望解决的未知数。 h(k)d(k)由输入数据定义。 这个三项递归关系定义了一个三对角线性系统。确定m(k)后,Sk的系数由

    给出
       sk0 = y(k)
       sk1 = d(k) - h(k)*(2*m(k) + m(k-1))/6
       sk2 = m(k)/2
       sk3 = m(k+1) - m(k)/(6*h(k))
    

    好的,这是完全定义计算三次样条曲线的算法所需的全部数学运算。这是在Matlab中:

    function [s0,s1,s2,s3]=cubic_spline(x,y)
    
    if any(size(x) ~= size(y)) || size(x,2) ~= 1
       error('inputs x and y must be column vectors of equal length');
    end
    
    n = length(x)
    
    h = x(2:n) - x(1:n-1);
    d = (y(2:n) - y(1:n-1))./h;
    
    lower = h(1:end-1);
    main  = 2*(h(1:end-1) + h(2:end));
    upper = h(2:end);
    
    T = spdiags([lower main upper], [-1 0 1], n-2, n-2);
    rhs = 6*(d(2:end)-d(1:end-1));
    
    m = T\rhs;
    
    % Use natural boundary conditions where second derivative
    % is zero at the endpoints
    
    m = [ 0; m; 0];
    
    s0 = y;
    s1 = d - h.*(2*m(1:end-1) + m(2:end))/6;
    s2 = m/2;
    s3 =(m(2:end)-m(1:end-1))./(6*h);
    

    以下是绘制三次样条曲线的一些代码:

    function plot_cubic_spline(x,s0,s1,s2,s3)
    
    n = length(x);
    
    inner_points = 20;
    
    for i=1:n-1
       xx = linspace(x(i),x(i+1),inner_points);
       xi = repmat(x(i),1,inner_points);
       yy = s0(i) + s1(i)*(xx-xi) + ... 
         s2(i)*(xx-xi).^2 + s3(i)*(xx - xi).^3;
       plot(xx,yy,'b')
       plot(x(i),0,'r');
    end
    

    这是一个构造三次样条函数的函数,并绘制着名的Runge函数:

    function cubic_driver(num_points)
    
    runge = @(x) 1./(1+ 25*x.^2);
    
    x = linspace(-1,1,num_points);
    y = runge(x);
    
    [s0,s1,s2,s3] = cubic_spline(x',y');
    
    plot_points = 1000;
    xx = linspace(-1,1,plot_points);
    yy = runge(xx);
    
    plot(xx,yy,'g');
    hold on;
    plot_cubic_spline(x,s0,s1,s2,s3);
    

    您可以通过在Matlab提示符下运行以下内容来查看它的实际效果

     >> cubic_driver(5)
     >> clf 
     >> cubic_driver(10)
     >> clf
     >> cubic_driver(20)
    

    当你有20个节点时,你的插值在视觉上与Runge函数无法区分。

    对Matlab代码的一些评论:我不使用任何for或while循环。我能够对所有操作进行矢量化。我用spdiags快速形成稀疏的三对角矩阵。我使用反斜杠运算符解决它。我依靠Tim Davis的UMFPACK来处理分解以及向前和向后的解决方案。

    希望有所帮助。该代码在github https://gist.github.com/1269709

    上作为要点提供

答案 1 :(得分:3)

样条函数存在错误,(n-2)生成(n-2)应该是对称的:

lower = h(2:end);
main  = 2*(h(1:end-1) + h(2:end));
upper = h(1:end-1);

http://www.mpi-hd.mpg.de/astrophysik/HEA/internal/Numerical_Recipes/f3-3.pdf