Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Thread Subject:
New Starter Help Implementing Best Fit Circle Function

Subject: New Starter Help Implementing Best Fit Circle Function

From: John hope

Date: 3 Aug, 2012 20:08:23

Message: 1 of 1

Hello all

New to Matlab just started in 2012. i am tryng to implement circle fit function but i have no idea how to do it.

The code which i found on the internet is as follows:-

function Par = LM(XY,ParIni,LambdaIni)

% Geometric circle fit (minimizing orthogonal distances)
% based on the standard Levenberg-Marquardt scheme
% in the full (a,b,R) parameter space
% This is perhaps the best geometric circle fit
% Input: XY(n,2) is the array of coordinates of n points x(i)=XY(i,1), y(i)=XY(i,2)
% ParIni = [a b R] is the initial guess (supplied by user)
% LambdaIni is initial value for the correction factor lambda
% (this is optional; if it is missing, LM sets it to 1)
% Output: Par = [a b R] is the fitting circle:
% center (a,b) and radius R

if (nargin < 3), LambdaIni = 1; end; % if Lambda(initial) is not supplied, set it to one

epsilon=0.000001; % tolerance (small threshold)

IterMAX = 50; % maximal number of (main) iterations; usually 10-20 suffice

lambda_sqrt = sqrt(LambdaIni); % sqrt(Lambda) is actually used by the code

Par = ParIni; % starting with the given initial guess

[J,g,F] = CurrentIteration(Par,XY); % compute objective function and its derivatives

for iter=1:IterMAX % main loop, each run is one (main) iteration

    while (1) % secondary loop - adjusting Lambda (no limit on cycles)

        DelPar = [J; lambda_sqrt*eye(3)]\[g; zeros(3,1)]; % step candidate
        progress = norm(DelPar)/(norm(Par)+epsilon);
        if (progress < epsilon) break; end; % stopping rule
        ParTemp = Par - DelPar';
        [JTemp,gTemp,FTemp] = CurrentIteration(ParTemp,XY); % objective function + derivatives

        if (FTemp < F && ParTemp(3)>0) % yes, improvement
           lambda_sqrt = lambda_sqrt/2; % reduce lambda, move to next iteration
        else % no improvement
           lambda_sqrt = lambda_sqrt*2; % increase lambda, recompute the step
    end % while (1), the end of the secondary loop
% fprintf(1,' %d %.8f %.8f %.8f\n',iter,Par);
    if (progress < epsilon) break; end; % stopping rule
    Par = ParTemp; J = JTemp; g = gTemp; F = FTemp; % update the iteration
end % the end of the main loop (over iterations)
end % LM

function [J,g,F] = CurrentIteration(Par,XY)

% computes the objective function F and its derivatives at the current point Par

Dx = XY(:,1) - Par(1);
Dy = XY(:,2) - Par(2);
D = sqrt(Dx.*Dx + Dy.*Dy);
J = [-Dx./D, -Dy./D, -ones(size(XY,1),1)];
g = D - Par(3);
F = norm(g)^2;

end % CurrentIteration

In my matlab text editor i have created two functions and saved them as CurrentIteration and LM

When go to the command line i enter LM and press enter but i get the following error:-
>> LM
??? Input argument "ParIni" is undefined.

Error in ==> LM at 28
Par = ParIni; % starting with the given initial guess

The comments state that the user enters the initial guess but where?? and also where do i input my list of x y co ordinates?

Any help will be greatly apperciated.


Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us