from Fitting NURBS to a given set of data points by Vikash Gupta
Given a set of six points a closed NURBS curve is used to approximately fit all the six data points.

getbspline(P)
function [x,y] = getbspline(P)

n=5;

X=[];X1=[];
Y=[];Y1=[];
for i=1:n
    for u=0:0.01:1
        knot1=mod(i-2,n)+1;
        knot2=mod((i-1),n)+1;
        knot3=mod((i+0),n)+1;
        knot4=mod((i+1),n)+1;
                knot5=mod((i+2),n)+1;
        points=[P(knot1,:);P(knot2,:);P(knot3,:);P(knot4,:);P(knot5,:)];
%         U=[u^3 u^2 u 1];
%         M=1/6*[-1 3 -3 1;3 -6 3 0;-3 0 3 0;1 4 1 0];
            
            U=[u^4 u^3 u^2 u 1];
            M=1/24*[1 -4 6 -4 1;-4 12 -12 4 0;6 -6 -6 6 0;-4 -12 12 4 0; 1 11 11 1 0];
        PP1=U*M*points;

        X=[X PP1(1)];
        Y=[Y PP1(2)];
        

        
    end    
end
% plot(X,Y,'r','linewidth',2);
hold on
% for i=1:n
%     plot(P(i,1),P(i,2),'.','markersize',20);hold on;
%     plot([P(i,1) P(i+1,1)],[P(i,2) P(i+1,2)],'--','linewidth',2); hold on;
% end
%  plot([P(n,1) P(1,1)],[P(n,2) P(1,2)],'--.','linewidth',2,'markersize',20);
x=X; y=Y;
axis('square');    

Contact us at files@mathworks.com