Plotting a Parametric Spline Curve Using a Periodic Spline












1















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.










share|improve this question



























    1















    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.










    share|improve this question

























      1












      1








      1


      1






      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.










      share|improve this question














      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.







      matlab plot parametric-equations






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 9:46









      SeePlusPlusSeePlusPlus

      375




      375
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316556%2fplotting-a-parametric-spline-curve-using-a-periodic-spline%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316556%2fplotting-a-parametric-spline-curve-using-a-periodic-spline%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values