matlab - Matlab如何画一个圆?(最小二乘法)
问题描述
我试图在 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])
功能[EDITED: thx to @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
推荐阅读
- mantis - 创建 Mantis 票证时插入到 custom_fields
- docker - Jenkins windows docker运行镜像失败,工作目录无效,需要是绝对路径
- c# - UnitOfWork 暴露在服务层。我正确地接近这个吗?
- kubernetes - Kubernetes setup
- r - ggplot 上 Tukey 字母图的各个方面的一致刻字
- boost - 检查 Boost::boost 目标是否存在失败
- python - Pygame 将 28x28 像素缩放为 420x420
- reactjs - 如何在材质 UI 中为每个选项卡创建具有多个标签的自定义选项卡?
- java - 使用java识别和禁用(只读)word文档(.docx)的特定部分
- javafx - 如何在不选择其项目的情况下使用 tableView 的所有数据更新数据库?