簡體   English   中英

如何在Matlab中繪制一個圓? (最小二乘法)

[英]How to plot a circle in Matlab? (least square)

我試圖在 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 中有兩種繪制圓的方法:

  • 繪制一條線,其中數據點形成一個圓圈
  • 使用繪圖中的'o'標記'MarkerSize'名稱-值對來設置圓的半徑
  • 您可以使用vscircle 函數繪制圓形圖像在您的情況下,我會選擇第一個選項,因為您可以控制圓形大小。
  • 使用rectangle(...,'Curvature',[1 1]) 函數[編輯:感謝@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 圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM