MATLAB-在一个图形中绘制多个曲面拟合
我有3套3D坐标,每一套都安装了飞机。现在,我想在一个图中绘制所有数据点和3个平面 到目前为止,我有以下功能:MATLAB-在一个图形中绘制多个曲面拟合,matlab,plot,colors,3d,surface,Matlab,Plot,Colors,3d,Surface,我有3套3D坐标,每一套都安装了飞机。现在,我想在一个图中绘制所有数据点和3个平面 到目前为止,我有以下功能: function [fitresult, gof] = create_fit(xx, yy, zz, grp) [xData, yData, zData] = prepareSurfaceData( xx, yy, zz ); ft = fittype( 'poly11' ); opts = fitoptions( ft ); opts.Lower = [-Inf -Inf -In
function [fitresult, gof] = create_fit(xx, yy, zz, grp)
[xData, yData, zData] = prepareSurfaceData( xx, yy, zz );
ft = fittype( 'poly11' );
opts = fitoptions( ft );
opts.Lower = [-Inf -Inf -Inf];
opts.Upper = [Inf Inf Inf];
hold on;
% figure( 'Name', 'fit1' );
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );
h = plot( fitresult, [xData, yData], zData);
if grp == 1
colormap(hot);
elseif grp == 2
colormap(cool);
else
colormap(grey);
end
legend( h, 'fit1', 'zz vs. xx, yy', 'Location', 'NorthEast' );
xlabel( 'xx' );
ylabel( 'yy' );
zlabel( 'zz' );
grid on
function [fitresult, gof, h] = create_fit(xx, yy, zz, color)
[xData, yData, zData] = prepareSurfaceData( xx, yy, zz );
ft = fittype( 'poly11' );
opts = fitoptions( ft );
opts.Lower = [-Inf -Inf -Inf];
opts.Upper = [Inf Inf Inf];
hold on;
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );
h = plot( fitresult, [xData, yData], zData);
set(h(1), 'FaceColor', color);
set(h(2), 'MarkerFaceColor', color, 'MarkerEdgeColor', 'k');
% Define sample data
N = 20;
x = rand(1,N);
y = rand(1,N);
z = rand(1,N);
% Call the function, specify color
[f1, gof1, h1] = create_fit(x, y, z, 'r');
[f2, gof2, h2] = create_fit(x, y.^10, z, 'g');
[f3, gof3, h3] = create_fit(x.^10, y, z, 'b');
% Figure adjustments
xlabel( 'xx' );
ylabel( 'yy' );
zlabel( 'zz' );
view(3)
grid on
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
zlim([min(z) max(z)]);
但是,这有两个问题:
轴([-0.04 0.04-0.04 0.04-0.04-0.04 0.04-1 1])修复问题代码>,但它是硬编码的,看起来还是有点不对劲。
colormap
命令似乎只有在第一次调用时才起作用。因此,所有的飞机都是蓝色的。如何对每个平面和适合该平面的点进行不同的着色plot
的输出是一个两元素图形对象,因此您必须分别调用h(1)
和h(2)
来设置平面和数据点的属性
以下是该函数的代码:
function [fitresult, gof] = create_fit(xx, yy, zz, grp)
[xData, yData, zData] = prepareSurfaceData( xx, yy, zz );
ft = fittype( 'poly11' );
opts = fitoptions( ft );
opts.Lower = [-Inf -Inf -Inf];
opts.Upper = [Inf Inf Inf];
hold on;
% figure( 'Name', 'fit1' );
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );
h = plot( fitresult, [xData, yData], zData);
if grp == 1
colormap(hot);
elseif grp == 2
colormap(cool);
else
colormap(grey);
end
legend( h, 'fit1', 'zz vs. xx, yy', 'Location', 'NorthEast' );
xlabel( 'xx' );
ylabel( 'yy' );
zlabel( 'zz' );
grid on
function [fitresult, gof, h] = create_fit(xx, yy, zz, color)
[xData, yData, zData] = prepareSurfaceData( xx, yy, zz );
ft = fittype( 'poly11' );
opts = fitoptions( ft );
opts.Lower = [-Inf -Inf -Inf];
opts.Upper = [Inf Inf Inf];
hold on;
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );
h = plot( fitresult, [xData, yData], zData);
set(h(1), 'FaceColor', color);
set(h(2), 'MarkerFaceColor', color, 'MarkerEdgeColor', 'k');
% Define sample data
N = 20;
x = rand(1,N);
y = rand(1,N);
z = rand(1,N);
% Call the function, specify color
[f1, gof1, h1] = create_fit(x, y, z, 'r');
[f2, gof2, h2] = create_fit(x, y.^10, z, 'g');
[f3, gof3, h3] = create_fit(x.^10, y, z, 'b');
% Figure adjustments
xlabel( 'xx' );
ylabel( 'yy' );
zlabel( 'zz' );
view(3)
grid on
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
zlim([min(z) max(z)]);
下面是调用函数的示例脚本:
function [fitresult, gof] = create_fit(xx, yy, zz, grp)
[xData, yData, zData] = prepareSurfaceData( xx, yy, zz );
ft = fittype( 'poly11' );
opts = fitoptions( ft );
opts.Lower = [-Inf -Inf -Inf];
opts.Upper = [Inf Inf Inf];
hold on;
% figure( 'Name', 'fit1' );
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );
h = plot( fitresult, [xData, yData], zData);
if grp == 1
colormap(hot);
elseif grp == 2
colormap(cool);
else
colormap(grey);
end
legend( h, 'fit1', 'zz vs. xx, yy', 'Location', 'NorthEast' );
xlabel( 'xx' );
ylabel( 'yy' );
zlabel( 'zz' );
grid on
function [fitresult, gof, h] = create_fit(xx, yy, zz, color)
[xData, yData, zData] = prepareSurfaceData( xx, yy, zz );
ft = fittype( 'poly11' );
opts = fitoptions( ft );
opts.Lower = [-Inf -Inf -Inf];
opts.Upper = [Inf Inf Inf];
hold on;
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );
h = plot( fitresult, [xData, yData], zData);
set(h(1), 'FaceColor', color);
set(h(2), 'MarkerFaceColor', color, 'MarkerEdgeColor', 'k');
% Define sample data
N = 20;
x = rand(1,N);
y = rand(1,N);
z = rand(1,N);
% Call the function, specify color
[f1, gof1, h1] = create_fit(x, y, z, 'r');
[f2, gof2, h2] = create_fit(x, y.^10, z, 'g');
[f3, gof3, h3] = create_fit(x.^10, y, z, 'b');
% Figure adjustments
xlabel( 'xx' );
ylabel( 'yy' );
zlabel( 'zz' );
view(3)
grid on
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
zlim([min(z) max(z)]);
结果是:
最小-最大技巧奏效了。但是,“FaceColor”出现以下错误:
参数“FaceColor”与解析器的任何有效参数都不匹配。
另外,是否有一种方法可以使每组点具有不同的颜色?这将有助于可视化拟合的效果,并发现异常值。好的,抱歉,但由于我们没有您拥有的所有信息(值xx
,grp
,…),我无法真正测试我的答案。用'color'
替换'Facecolor'
怎么样?xx、yy、zz
只是相同长度的1D双倍数组grp
可以是1、2或3,具体取决于平面。尝试“color”或“color”会出现相同的错误:/