Where does the diffrence beetween lsqcurvefit and data come from?

Hello,
I am using lsqcurvefit to determine the best fitting circle (in terms of the center point) with known tangent point and radius to a certain number of points. The code works fine but as shown in the screenshot below, one can see that there is a gap beetween the data and the fitted values.
Is the error located in the code or in the function?
Problem:
Code:
load('data-ask.mat')
% Gleichung 1: Tangentengleichung mit wt als Tangentenpunkt und bekanntem Radius
fun =@(x1,xdata1)((x1(1).^2-x1(1).*gp(1)-x1(1).*xdata1+x1(2).^2-x1(2).*gp(2)+gp(1).*xdata1-gr.^2).*((x1(2)-gp(2)).^-1));
u = [mean(xdata1) mean(ydata1)+gr];
options=optimoptions(@lsqcurvefit,'StepTolerance',1e-32);
lb=[0 min(ydata1)];
ub=[inf gp(2)+gr];
x1 = lsqcurvefit(fun,u,xdata1,ydata1,lb,ub,options);
%figure
figure()
h(1)=plotCircle(x1(1,1),x1(1,2),gr);
h(1).Marker='*'
h(1).Color='black';
hold on;axis equal; grid on
s(2)=scatter(xdata1,ydata1,'red','s');
xlim([min(xdata1) max(xdata1)])
ylim([min(ydata1) max(ydata1)])
l=legend({'Circle-Fit' 'Data'})
l.Location='northoutside'

 채택된 답변

Torsten
Torsten 2022년 3월 31일
편집: Torsten 2022년 3월 31일
Does this answer your question ?
1st code (free radius):
load('data-ask.mat')
fun =@(x)(xdata1-x(1)).^2+(ydata1-x(2)).^2 - x(3)^2;
u = [mean(xdata1) mean(ydata1) 5];
x1 = lsqnonlin(fun,u);
t = linspace((3/2-1/16)*pi,(3/2+3/16)*pi,100)';
circsx = x1(3)*cos(t) + x1(1);
circsy = x1(3)*sin(t) + x1(2);
plot(circsx,circsy);
h(1).Marker='*'
h(1).Color='black';
hold on;axis equal; grid on
s(2)=scatter(xdata1,ydata1);%,'red','s');
l=legend({'Circle-Fit' 'Data'})
l.Location='northoutside'
2nd code (radius fixed):
load('data-ask.mat')
fun =@(x)(xdata1-x(1)).^2+(ydata1-x(2)).^2 - gr^2;
u = [mean(xdata1) mean(ydata1)];
x1 = lsqnonlin(fun,u);
t = linspace((3/2-1/16)*pi,(3/2+3/16)*pi,100)';
circsx = gr*cos(t) + x1(1);
circsy = gr*sin(t) + x1(2);
plot(circsx,circsy);
h(1).Marker='*'
h(1).Color='black';
hold on;axis equal; grid on
s(2)=scatter(xdata1,ydata1);%,'red','s');
l=legend({'Circle-Fit' 'Data'})
l.Location='northoutside'
3rd code: (radius and tangent fixed)
Your code

댓글 수: 2

Thanks for this alternative solution to my Problem. Unfortunately it does not consider a fixed radius. I have slightly modified your answer and checked it. It's getting better but doesn't solve completely. I have to find out if this a result of setting the radius fixed. Your code helped anways.
Torsten
Torsten 2022년 3월 31일
편집: Torsten 2022년 3월 31일
I included the code for fixed radius.
It's clear that the fit becomes worse the more restrictions you impose.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Nonlinear Least Squares (Curve Fitting)에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2022년 3월 31일

편집:

2022년 3월 31일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by