如何在Matlab中绘制圆? (最小二乘)

问题描述 投票:0回答:1

我正在尝试在x和y上绘制圆的方程式回归,但是我不知道如何进行。有什么建议么? (我想要一个圆通过最小二乘解连接点)

x = [5; 4; -1; 1];
y = [3; 5; 2; 1];

% circle's equation: x^2+y^2 = 2xc1+2yc2+c3
a = [2.*x,2.*y,ones(n,3)]
b = [x.^2 + y.^2];
c = a\b;

如何在此之后绘制圆

matlab
1个回答
2
投票

有两种方法可以在Matlab中绘制圆:

  • 绘制数据点形成圆的线
  • 使用绘图中的'o' marker'MarkerSize'名称-值对设置圆的半径
  • 您可以使用vscircle function绘制圆形图像对于您而言,我会选择第一种方法,因为您可以控制圆的大小。
  • 使用rectangle(...,'Curvature',[1 1])function[编辑:谢谢@Cris Luengo]

所以这是绘图功能

function circle(x,y,r)
%x and y are the coordinates of the center of the circle
%r is the radius of the circle
%0.01 is the angle step, bigger values will draw the circle faster but
%you might notice imperfections (not very smooth)
ang=0:0.01:2*pi+.01; 
xp=r*cos(ang);
yp=r*sin(ang);

plot(x+xp,y+yp);
end

因此,您的(更正的)代码看起来像这样

x = [5; 4; -1; 1];
y = [3; 5; 2; 1];

% circle's equation:  x^2+y^2 = 2xc1+2yc2+c3
a = [2.*x,2.*y,ones(length(x),1)];
b = [x.^2 + y.^2];
c = a\b;

x_m = c(1)/2;
y_m = c(2)/2;
r = sqrt(x_m^2 + y_m^2 -c(3));

% plot data points
plot(x,y,'o')
hold on
% plot center
plot(x_m,y_m,'+')
% plot circle
circle(x_m,y_m,r)
hold off

matlab plot

© www.soinside.com 2019 - 2024. All rights reserved.