
function [toterr,sq_err] = cost_function_edge(xyEdg, spring_edges, spring_len)
% function [toterr,erri] =cost_function_edge(xyEdg, nsc, dist)
%
%
% Dr. A. I. Hanna (2005).


diff = xyEdg(spring_edges(:,1),:) - xyEdg(spring_edges(:,2),:);
d = abs(complex(diff(:,1), diff(:,2)));
err = d - spring_len;
sq_err = err.^2;
toterr = mean(sq_err);
return;



sq_err = [];
for l=1:size(spring_edges,1)

    p1 = xyEdg(spring_edges(l,1),:);
    p2 = xyEdg(spring_edges(l,2),:);
    d = norm(p1-p2);


    err = d - spring_len(l);
    sq_err = cat(1, sq_err, err^2);
end
toterr=mean(sq_err);
return;

X = xyEdg(ns,:);
figure(1); clf; hold on;
plot(xyEdg(:,1), xyEdg(:,2), 'o');

plot(X(:,1), X(:,2), 'r.');
plot(p1(:,1), p1(:,2), 'go');
plot(p2(:,1), p2(:,2), 'go');
%
%
% function [toterr,sq_err] = cost_function_edge(xyEdg, nsc, dist)
% % function [toterr,erri] =cost_function_edge(xyEdg, nsc, dist)
% % %
% % Dr. A. I. Hanna (2005).
% sq_err = [];
% for l=1:length(nsc)
%    ns=nsc{l};
%    ns = ns(ns>l);
%    p1 = xyEdg(l,:);
%    for j=ns
%        p2 = xyEdg(j,:);
%        d = norm(p1-p2);
%
%        %d = sqrt(sum((p1 - p2).^2,2));
%        err = d - dist(l, j);
%        sq_err = cat(1, sq_err, err^2);
%    end
% end
% toterr=mean(sq_err);
% return;
%
% X = xyEdg(ns,:);
% figure(1); clf; hold on;
% plot(xyEdg(:,1), xyEdg(:,2), 'o');
%
% plot(X(:,1), X(:,2), 'r.');
% plot(p1(:,1), p1(:,2), 'go');
% plot(p2(:,1), p2(:,2), 'go');
%
