使用周期样条曲线绘制参数样条曲线

时间:2018-11-15 09:46:59

标签: matlab plot parametric-equations

我想使用周期性样条曲线绘制这样的曲线:enter image description here

我具有以下值:enter image description here

因此,我创建了以下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中的值将它们相对绘制以绘制参数曲线。但是,我不断得到这个曲线: enter image description here

我真的不确定为什么会这样。

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

非常感谢您的帮助。

0 个答案:

没有答案