简体   繁体   English

使用MATLAB将f(x,y,z)= 0拟合到一组3D点

[英]Fit f(x,y,z)=0 to a set of 3D points using MATLAB

The statement is the following: 该语句如下:

Given a 3D set of points (x,y,z), fit a surface defined by a certain number of parameters IMPLICITLY 给定3D点(x,y,z)集,隐式拟合由一定数量的参数定义的曲面

I'm definitely no expert in programming, but I need to get this done in one way or another. 我绝对不是编程专家,但是我需要以一种或另一种方式来完成。 I've considered other programs, such as OriginPro, which can solve this problem pretty easily, but I want to have it done in MATLAB. 我考虑过其他程序,例如OriginPro,可以很轻松地解决此问题,但我想在MATLAB中完成。

The surface is defined by: 该表面由以下方式定义:

A*x^2+B*y^2+C*z^2+D*x+E*y+F*z+G*xy+H*xz+I*yz+J=0

Considering that the Curve Fitting Toolbox can only fit explicit functions, what would you guys suggest? 考虑到“曲线拟合工具箱”只能拟合显式函数,你们会建议些什么?

IMPORTANT REMARK: I'm not asking for a solution, just advice on how to proceed 重要提示:我不是要寻求解决方案,而只是建议如何进行

This can be chalked up to solving a linear system of equations where each point forms a constraint or equation in your system. 这可以归结为求解线性方程组,其中每个点在系统中构成约束或方程。 You would thus find the right set of coefficients in your surface equation that satisfies all points. 因此,您将在满足所有点的曲面方程中找到正确的系数集。 Using the equation in your question as is, one would find the null space of the linear system that satisfies the surface equation. 照原样使用问题中的方程,将找到满足表面方程的线性系统的零空间 What you'll have to do is given a set of m points that contain x , y and z coordinates, we can reformulate the above equation as a matrix-vector multiplication with the first argument technically being a matrix of one row and the vector being the coefficients that fit your plane. 给定一组包含xyz坐标的m个点,我们可以将上面的方程重新表示为矩阵向量乘法,第一个参数从技术上讲是一行矩阵,向量是适合您飞机的系数。 This is important before we proceed to the null space part of this problem. 在进行此问题的零空间部分之前,这一点很重要。

In particular, you can agree with me that we can represent the above in the following matrix-vector multiplication: 特别是,您可以同意我可以在以下矩阵向量乘法中表示上述内容:

[x^2 y^2 z^2 x y z xy xz yz 1][A]    =    [0]
                              [B]
                              [C]
                              [D]
                              [E]
                              [F]
                              [G]
                              [H]
                              [I]
                              [J]

Our objective is to find the coefficients A, B, ..., J that would satisfy the constraint above. 我们的目标是找到满足上述约束的系数A, B, ..., J Now moving onto the more general case, since you have m points, we can build our linear system and thus a matrix of coefficients on the left side of this expression: 现在转到更一般的情况,因为您有m个点,所以我们可以构建我们的线性系统,因此可以在该表达式的左侧建立系数矩阵:

[x_1^2 y_1^2 z_1^2 x_1 y_1 z_1 x_1*y_1 x_1*z_1 y_1*z_1 1][A]    =    [0]    
[x_2^2 y_2^2 z_2^2 x_2 y_2 z_2 x_2*y_2 x_2*z_2 y_2*z_2 1][B]    =    [0]
[x_3^2 y_3^2 z_3^2 x_3 y_3 z_3 x_3*y_3 x_3*z_3 y_3*z_3 1][C]    =    [0]
                    ...                                  [D]    =    [0]
                    ...                                  [E]    =    [0]
                    ...                                  [F]    =    [0]
                    ...                                  [G]    =    [0]
                    ...                                  [H]    =    [0]
                    ...                                  [I]    =    [0]
[x_m^2 y_m^2 z_m^2 x_m y_m z_m x_m*y_m x_m*z_m y_m*z_m 1][J]    =    [0]

