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.

getdistance(C,A)
function [sqerror,indices] = getdistance(C,A)
    
    dist=zeros(length(C),1);
    hold on; error=0;
    for i=1:(length(A))
        for j=1:length(C)
            dist(j)=norm([A(i,1)-C(j,1),A(i,2)-C(j,2)]);
        end
        [err,ind]=min(dist);
        error = error+ err*err*err*err*err*err;
%         line([A(i,1),C(ind,1)],[A(i,2),C(ind,2)]);
    end
    sqerror=error;
    indices=ind;
    
    
    

Contact us at files@mathworks.com