matlab最小二乘圆程序,matlab - 使用MATLAB Optimization Toolbox的最小二乘圆拟合 - 堆栈内存溢出...

值得一提的是,我不久前在MATLAB中实现了这些方法。 但是,我显然在知道lsqnonlin等之前就lsqnonlin ,因为它使用了手动执行的回归。 这可能很慢,但可能有助于与您的代码进行比较。

function [x, y, r, sq_error] = circFit ( P )

%# CIRCFIT fits a circle to a set of points using least sqaures

%# P is a 2 x n matrix of points to be fitted

per_error = 0.1/100; % i.e. 0.1%

%# initial estimates

X = mean(P, 2)';

r = sqrt(mean(sum((repmat(X', [1, length(P)]) - P).^2)));

v_cen2points = zeros(size(P));

niter = 0;

%# looping until convergence

while niter < 1 || per_diff > per_error

%# vector from centre to each point

v_cen2points(1, :) = P(1, :) - X(1);

v_cen2points(2, :) = P(2, :) - X(2);

%# distacnes from centre to each point

centre2points = sqrt(sum(v_cen2points.^2));

%# distances from edge of circle to each point

d = centre2points - r;

%# computing 3x3 jacobean matrix J, and solvign matrix eqn.

R = (v_cen2points ./ [centre2points; centre2points])';

J = [ -ones(length(R), 1), -R ];

D_rXY = -J\d';

%# updating centre and radius

r_old = r; X_old = X;

r = r + D_rXY(1);

X = X + D_rXY(2:3)';

%# calculating maximum percentage change in values

per_diff = max(abs( [(r_old - r) / r, (X_old - X) ./ X ])) * 100;

%# prevent endless looping

niter = niter + 1;

if niter > 1000

error('Convergence not met in 1000 iterations!')

end

end

x = X(1);

y = X(2);

sq_error = sum(d.^2);

然后运行:

X = [1 2 5 7 9 3];

Y = [7 6 8 7 5 7];

[x_centre, y_centre, r] = circFit( [X; Y] )

并绘制为:

[X, Y] = cylinder(r, 100);

scatter(X, Y, 60, '+r'); axis equal

hold on

plot(X(1, :) + x_centre, Y(1, :) + y_centre, '-b', 'LineWidth', 1);

给予:

376b999aae9b0ce4c1e6421a8f09d480.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值