% file: optim_w_fileexchange.m
addpath('/home/al/matlab/DE'); % location of unzipped files
optimInfo.title = 'Shape alignment';
tolerance = 0.0001;
paramDefCell = {
	'parameter1', [-1 1], tolerance  % offset_x
	'parameter2', [-1 1], tolerance  % offset_y
};
objFctParams.offset_x = 0;
objFctParams.offset_y = 0;
objFctHandle = @cost;
DEParams = getdefaultparams;
DEParams.maxiter = 50;
objFctSettings = 100;
emailParams = [];  % no email
[bestmem, bestval, bestFctParams, nrOfIterations, resultFileName] = ...
    differentialevolution(DEParams, paramDefCell, objFctHandle,     ...
    objFctSettings, objFctParams, emailParams, optimInfo);

function [sum_dist] = cost(scale, params)
    offset_x = params.parameter1(1);
    offset_y = params.parameter2(1);
    known = [0.6 0.5; 0.6 0.9; 0.7 0.5];
    target = [ 8.147236863931789e-01, 1.576130816775483e-01;
               9.057919370756192e-01, 9.705927817606157e-01;
               2.0e-01              , 3.0e-01              ;
               9.133758561390194e-01, 4.853756487228412e-01;
               6.323592462254095e-01, 8.002804688888001e-01;
               2.0e-01              , 7.0e-01              ;
               2.784982188670484e-01, 4.217612826262750e-01;
               5.468815192049838e-01, 9.157355251890671e-01;
               9.575068354342976e-01, 7.922073295595544e-01;
               3.0e-01              , 3.0e-01              ];
    offset_target = target + [offset_x, offset_y];
    dist = sqrt( ( offset_target(:,1) - known(:,1)' ).^2 + ...
                 ( offset_target(:,2) - known(:,2)' ).^2 );
    sum_dist = scale*sum(min(dist));
end