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'

 采纳的回答

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.
I included the code for fixed radius.
It's clear that the fit becomes worse the more restrictions you impose.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Simulation, Tuning, and Visualization 的更多信息

产品

版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by