首页 > 解决方案 > 使用周期样条绘制参数样条曲线

问题描述

我想使用周期性样条绘制这样的曲线:在此处输入图像描述

我有以下价值观:在此处输入图像描述

因此,我创建了以下 matlab 代码来绘制此函数:

%initializing values
t = [1,2,3,4,5,6,7,8,9,10,11,12,13];
tplot = [1:0.1:13];
x = [2.5,1.3,-0.25,0.0,0.25,-1.3,-2.5,-1.3,0.25,0.0,-0.25,1.3,2.5];
y = [0.0,-0.25,1.3,2.5,1.3,-0.25,0.0,0.25,-1.3,-2.5,-1.3,0.25,0.0];
[a1, b1, c1, d1] = perspline2(t,x);
[a2, b2, c2, d2] = perspline2(t,y);

for i = 1:12
  xx = linspace(x(i), x(i+1), 100);
  xxx = a1(i) + b1(i)*(xx-x(i)) + c1(i)*(xx-x(i)).^2 ...
       + d1(i)*(xx-x(i)).^3;
  yyy = a2(i) + b2(i)*(xx-x(i)) + c2(i)*(xx-x(i)).^2 ...
       + d2(i)*(xx-x(i)).^3;
  h3=plot(xxx, yyy, 'r-');
  hold on
end
plot(x,y,'k.', 'MarkerSize', 30)
hold off

perspline2() 看起来像这样:

function [a1,b1,c1,d1] = perspline2(xnot,ynot)
    x = xnot';
    y = ynot';
    n = length(x) - 1;

    h = diff(x);
    diag0 = [1; 2*(h(1:end-1)+h(2:end)); 2*h(end)];
    A = spdiags([[h;0], diag0, [0;h]], [-1, 0, 1], n+1, n+1);
    % Do a little surgery on the matrix:
    A(1,2)   = 0;
    A(1,end) = -1;
    A(end,1) = 2*h(1);
    A(end,2) = h(1);
    dy = diff(y);
    rhs = 6*[0; diff(dy./h); dy(1)/h(1)-dy(end)/h(end)];
    m = A \ rhs;     % Solve for the slopes, S''(x_i)

    % Compute the coefficients of the cubics.
    a1 = y;
    b1 = dy./h - h.*m(1:end-1)/2 - h.*diff(m)/6;
    c1 = m/2;
    d1 = diff(m)./h/6;

所以基本上,我知道我必须使用参数样条来找到要绘制的正确点。我t = [1,2,3,4,5,6,7,8,9,10,11,12,13];用作我的索引。因此,我找到 t 与 x 的三次样条多项式的系数,然后找到 t 与 y 的系数,然后我尝试使用 t 中的值将它们相互绘制以绘制参数曲线。但是,我不断得到这条曲线: 在此处输入图像描述

我真的不确定为什么会发生这种情况。

PS 我知道我可以使用 matlab 样条函数,但是当我这样做时,它会导致正确的尖点比其他尖点大一点。我希望所有尖点的大小都相等,并且作业说我们必须使用三次样条。

任何帮助是极大的赞赏。

标签: matlabplotparametric-equations

解决方案


推荐阅读