给出一些椭圆上离散的点的横纵坐标,怎么用matlab拟合出椭圆方程
来源:学生作业帮 编辑:搜搜做题作业网作业帮 分类:数学作业 时间:2024/08/19 16:31:41
给出一些椭圆上离散的点的横纵坐标,怎么用matlab拟合出椭圆方程
比如这些点是(1,0) (-1,0) (0,2 ) (0,-2)
比如这些点是(1,0) (-1,0) (0,2 ) (0,-2)
![给出一些椭圆上离散的点的横纵坐标,怎么用matlab拟合出椭圆方程](/uploads/image/z/19313354-2-4.jpg?t=%E7%BB%99%E5%87%BA%E4%B8%80%E4%BA%9B%E6%A4%AD%E5%9C%86%E4%B8%8A%E7%A6%BB%E6%95%A3%E7%9A%84%E7%82%B9%E7%9A%84%E6%A8%AA%E7%BA%B5%E5%9D%90%E6%A0%87%2C%E6%80%8E%E4%B9%88%E7%94%A8matlab%E6%8B%9F%E5%90%88%E5%87%BA%E6%A4%AD%E5%9C%86%E6%96%B9%E7%A8%8B)
M文件的代码如下:
function [newX,newY,v]=FitEllip(X,Y,N)
%本函数用最小二乘法拟合椭圆
%输入变量:X、Y为数据点坐标(列向量),N为输出椭圆上的点的数量
%输出变量:newX,newY为拟合的椭圆上的点的坐标(列向量)
%输出变量:v为拟合的椭圆参数,是一个5维行向量,v(1)、v(2)分别为长、短轴,v(3)、v(4)分别为椭圆中心点横、纵坐标,v(5)为长轴与x轴夹角
% a = fitellip(X1,Y1);
mx = mean(X);my = mean(Y); sx = (max(X)-min(X))/2; sy = (max(Y)-min(Y))/2; x = (X-mx)/sx; y = (Y-my)/sy;
% Build design matrix
D = [ x.*x x.*y y.*y x y ones(size(x)) ];
% Build scatter matrix
S = D'*D;
% Build 6x6 constraint matrix
C(6,6) = 0; C(1,3) = -2; C(2,2) = 1; C(3,1) = -2;
% Solve eigensystem
[gevec,geval] = eig(S,C);
% Find the negative eigenvalue
[NegR1,NegC] = find(geval < 0 & isinf(geval));
% Extract eigenvector corresponding to positive eigenvalue
A = gevec(:,NegC);
% unnormalize
a(1)=A(1)*sy*sy;
a(2)=A(2)*sx*sy;
a(3)=A(3)*sx*sx;
a(4)=-2*A(1)*sy*sy*mx -A(2)*sx*sy*my + A(4)*sx*sy*sy;
a(5)=-A(2)*sx*sy*mx -2*A(3)*sx*sx*my + A(5)*sx*sx*sy;
a(6)=A(1)*sy*sy*mx*mx + A(2)*sx*sy*mx*my + A(3)*sx*sx*my*my-A(4)*sx*sy*sy*mx -A(5)*sx*sx*sy*my+ A(6)*sx*sx*sy*sy;
a=a';
% get ellipse orientation
theta = atan2(a(2),a(1)-a(3))/2;
% get scaled major/minor axes
ct = cos(theta); st = sin(theta); ap = a(1)*ct*ct + a(2)*ct*st + a(3)*st*st; cp = a(1)*st*st -a(2)*ct*st + a(3)*ct*ct;
% get translations
T = [[a(1) a(2)/2]' [a(2)/2 a(3)]']; t = -inv(2*T)*[a(4) a(5)]'; cx = t(1); cy = t(2);
% get scale factor
val = t'*T*t; scale = 1 / (val-a(6));
% get major/minor axis radii
r1 = 1/sqrt(scale*ap);
r2 = 1/sqrt(scale*cp);
v = [r1 r2 cx cy theta]';
%判定长轴、短轴是否与r1、r2对应
if r1
function [newX,newY,v]=FitEllip(X,Y,N)
%本函数用最小二乘法拟合椭圆
%输入变量:X、Y为数据点坐标(列向量),N为输出椭圆上的点的数量
%输出变量:newX,newY为拟合的椭圆上的点的坐标(列向量)
%输出变量:v为拟合的椭圆参数,是一个5维行向量,v(1)、v(2)分别为长、短轴,v(3)、v(4)分别为椭圆中心点横、纵坐标,v(5)为长轴与x轴夹角
% a = fitellip(X1,Y1);
mx = mean(X);my = mean(Y); sx = (max(X)-min(X))/2; sy = (max(Y)-min(Y))/2; x = (X-mx)/sx; y = (Y-my)/sy;
% Build design matrix
D = [ x.*x x.*y y.*y x y ones(size(x)) ];
% Build scatter matrix
S = D'*D;
% Build 6x6 constraint matrix
C(6,6) = 0; C(1,3) = -2; C(2,2) = 1; C(3,1) = -2;
% Solve eigensystem
[gevec,geval] = eig(S,C);
% Find the negative eigenvalue
[NegR1,NegC] = find(geval < 0 & isinf(geval));
% Extract eigenvector corresponding to positive eigenvalue
A = gevec(:,NegC);
% unnormalize
a(1)=A(1)*sy*sy;
a(2)=A(2)*sx*sy;
a(3)=A(3)*sx*sx;
a(4)=-2*A(1)*sy*sy*mx -A(2)*sx*sy*my + A(4)*sx*sy*sy;
a(5)=-A(2)*sx*sy*mx -2*A(3)*sx*sx*my + A(5)*sx*sx*sy;
a(6)=A(1)*sy*sy*mx*mx + A(2)*sx*sy*mx*my + A(3)*sx*sx*my*my-A(4)*sx*sy*sy*mx -A(5)*sx*sx*sy*my+ A(6)*sx*sx*sy*sy;
a=a';
% get ellipse orientation
theta = atan2(a(2),a(1)-a(3))/2;
% get scaled major/minor axes
ct = cos(theta); st = sin(theta); ap = a(1)*ct*ct + a(2)*ct*st + a(3)*st*st; cp = a(1)*st*st -a(2)*ct*st + a(3)*ct*ct;
% get translations
T = [[a(1) a(2)/2]' [a(2)/2 a(3)]']; t = -inv(2*T)*[a(4) a(5)]'; cx = t(1); cy = t(2);
% get scale factor
val = t'*T*t; scale = 1 / (val-a(6));
% get major/minor axis radii
r1 = 1/sqrt(scale*ap);
r2 = 1/sqrt(scale*cp);
v = [r1 r2 cx cy theta]';
%判定长轴、短轴是否与r1、r2对应
if r1
通过一些离散点,如何拟合成一个完整的椭圆曲线的matlab程序?
matlab中,怎么根据一系列三维离散的点拟合出一个球面啊,并在球面上显示这些离散的点,进行数据分析
给出椭圆参数方程怎么用matlab画图
matlab求解直线与椭圆交点方程出的问题
matlab,知道一些点的三维坐标,拟合平面,平面方程怎么求?
matlab中离散数拟合出函数方程
已知椭圆方程,如何求椭圆的一个焦点到椭圆上的点的距离?
椭圆外到椭圆的最短距离该怎么求?(点坐标,椭圆方程已知)
用MATLAB画孤立点拟合出的曲线切线
如何根据一些离散点,用matlab拟合函数,函数未知,
Matlab中如何拟合椭圆
已知椭圆方程和椭圆上一点Q(a,b) 求过点Q的椭圆切线方程