使用最小二乘法的椭圆
Ellipse using least squares
x = [1.02 .95 .87 .77 .67 .56 .44 .30 .16 .01]';
y = [0.39 .32 .27 .22 .18 .15 .13 .12 .13 .15]';
% function Ax = b
N = numel(x);
A = [x.^2 x.*y y.^2 x y];
b = [-1*ones(N,1)];
%coeffs = [a b c d e]';
coeffs = A\b;
a = coeffs(1)
b = coeffs(2);
c = coeffs(3);
d = coeffs(4);
e = coeffs(5);
plot(x,y, 'bo')
hold on
% prepare contour plot
xmin = min(x);
xmax = max(x);
ymin = min(y);
ymax = max(y);
%set deltas
deltay = .001;
deltax = .001;
%create arrays X and Y
[X,Y] = meshgrid(xmin:deltax:xmax,ymin:deltay:ymax);
% evaluate quadratic form
Z = a*X.^2 + b*X.*Y + c*Y.^2 + d*X + e*Y +f;
% plot where Z =0. line
contour(X,Y,Z,[0,0])
title('Planetary Orbit');
我正在尝试制作椭圆,但代码正在制作抛物线。我认为这是我设置等高线图的方式。如果有人可以给我一些需要更改的方向,将不胜感激。
如果您这样做 plot(x,y)
,您会发现输入数据只是轨道的一小部分。因为您将绘图的限制(xmin
、xmax
等)设置为输入数据的边界框,所以您的绘图将仅显示该边界框内的椭圆部分。
只需放大边界框即可显示完整的椭圆:
% OP's code here
f = 1;
xmin = -0.6; % enlarge the bounding box
xmax = 1.2;
ymin = 0;
ymax = 1.3;
[X,Y] = meshgrid(xmin:deltax:xmax,ymin:deltay:ymax);
Z = a*X.^2 + b*X.*Y + c*Y.^2 + d*X + e*Y +f;
contour(X,Y,Z,[0,0])
axis equal
hold on
plot(x,y,'r.') % Data points on top of the ellipse
x = [1.02 .95 .87 .77 .67 .56 .44 .30 .16 .01]';
y = [0.39 .32 .27 .22 .18 .15 .13 .12 .13 .15]';
% function Ax = b
N = numel(x);
A = [x.^2 x.*y y.^2 x y];
b = [-1*ones(N,1)];
%coeffs = [a b c d e]';
coeffs = A\b;
a = coeffs(1)
b = coeffs(2);
c = coeffs(3);
d = coeffs(4);
e = coeffs(5);
plot(x,y, 'bo')
hold on
% prepare contour plot
xmin = min(x);
xmax = max(x);
ymin = min(y);
ymax = max(y);
%set deltas
deltay = .001;
deltax = .001;
%create arrays X and Y
[X,Y] = meshgrid(xmin:deltax:xmax,ymin:deltay:ymax);
% evaluate quadratic form
Z = a*X.^2 + b*X.*Y + c*Y.^2 + d*X + e*Y +f;
% plot where Z =0. line
contour(X,Y,Z,[0,0])
title('Planetary Orbit');
我正在尝试制作椭圆,但代码正在制作抛物线。我认为这是我设置等高线图的方式。如果有人可以给我一些需要更改的方向,将不胜感激。
如果您这样做 plot(x,y)
,您会发现输入数据只是轨道的一小部分。因为您将绘图的限制(xmin
、xmax
等)设置为输入数据的边界框,所以您的绘图将仅显示该边界框内的椭圆部分。
只需放大边界框即可显示完整的椭圆:
% OP's code here
f = 1;
xmin = -0.6; % enlarge the bounding box
xmax = 1.2;
ymin = 0;
ymax = 1.3;
[X,Y] = meshgrid(xmin:deltax:xmax,ymin:deltay:ymax);
Z = a*X.^2 + b*X.*Y + c*Y.^2 + d*X + e*Y +f;
contour(X,Y,Z,[0,0])
axis equal
hold on
plot(x,y,'r.') % Data points on top of the ellipse