We now build this linear system, and solve to find our coefficients. 现在我们建立这个线性系统,并求解以找到我们的系数。 The trick is to build the matrix that you see on the left hand side of this linear system, which I will call M . 诀窍是建立您在线性系统左侧看到的矩阵,我将其称为M Each row is such that you create [x_i^2 y_i^2 z_i^2 x_i y_i z_i x_i*y_i x_i*z_i y_i*z_i 1] with x_i , y_i and z_i being the i th (x,y,z) coordinate in your dataset. 每行都是这样的:您创建了[x_i^2 y_i^2 z_i^2 x_i y_i z_i x_i*y_i x_i*z_i y_i*z_i 1]其中x_iy_iz_i是第i (x,y,z)坐标您的数据集。

Once you build this, you would thus find the null space of this system. 构建此文件后,您将因此找到该系统的空空间。 There are many methods to do this in MATLAB. 在MATLAB中有很多方法可以做到这一点。 One way is to simply use the null function on the matrix you build above and it will return to you a matrix where each column is a potential solution to the surface you are fitting above. 一种方法是在上面构建的矩阵上简单使用null函数,它将返回一个矩阵,其中每一列都是您要在上面拟合的表面的潜在解决方案。 That is, each column directly corresponds to the coefficients A , B , ..., J that would fit your data to the surface. 也就是说,每一列都直接对应于将您的数据拟合到表面的系数AB ,..., J You can also try using the singular value decomposition or QR decomposition if you like, but the null function is a good place to start as it uses singular value decomposition already. 如果愿意,您也可以尝试使用奇异值分解QR分解 ,但是null函数是一个很好的起点,因为它已经使用了奇异值分解。


I would like to point out that the above will only work if you provide a matrix that is not full rank . 我想指出的是,上述内容只有在您提供的矩阵不是等级的情况下才有效 To simplify things, this can happen if the number of points you have is less than the number of parameters you have. 为简化起见,如果您拥有的点数少于您拥有的参数数,则会发生这种情况。 Therefore, this method would only work if you had up to 9 points in your data. 因此,仅当您的数据中最多有9个点时,此方法才有效。 If you have exactly 9, then this method will work very well. 如果您正好有9,则此方法将非常有效。 If you have less than 9, then there will be more potential solutions as the number of degrees of freedom increases. 如果少于9,则随着自由度数量的增加,将有更多潜在的解决方案。 Specifically, you will have 10 - m possible solutions and any of those solutions is valid. 具体来说,您将有10 - m可能的解决方案,并且这些解决方案中的任何一个都是有效的。 If you have more than 10 points and they are all unique , this would be considered a full rank matrix and thus the only solution to the null space is the trivial one with the coefficients being all set to 0. 如果您拥有10个以上的点并且它们都是唯一的 ,则将其视为满秩矩阵,因此,零位空间的唯一解决方案是将所有系数都设置为0的琐碎空间。

In order to escape the possibility of the null space being all 0, or the possibility of the null space providing more than one solution, you probably just want one solution, and you most likely have 10 or more possible points that you want to fit your data with. 为了逃避零空间全为0的可能性,或者避免零空间提供多个解决方案的可能性,您可能只需要一个解决方案,并且您很可能希望拥有10个或更多的点来满足您的需求。数据一起。 An alternative method that I can provide is simply an extension of the above but we don't need to find the null space. 我可以提供的替代方法只是上述内容的扩展,但是我们不需要找到空空间。 Specifically, you relax one of the coefficients, say J and you can set that to any value you wish. 具体来说,您可以放宽其中一个系数,例如J然后可以将其设置为所需的任何值。 For example, set it to J = 1 . 例如,将其设置为J = 1 Therefore, the system of equations now changes where J disappears from the mix and it now appears on the right side of the system: 因此,方程组现在发生变化,其中J从混合中消失,并且现在出现在系统的右侧:

[x_1^2 y_1^2 z_1^2 x_1 y_1 z_1 x_1*y_1 x_1*z_1 y_1*z_1][A]    =    [-1]    
[x_2^2 y_2^2 z_2^2 x_2 y_2 z_2 x_2*y_2 x_2*z_2 y_2*z_2][B]    =    [-1]
[x_3^2 y_3^2 z_3^2 x_3 y_3 z_3 x_3*y_3 x_3*z_3 y_3*z_3][C]    =    [-1]
                    ...                                [D]    =    [-1]
                    ...                                [E]    =    [-1]
                    ...                                [F]    =    [-1]
                    ...                                [G]    =    [-1]
                    ...                                [H]    =    [-1]
[x_m^2 y_m^2 z_m^2 x_m y_m z_m x_m*y_m x_m*z_m y_m*z_m][I]    =    [-1]

