Plotting a Parametric Spline Curve Using a Periodic Spline

577 Views Asked by At

I want to plot a curve like this using a periodic spline: enter image description here

I have the following values: enter image description here

And so, I have created the following matlab code to plot this function:

%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

With perspline2() looking like this:

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;

So basically, I know that I must use a parametric spline in order to find the correct points to plot. I use t = [1,2,3,4,5,6,7,8,9,10,11,12,13]; as my indexes. So, I find the coefficients for the cubic spline polynomial of t vs. x and then find the coefficients for t vs. y, and then I attempt to plot them against each other using values from t in order to plot the parametric curve. However, I keep getting this curve: enter image description here

I am really not sure why this is occurring.

P.S I know I can use the matlab spline function but when I do, it results in the right cusp being a bit bigger than the other cusps. I want all cusps to be equal in size and the assignment says that we must use a cubic spline.

Any help is greatly appreciated.

0

There are 0 best solutions below