【Matlab学习⼿记】⼆次多项式曲⾯拟合
⼆次多项式曲⾯公式
总共有6个系数。
绘制曲⾯图形时,⼀般给定x和y的取值(⼀维数组),然后对x和y⽹格化成⼆维数组X和Y,将X和Y代⼊公式,即可得到曲⾯的数值,最后⽤surf函数显⽰。
实例
褴褛的意思
给定⼀个⼆次多项式模型,然后成图
x = 0.1 : 0.1 : 5;
y = 0.1 : 0.1 : 5;
[X, Y] = meshgrid(x, y);
Z = X.^2 + Y.^2 + X.*Y + X + Y + 1;
surf(x, y, Z);
colormap hot
shading interp
添加噪声,随机选择 100 个点⽤于拟合
书香少年
rng('default');
Zn = awgn(Z, 30, 'measured');
xfit = randi(50, 100, 1) / 10;
yfit = randi(50, 100, 1) / 10;
zfit = zeros(100, 1);
for i = 1 : 100
zfit(i) = Zn(xfit(i) * 10, yfit(i) * 10);
end
surf(x, y, Zfit);
家庭屏风colormap hot
shading interp
scatter3(xfit, yfit, zfit, 50, 'MarkerFaceColor', [0 0 0]);
⽤lsqcurvefit函数拟合:拟合结果精度⾼
func = @(var,x) var(1)*x(:,1).^2 + var(2)*x(:,2).^2 + var(3)*x(:,1).*x(:,2) + var(4)*x(:,1) + var(5)*x(:,2) + var(6);
a = lsqcurvefit(func,ones(1, 6),[xfit, yfit],zfit);
Zfit = a(1) * X.^2 + a(2) * Y.^2 + a(3) * X.*Y + a(4) * X + a(5) * Y + a(6);
ya = func(a,[xfit, yfit]);
surf(x, y, Zfit);
colormap hot
shading interp
hold on
surf(x, y, Zfit);
shading interp
scatter3(xfit, yfit, ya, 50, 'MarkerFaceColor', [0 0 1]);
hold off
完整程序
clear;clc;
x = 0.1 : 0.1 : 5;
y = 0.1 : 0.1 : 5;
[X, Y] = meshgrid(x, y);
Z = X.^2 + Y.^2 + X.*Y + X + Y + 1;
rng('default');
Zn = awgn(Z, 30, 'measured');
xfit = randi(50, 100, 1) / 10;
yfit = randi(50, 100, 1) / 10;
zfit = zeros(100, 1);
for i = 1 : 100
zfit(i) = Zn(xfit(i) * 10, yfit(i) * 10);
end
func = @(var,x) var(1)*x(:,1).^2 + var(2)*x(:,2).^2 + var(3)*x(:,1).*x(:,2) + var(4)*x(:,1) + var(5)*x(:,2) + var(6);
a = lsqcurvefit(func,ones(1, 6),[xfit, yfit],zfit);
Zfit = a(1) * X.^2 + a(2) * Y.^2 + a(3) * X.*Y + a(4) * X + a(5) * Y + a(6);
日本研修生ya = func(a,[xfit, yfit]);
surf(x, y, Zn);
回馈shading interp
freezeColors
hold on
surf(x, y, Zfit);
colormap hot
shading interp
scatter3(xfit, yfit, zfit, 50, 'MarkerFaceColor', [0 0 0]);
scatter3(xfit, yfit, ya, 50, 'MarkerFaceColor', [0 0 1]);
surf(x, y, Z - Zfit);
shading interp
hold off
legend('Model','Fits','Fit','fit','Error');
使⽤cftool⼯具拟合
附录:freezeColors函数可从下载
function freezeColors(varargin)
% freezeColors Lock colors of plot, enabling multiple colormaps per figure. (v2.3) %
% Problem: There is only one colormap per figure. This function provides
% an easy solution when plots using different colomaps are desired
% in the same figure.
%
前台文员工作内容% freezeColors freezes the colors of graphics objects in the current axis so
% that subquent changes to the colormap (or caxis) will not change the
% colors of the objects. freezeColors works on any graphics object
% with CData in indexed-color mode: surfaces, images, scattergroups,
% bargroups, patches, etc. It works by converting CData to true-color rgb
% bad on the colormap active at the time freezeColors is called.
%
% The original indexed color data is saved, and can be restored using
% unfreezeColors, making the plot once again subject to the colormap and
% unfreezeColors, making the plot once again subject to the colormap and
% caxis.
%
%
% Usage:
% freezeColors applies to all objects in current axis (gca),
% freezeColors(axh) same, but works on axis axh.
%
% Example:
% subplot(2,1,1); imagesc(X); colormap hot; freezeColors
% subplot(2,1,2); imagesc(Y); colormap hsv;
%
猪心的做法大全% Note: colorbars must also be frozen. Due to Matlab 'improvements' this can % no longer be done with freezeColors. Instead, plea
% u the function CBFREEZE by Carlos Adrian Vargas Aguilera
% that can be downloaded from the MATLAB File Exchange
% (/matlabcentral/fileexchange/24371)
%
% h=colorbar; cbfreeze(h), or simply cbfreeze(colorbar)
%
% For additional examples, e test/test_main.m
%
% Side effect on render mode: freezeColors does not work with the painters
% renderer, becau Matlab doesn't support rgb color data in
% painters mode. If the current renderer is painters, freezeColors
% changes it to zbuffer. This may have unexpected effects on other aspects % of your plots.
%
% See also unfreezeColors, freezeColors_pub.html, cbfreeze.
%
%
% John Ivern (ivern@nsi.edu) 3/23/05
%
% Changes:
% JRI (ivern@nsi.edu) 4/19/06 Correctly handles scaled integer cdata
% JRI 9/1/06 should now handle all objects with cdata: images, surfaces,
% scatterplots. (v 2.1)
% JRI 11/11/06 Prerves NaN colors. Hidden option (v 2.2, not uploaded)
word对不齐
% JRI 3/17/07 Prerve caxis after freezing--maintains colorbar scale (v 2.3)
% JRI 4/12/07 Check for painters mode as Matlab doesn't support rgb in it.
% JRI 4/9/08 Fix prerving caxis for objects within hggroups (e.g. contourf)
% JRI 4/7/10 Change documentation for colorbars
% Hidden option for NaN colors:
% Missing data are often reprented by NaN in the indexed color
% data, which renders transparently. This transparency will be prerved
% when freezing colors. If instead you wish such gaps to be filled with
% a real color, add 'nancolor',[r g b] to the end of the arguments. E.g.
% freezeColors('nancolor',[r g b]) or freezeColors(axh,'nancolor',[r g b]),
% where [r g b] is a color vector. This works on images & pcolor, but not on
% surfaces.
% Thanks to Fabiano Busdraghi and Jody Klymak for the suggestions. Bugfixes % attributed in the code.
% Free for all us, but plea retain the following:
% Original Author:
% John Ivern, 2005-10
% john_ivern@post.harvard.edu
appdatacode = 'JRI__freezeColorsData';
[h, nancolor] = checkArgs(varargin);
%gather all children with scaled or indexed CData
cdatah = getCDataHandles(h);