2

我实际上正在尝试为三次样条插值编写代码。三次样条曲线归结为一系列n-1段,其中n是最初给出的原始坐标的数量,每个段都由一些三次函数表示。

我已经想出了如何获取每个段的所有系数和值,存储在向量中a,b,c,d,但我不知道如何将函数绘制为不同间隔上的分段函数。到目前为止,这是我的代码。最后一个 for 循环是我尝试绘制每个段的地方。

%initializations
x = [1 1.3 1.9 2.1 2.6 3.0 3.9 4.4 4.7 5.0 6 7 8 9.2 10.5 11.3 11.6 12 12.6 13 13.3].';
y = [1.3 1.5 1.85 2.1 2.6 2.7 2.4 2.15 2.05 2.1 2.25 2.3 2.25 1.95 1.4 0.9 0.7 0.6 0.5 0.4 0.25].';

%n is the amount of coordinates
n = length(x);
%solving for a-d for all n-1 segments
a = zeros(n,1);
b = zeros(n,1);
d = zeros(n,1);

%%%%%%%%%%%%%% SOLVE FOR a's %%%%%%%%%%%%%
%Condition (b) in Definition 3.10 on pg 146
%Sj(xj) = f(xj) aka yj
for j = 1: n
    a(j) = y(j);
end

%initialize hj
h = zeros(n-1,1);

for j = 1: n-1
    h(j) = x(j+1) - x(j);
end

A = zeros(n,n);
bv = zeros(n,1); %bv = b vector

%initialize corners to 1
A(1,1) = 1;
A(n,n) = 1;

%set main diagonal
for k = 2: n-1
    A(k,k) = 2*(h(k-1) + h(k));
end

%set upper and then lower diagonals
for k = 2 : n-1
    A(k,k+1) = h(k); %h2, h3, h4...hn-1
    A(k,k-1) = h(k-1); %h1, h2, h3...hn
end

%fill up the b vector using equation in notes
%first and last spots are 0
for j = 2 : n-1
    bv(j) = 3*(((a(j+1)-a(j)) / h(j)) - ((a(j) - a(j-1)) / h(j-1)));
end

%augmented matrix
A = [A bv];



%%%%%%%%%%%% BEGIN GAUSSIAN ELIMINATION %%%%%%%%%%%%%%%
offset = 1;
%will only need n-1 iterations since "first" pivot row is unchanged
for k = 1: n-1
  %Searching from row p to row n for non-zero pivot
  for p = k : n
      if A(p,k) ~= 0;
          break;
      end
  end

  %row swapping using temp variable
  if p ~= k
      temp = A(p,:);
      A(p,:) = A(k,:);
      A(k,:) = temp;
  end


  %Eliminations to create Upper Triangular Form
  for j = k+1:n
     A(j,offset:n+1) = A(j,offset:n+1) - ((A(k, offset:n+1) * A(j,k)) / A(k,k));
  end
  offset = offset + 1;
end

c = zeros(n,1); %initializes vector of data of n rows, 1 column

%Backward Subsitution
%First, solve the nth equation
c(n) = A(n,n+1) / A(n,n);

%%%%%%%%%%%%%%%%% SOLVE FOR C's %%%%%%%%%%%%%%%%%%
%now solve the n-1 : 1 equations (the rest of them going backwards
for j = n-1:-1:1 %-1 means decrement
    c(j) = A(j,n+1);
    for k = j+1:n
        c(j) = c(j) - A(j,k)*c(k); 
    end
    c(j) = c(j)/A(j,j);
end

%%%%%%%%%%%%% SOLVE FOR B's and D's %%%%%%%%%%%%%%%%%%%%
for j = n-1 : -1 : 1
    b(j) = ((a(j+1)-a(j)) / h(j)) - (h(j)*(2*c(j) + c(j+1)) / 3);
    d(j) = (c(j+1) - c(j)) / 3*h(j);
end

%series of equation segments

for j = 1 : n-1
    f = @(x) a(j) + b(j)*(x-x(j)) + c(j)*(x-x(j))^2 + d(j)*(x-x(j))^3;
end
plot(x,y,'o');

假设我已经a,b,c,d为每个段正确计算了向量。如何绘制每个立方段,使它们都显示在一个图上?

我很感激帮助。

4

1 回答 1

2

这很容易。通过定义一个用于每个区间之间的三次样条曲线的匿名函数,您已经完成了一半的工作。但是,您需要确保函数中的操作是element-wise。您目前在标量上运行它,或者假设我们正在使用矩阵运算。不要那样做。使用.*代替*.^代替^。您需要这样做的原因是为了更容易地生成样条曲线上的点,我的下一点如下。

接下来你要做的就是x在相邻关键点定义的区间内定义一堆点x,并将它们替换到你的函数中,然后绘制结果......所以像这样:

figure;
hold on;
for j = 1 : n-1
    f = @(x) a(j) + b(j).*(x-x(j)) + c(j).*(x-x(j)).^2 + d(j)*(x-x(j)).^3; %// Change function to element-wise operations - be careful
    x0 = linspace(x(j), x(j+1)); %// Define set of points
    y0 = f(x0); %// Find output points
    plot(x0, y0, 'r'); %// Plot the line in between the key points
end
plot(x, y, 'bo');

我们生成一个新图形,然后使用hold on这样当我们plot多次调用时,我们会将结果附加到同一个图形上。接下来,对于我们拥有的每组三次样条系数,定义一个样条函数,然后生成一堆x值,这些值linspace介于当前x关键点和旁边的关键点之间。默认情况下,linspace在起点 (ie x(j)) 和终点 (ie x(j+1)) 之间生成 100 个点。您可以通过指定第三个参数来控制要生成的点数(例如linspace(x(j), x(j+1), 25);生成 25 个点)。我们使用这些x值并将它们代入我们的样条方程得到我们的y价值观。然后我们用红线在图中绘制这个结果。完成后,我们将关键点绘制为曲线顶部的蓝色空心圆圈。

作为奖励,我使用上述绘图机制运行了您的代码,这就是我得到的:

在此处输入图像描述

于 2015-03-10T04:41:09.527 回答