您正在寻找椭圆体最小包围体积 http://www.mathworks.com/matlabcentral/fileexchange/9542,或者在二维情况下,最小面积。这个优化问题是凸的并且可以有效地解决。查看我包含的链接中的 MATLAB 代码 - 实现很简单,不需要比矩阵求逆更复杂的东西。
对数学感兴趣的任何人都应该阅读这个文件 http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.116.7691.
另外,绘制椭圆也很简单 - 这可以找到here http://www.mathworks.com/matlabcentral/fileexchange/13844,但您需要一个 MATLAB 特定的函数来生成椭圆上的点。
But由于该算法以矩阵形式返回椭圆方程,
您可以使用此代码来查看如何将方程转换为规范形式,
using 奇异值分解 (SVD) http://en.wikipedia.org/wiki/Singular_value_decomposition。然后使用以下命令很容易绘制椭圆规范形式 http://en.wikipedia.org/wiki/Ellipse#Canonical_form.
Here's the result of the MATLAB code on a set of 10 random 2D points (blue).
其他方法如PCA http://en.wikipedia.org/wiki/Principal_component_analysis不保证分解得到的椭圆 (eigen http://en.wikipedia.org/wiki/Eigendecomposition_of_a_matrix/奇异值)将是最小边界椭圆,因为椭圆之外的点是方差的指示。
EDIT:
因此,如果有人阅读了该文档,有两种方法可以在 2D 中实现此目的:这是最佳算法的伪代码 - 次优算法在文档中进行了清楚的解释:
最优算法:
Input: A 2x10 matrix P storing 10 2D points
and tolerance = tolerance for error.
Output: The equation of the ellipse in the matrix form,
i.e. a 2x2 matrix A and a 2x1 vector C representing
the center of the ellipse.
// Dimension of the points
d = 2;
// Number of points
N = 10;
// Add a row of 1s to the 2xN matrix P - so Q is 3xN now.
Q = [P;ones(1,N)]
// Initialize
count = 1;
err = 1;
//u is an Nx1 vector where each element is 1/N
u = (1/N) * ones(N,1)
// Khachiyan Algorithm
while err > tolerance
{
// Matrix multiplication:
// diag(u) : if u is a vector, places the elements of u
// in the diagonal of an NxN matrix of zeros
X = Q*diag(u)*Q'; // Q' - transpose of Q
// inv(X) returns the matrix inverse of X
// diag(M) when M is a matrix returns the diagonal vector of M
M = diag(Q' * inv(X) * Q); // Q' - transpose of Q
// Find the value and location of the maximum element in the vector M
maximum = max(M);
j = find_maximum_value_location(M);
// Calculate the step size for the ascent
step_size = (maximum - d -1)/((d+1)*(maximum-1));
// Calculate the new_u:
// Take the vector u, and multiply all the elements in it by (1-step_size)
new_u = (1 - step_size)*u ;
// Increment the jth element of new_u by step_size
new_u(j) = new_u(j) + step_size;
// Store the error by taking finding the square root of the SSD
// between new_u and u
// The SSD or sum-of-square-differences, takes two vectors
// of the same size, creates a new vector by finding the
// difference between corresponding elements, squaring
// each difference and adding them all together.
// So if the vectors were: a = [1 2 3] and b = [5 4 6], then:
// SSD = (1-5)^2 + (2-4)^2 + (3-6)^2;
// And the norm(a-b) = sqrt(SSD);
err = norm(new_u - u);
// Increment count and replace u
count = count + 1;
u = new_u;
}
// Put the elements of the vector u into the diagonal of a matrix
// U with the rest of the elements as 0
U = diag(u);
// Compute the A-matrix
A = (1/d) * inv(P * U * P' - (P * u)*(P*u)' );
// And the center,
c = P * u;