You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: chamfer_match.m
    path of file: ./repos/machinevision-toolbox-matlab/examples
    
    the code of the file until where you have to start completion: function chamfer_match
    
    % Things to try:
    %   - modify to search for just position and orientation, you set the scale
    %   - vary the number of particles in the swarm
    %   - try different optimizers
    %   - implement truncated chamfer match
    %   - extend to directional chamfer match, use the VLFeat implementation of
    %     distance transform which optionally returns the closest set pixel
    %   - test on an image of a real square

    
    %% create the outline of a rotated square
    im = testpattern('squares', 256, 256, 128);
    im = irotate(im, -0.3);
    edges = icanny(im) > 0;
    idisp(edges);

    
    %% compute the distance transform and display it
    dx = distancexform(edges, 'euclidean');
    idisp(dx, 'bar', 'new')

    %% create the model
    % vertices of a square centred at origin with side length of 1
    corners = [-1 -1; 1 -1; 1 1; -1 1; -1 -1]'/2 ;

    
    %% minimize!
    
    % we use particle swarm since it provides constrained minimization and
    % copes well with local minima
    options = optimoptions('particleswarm', 'OutputFcns', @display)
  
    % state vector is: x, y, theta, scale
    [xf,ef] = particleswarm( @cost, 4,  ...
        [10 10 -pi/2 50], ...   % lower bounds
        [200 200 pi/2 200], ... % upper bounds
        options )
    
    %% nested functions to compute cost and display progress
    %   these have access to variables in this scope such as dx and corners
    
    function c = cost(x)
        % compute cost for this particle
        %  x = (x y theta scale)
        
        % compute an SE2 transform
        T = SE2(x(1:3));
        

    
function chamfer_match
    
    % Things to try:
    %   - modify to search for just position and orientation, you set the scale
    %   - vary the number of particles in the swarm
    %   - try different optimizers
    %   - implement truncated chamfer match
    %   - extend to directional chamfer match, use the VLFeat implementation of
    %     distance transform which optionally returns the closest set pixel
    %   - test on an image of a real square

    
    %% create the outline of a rotated square
    im = testpattern('squares', 256, 256, 128);
    im = irotate(im, -0.3);
    edges = icanny(im) > 0;
    idisp(edges);

    
    %% compute the distance transform and display it
    dx = distancexform(edges, 'euclidean');
    idisp(dx, 'bar', 'new')

    %% create the model
    % vertices of a square centred at origin with side length of 1
    corners = [-1 -1; 1 -1; 1 1; -1 1; -1 -1]'/2 ;

    
    %% minimize!
    
    % we use particle swarm since it provides constrained minimization and
    % copes well with local minima
    options = optimoptions('particleswarm', 'OutputFcns', @display)
  
    % state vector is: x, y, theta, scale
    [xf,ef] = particleswarm( @cost, 4,  ...
        [10 10 -pi/2 50], ...   % lower bounds
        [200 200 pi/2 200], ... % upper bounds
        options )
    
    %% nested functions to compute cost and display progress
    %   these have access to variables in this scope such as dx and corners
    
    function c = cost(x)
        % compute cost for this particle
        %  x = (x y theta scale)
        
        % compute an SE2 transform
        T = SE2(x(1:3));
        
        % scale the corners and then apply SE2 transform
        p = T * (x(4)*corners);
        
        % check if all the vertices are within the image
        c = [p(:)-1; 256-p(:)];
        if all(c>0)
            % vertices are inside
            
            dp = iprofile(dx, p);  % compute cost at each point along all line segments
            c = sum( dp );  % compute total cost (scalar)
            c = double(c);  % ensure result is a double
            c = c / x(4);   % normalize cost with respect to size
        else
            % vertices outside, penalize the particle
            c = Inf;
        end