You can thus find the parameters A , B , ..., I using linear least squares where the solution can be solved using the pseudoinverse . 因此,您可以使用线性最小二乘法找到参数AB ,..., I ,其中可以使用拟逆来求解。 The benefit with this approach is that because the matrix is full rank, there is one and only one solution, thus being unique . 这种方法的好处是,因为矩阵是满秩的, 所以只有一个解决方案,因此是唯一的 Additionally, this formulation is nice because if there is an exact solution to the linear system, solving with the pseudoinverse will provide the exact solution. 另外,这种表示法很好,因为如果线性系统有精确的解决方案,则用伪逆解将提供精确的解决方案。 If there is no exact solution to the system, meaning that not all constraints are satisfied, the solution provided is one that minimizes the least squared error between the data and the parameters that were fit with that data. 如果没有针对该系统的精确解决方案,这意味着并非所有约束都得到满足,则提供的解决方案将使数据与适合该数据的参数之间的最小平方误差最小化。

MATLAB already has an awesome utility to solve a system through linear least squares - in fact, the core functionality of MATLAB is to solve linear algebra problems (if you didn't know that already). MATLAB已经有了一个很棒的实用程序,可以通过线性最小二乘法求解系统-实际上,MATLAB的核心功能是解决线性代数问题(如果您还不知道的话)。 You can use matrix left division to solve the problem. 您可以使用矩阵左除法解决问题。 Simply put, given that you built your matrix of coefficients above after you introduce the relaxation of J also being called M , the solution to the problem is simply coeff = M\\(-ones(m,1)); 简而言之,假设您在引入J的松弛(也称为M之后在上面建立了系数矩阵,则问题的解决方案就是coeff = M\\(-ones(m,1)); with m being the number of points and coeff being the coefficients for the surface equation that fit your points. m是点的数量, coeff是适合您的点的表面方程的系数。 The ones statement in the code creates a column vector of ones that are negative that has m elements. 代码中的“ ones语句创建一个包含m元素的,为负数的列向量。

Using the least squares approach has a more stable and unique solution as you are specifically constraining one of the coefficients, J , to be 1. Using the null space approach only works if you have less points than you do parameters and will possibly give you more than one solution so long as the coefficients span the null space. 使用最小二乘法具有更稳定和唯一的解决方案,因为您特别将一个系数J约束为1。使用零空间方法仅在点数少于参数的情况下有效,并且可能会为您提供更多的参数。只要系数跨越零空间,就可以得到一种解决方案。 Specifically, you will get 10 - m solutions and they are all equally good at fitting your data. 具体来说,您将获得10 - m解决方案,它们同样都非常适合拟合您的数据。

I hope this is enough to get you started and good luck! 我希望这足以让您入门并祝您好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用MATLAB将2D(x,y)坐标转换为3D(x,y,z)坐标? - How to convert 2D(x,y) coordinates to 3D(x,y,z) coordinates using MATLAB? 如何基于f(x,y,z)= 0之类的函数在Matlab中绘制3D图形? - How to plot a 3D figure in matlab based on a function like f(x,y,z)=0? 基于x,y,z数据的matlab 3d图形 - matlab 3d graph based on x,y,z data 如何分别对X * Y * Z(3D)矩阵matlab进行子图绘制? - How to separately subplot a X*Y*Z (3D) matrix matlab? 需要使用MATLAB(X,Y,Z,V)进行3D绘图的帮助 - Need help in 3D plot using MATLAB (X,Y,Z,V) 如何使用Matlab拟合表面((x,y,z)矩阵)? - How to fit a surface ((x,y,z) matrix) using Matlab? Matlab的z = f(x,y)散点图,其中z的值位于点的顶部 - Matlab scatter plot of z=f(x,y) with values of z on top of the points 如何使用Matlab在3D大数据散点图中用不同的颜色标记一个点(我有x,y,z)? - How to mark a point (I have x,y,z for it) in a 3D big data scatter plot with different color using matlab? matlab:在 3d 图中绘制 x、y 和 z 轴上的 2d 线 - matlab: Graph 2d lines on x,y and z axis in a 3d plot 如何将 3D 坐标 (x,y,z) 转换为 MATLAB 中的.STL 文件? - How to convert the 3D Coordinates (x,y,z) into .STL file in MATLAB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM