3D曲线拟合 [英] 3D curvefitting
问题描述
我有a,b
个点的离散规则网格及其对应的c
值,并对其进行进一步插值以获得平滑曲线.现在,从插值数据中,我还想创建一个用于曲线拟合的多项式方程.如何在多项式中拟合3D图?
I have discrete regular grid of a,b
points and their corresponding c
values and I interpolate it further to get a smooth curve. Now from interpolation data, I further want to create a polynomial equation for curve fitting. How to fit 3D plot in polynomial?
我尝试在MATLAB中执行此操作.我使用了MATLAB(r2010a)中的Surface拟合工具箱来对3维数据进行曲线拟合.但是,在MATLAB/MAPLE或任何其他软件中,如何找到最适合数据的公式.有什么建议吗?另外最有用的是一些实际的代码示例,例如PDF文件,网络等.
I try to do this in MATLAB. I used Surface fitting toolbox in MATLAB (r2010a) to curve fit 3-dimensional data. But, how does one find a formula that fits a set of data to the best advantage in MATLAB/MAPLE or any other software. Any advice? Also most useful would be some real code examples to look at, PDF files, on the web etc.
这只是我数据的一小部分.
This is just a small portion of my data.
a = [ 0.001 .. 0.011];
b = [1, .. 10];
c = [ -.304860225, .. .379710865];
谢谢.
推荐答案
要将曲线拟合到一组点上,我们可以使用普通最小二乘回归. MathWorks提供了一个解决方案页面来描述该过程.
To fit a curve onto a set of points, we can use ordinary least-squares regression. There is a solution page by MathWorks describing the process.
作为一个例子,让我们从一些随机数据开始:
As an example, let's start with some random data:
% some 3d points
data = mvnrnd([0 0 0], [1 -0.5 0.8; -0.5 1.1 0; 0.8 0 1], 50);
如 @BasSwinckels 所示,通过构造所需的 mldivide
或 pinv
到Ax=b
:
As @BasSwinckels showed, by constructing the desired design matrix, you can use mldivide
or pinv
to solve the overdetermined system expressed as Ax=b
:
% best-fit plane
C = [data(:,1) data(:,2) ones(size(data,1),1)] \ data(:,3); % coefficients
% evaluate it on a regular grid covering the domain of the data
[xx,yy] = meshgrid(-3:.5:3, -3:.5:3);
zz = C(1)*xx + C(2)*yy + C(3);
% or expressed using matrix/vector product
%zz = reshape([xx(:) yy(:) ones(numel(xx),1)] * C, size(xx));
接下来,我们将结果可视化:
Next we visualize the result:
% plot points and surface
figure('Renderer','opengl')
line(data(:,1), data(:,2), data(:,3), 'LineStyle','none', ...
'Marker','.', 'MarkerSize',25, 'Color','r')
surface(xx, yy, zz, ...
'FaceColor','interp', 'EdgeColor','b', 'FaceAlpha',0.2)
grid on; axis tight equal;
view(9,9);
xlabel x; ylabel y; zlabel z;
colormap(cool(64))
如前所述,我们可以通过向自变量矩阵(Ax=b
中的A
)添加更多项来获得高阶多项式拟合.
As was mentioned, we can get higher-order polynomial fitting by adding more terms to the independent variables matrix (the A
in Ax=b
).
说我们想用一个常数,线性,相互作用和平方项(1,x,y,xy,x ^ 2,y ^ 2)拟合二次模型.我们可以手动执行此操作:
Say we want to fit a quadratic model with constant, linear, interaction, and squared terms (1, x, y, xy, x^2, y^2). We can do this manually:
% best-fit quadratic curve
C = [ones(50,1) data(:,1:2) prod(data(:,1:2),2) data(:,1:2).^2] \ data(:,3);
zz = [ones(numel(xx),1) xx(:) yy(:) xx(:).*yy(:) xx(:).^2 yy(:).^2] * C;
zz = reshape(zz, size(xx));
统计信息"工具箱中还有一个帮助器功能 x2fx
,帮助建立几个模型订单的设计矩阵:
There is also a helper function x2fx
in the Statistics Toolbox that helps in building the design matrix for a couple of model orders:
C = x2fx(data(:,1:2), 'quadratic') \ data(:,3);
zz = x2fx([xx(:) yy(:)], 'quadratic') * C;
zz = reshape(zz, size(xx));
最后,在文件交换上,有一个出色的功能 polyfitn
John D'Errico,可让您指定所涉及的各种多项式阶数和项:
Finally there is an excellent function polyfitn
on the File Exchange by John D'Errico that allows you to specify all kinds of polynomial orders and terms involved:
model = polyfitn(data(:,1:2), data(:,3), 2);
zz = polyvaln(model, [xx(:) yy(:)]);
zz = reshape(zz, size(xx));
这篇关于3D曲线拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!