二次多项式曲面公式

Matlab--二次多项式曲面拟合

总共有6个系数。

绘制曲面图形时,一般给定x和y的取值(一维数组),然后对x和y网格化成二维数组X和Y,将X和Y代入公式,即可得到曲面的数值,最后用surf函数显示。

实例    
  •     给定一个二次多项式模型,然后成图
  1. x = 0.1 : 0.1 : 5;
  2. y = 0.1 : 0.1 : 5;
  3. [X, Y] = meshgrid(x, y);
  4. Z = X.^2 + Y.^2 + X.*Y + X + Y + 1;
  5. surf(x, y, Z);
  6. colormap hot
  7. shading interp

Matlab--二次多项式曲面拟合

  •  添加噪声,随机选择 100 个点用于拟合
  1. rng('default');
  2. Zn = awgn(Z, 30, 'measured');
  3. xfit = randi(50, 100, 1) / 10;
  4. yfit = randi(50, 100, 1) / 10;
  5. zfit = zeros(100, 1);
  6. for i = 1 : 100
  7. zfit(i) = Zn(xfit(i) * 10, yfit(i) * 10);
  8. end
  9. surf(x, y, Zfit);
  10. colormap hot
  11. shading interp
  12. scatter3(xfit, yfit, zfit, 50, 'MarkerFaceColor', [0 0 0]);

Matlab--二次多项式曲面拟合

Matlab--二次多项式曲面拟合

  • 用lsqcurvefit函数拟合:拟合结果精度高
  1. 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);
  2. a = lsqcurvefit(func,ones(1, 6),[xfit, yfit],zfit);
  3. Zfit = a(1) * X.^2 + a(2) * Y.^2 + a(3) * X.*Y + a(4) * X + a(5) * Y + a(6);
  4. ya = func(a,[xfit, yfit]);
  5. surf(x, y, Zfit);
  6. colormap hot
  7. shading interp
  8. hold on
  9. surf(x, y, Zfit);
  10. shading interp
  11. scatter3(xfit, yfit, ya, 50, 'MarkerFaceColor', [0 0 1]);
  12. hold off

Matlab--二次多项式曲面拟合

  •  完整程序
  1. clear;clc;
  2. x = 0.1 : 0.1 : 5;
  3. y = 0.1 : 0.1 : 5;
  4. [X, Y] = meshgrid(x, y);
  5. Z = X.^2 + Y.^2 + X.*Y + X + Y + 1;
  6. rng('default');
  7. Zn = awgn(Z, 30, 'measured');
  8. xfit = randi(50, 100, 1) / 10;
  9. yfit = randi(50, 100, 1) / 10;
  10. zfit = zeros(100, 1);
  11. for i = 1 : 100
  12. zfit(i) = Zn(xfit(i) * 10, yfit(i) * 10);
  13. end
  14. 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);
  15. a = lsqcurvefit(func,ones(1, 6),[xfit, yfit],zfit);
  16. Zfit = a(1) * X.^2 + a(2) * Y.^2 + a(3) * X.*Y + a(4) * X + a(5) * Y + a(6);
  17. ya = func(a,[xfit, yfit]);
  18. surf(x, y, Zn);
  19. shading interp
  20. freezeColors
  21. hold on
  22. surf(x, y, Zfit);
  23. colormap hot
  24. shading interp
  25. scatter3(xfit, yfit, zfit, 50, 'MarkerFaceColor', [0 0 0]);
  26. scatter3(xfit, yfit, ya, 50, 'MarkerFaceColor', [0 0 1]);
  27. surf(x, y, Z - Zfit);
  28. shading interp
  29. hold off
  30. legend('Model','Fits','Fit','fit','Error');

Matlab--二次多项式曲面拟合

  • 使用cftool工具拟合

Matlab--二次多项式曲面拟合

  1. function freezeColors(varargin)
  2. % freezeColors Lock colors of plot, enabling multiple colormaps per figure. (v2.3)
  3. %
  4. % Problem: There is only one colormap per figure. This function provides
  5. % an easy solution when plots using different colomaps are desired
  6. % in the same figure.
  7. %
  8. % freezeColors freezes the colors of graphics objects in the current axis so
  9. % that subsequent changes to the colormap (or caxis) will not change the
  10. % colors of these objects. freezeColors works on any graphics object
  11. % with CData in indexed-color mode: surfaces, images, scattergroups,
  12. % bargroups, patches, etc. It works by converting CData to true-color rgb
  13. % based on the colormap active at the time freezeColors is called.
  14. %
  15. % The original indexed color data is saved, and can be restored using
  16. % unfreezeColors, making the plot once again subject to the colormap and
  17. % caxis.
  18. %
  19. %
  20. % Usage:
  21. % freezeColors applies to all objects in current axis (gca),
  22. % freezeColors(axh) same, but works on axis axh.
  23. %
  24. % Example:
  25. % subplot(2,1,1); imagesc(X); colormap hot; freezeColors
  26. % subplot(2,1,2); imagesc(Y); colormap hsv; freezeColors etc...
  27. %
  28. % Note: colorbars must also be frozen. Due to Matlab 'improvements' this can
  29. % no longer be done with freezeColors. Instead, please
  30. % use the function CBFREEZE by Carlos Adrian Vargas Aguilera
  31. % that can be downloaded from the MATLAB File Exchange
  32. % (http://www.mathworks.com/matlabcentral/fileexchange/24371)
  33. %
  34. % h=colorbar; cbfreeze(h), or simply cbfreeze(colorbar)
  35. %
  36. % For additional examples, see test/test_main.m
  37. %
  38. % Side effect on render mode: freezeColors does not work with the painters
  39. % renderer, because Matlab doesn't support rgb color data in
  40. % painters mode. If the current renderer is painters, freezeColors
  41. % changes it to zbuffer. This may have unexpected effects on other aspects
  42. % of your plots.
  43. %
  44. % See also unfreezeColors, freezeColors_pub.html, cbfreeze.
  45. %
  46. %
  47. % John Iversen ([email protected]) 3/23/05
  48. %
  49. % Changes:
  50. % JRI ([email protected]) 4/19/06 Correctly handles scaled integer cdata
  51. % JRI 9/1/06 should now handle all objects with cdata: images, surfaces,
  52. % scatterplots. (v 2.1)
  53. % JRI 11/11/06 Preserves NaN colors. Hidden option (v 2.2, not uploaded)
  54. % JRI 3/17/07 Preserve caxis after freezing--maintains colorbar scale (v 2.3)
  55. % JRI 4/12/07 Check for painters mode as Matlab doesn't support rgb in it.
  56. % JRI 4/9/08 Fix preserving caxis for objects within hggroups (e.g. contourf)
  57. % JRI 4/7/10 Change documentation for colorbars
  58. % Hidden option for NaN colors:
  59. % Missing data are often represented by NaN in the indexed color
  60. % data, which renders transparently. This transparency will be preserved
  61. % when freezing colors. If instead you wish such gaps to be filled with
  62. % a real color, add 'nancolor',[r g b] to the end of the arguments. E.g.
  63. % freezeColors('nancolor',[r g b]) or freezeColors(axh,'nancolor',[r g b]),
  64. % where [r g b] is a color vector. This works on images & pcolor, but not on
  65. % surfaces.
  66. % Thanks to Fabiano Busdraghi and Jody Klymak for the suggestions. Bugfixes
  67. % attributed in the code.
  68. % Free for all uses, but please retain the following:
  69. % Original Author:
  70. % John Iversen, 2005-10
  71. appdatacode = 'JRI__freezeColorsData';
  72. [h, nancolor] = checkArgs(varargin);
  73. %gather all children with scaled or indexed CData
  74. cdatah = getCDataHandles(h);
  75. %current colormap
  76. cmap = colormap;
  77. nColors = size(cmap,1);
  78. cax = caxis;
  79. % convert object color indexes into colormap to true-color data using
  80. % current colormap
  81. for hh = cdatah',
  82. g = get(hh);
  83. %preserve parent axis clim
  84. parentAx = getParentAxes(hh);
  85. originalClim = get(parentAx, 'clim');
  86. % Note: Special handling of patches: For some reason, setting
  87. % cdata on patches created by bar() yields an error,
  88. % so instead we'll set facevertexcdata instead for patches.
  89. if ~strcmp(g.Type,'patch'),
  90. cdata = g.CData;
  91. else
  92. cdata = g.FaceVertexCData;
  93. end
  94. %get cdata mapping (most objects (except scattergroup) have it)
  95. if isfield(g,'CDataMapping'),
  96. scalemode = g.CDataMapping;
  97. else
  98. scalemode = 'scaled';
  99. end
  100. %save original indexed data for use with unfreezeColors
  101. siz = size(cdata);
  102. setappdata(hh, appdatacode, {cdata scalemode});
  103. %convert cdata to indexes into colormap
  104. if strcmp(scalemode,'scaled'),
  105. %4/19/06 JRI, Accommodate scaled display of integer cdata:
  106. % in MATLAB, uint * double = uint, so must coerce cdata to double
  107. % Thanks to O Yamashita for pointing this need out
  108. idx = ceil( (double(cdata) - cax(1)) / (cax(2)-cax(1)) * nColors);
  109. else %direct mapping
  110. idx = cdata;
  111. %10/8/09 in case direct data is non-int (e.g. image;freezeColors)
  112. % (Floor mimics how matlab converts data into colormap index.)
  113. % Thanks to D Armyr for the catch
  114. idx = floor(idx);
  115. end
  116. %clamp to [1, nColors]
  117. idx(idx<1) = 1;
  118. idx(idx>nColors) = nColors;
  119. %handle nans in idx
  120. nanmask = isnan(idx);
  121. idx(nanmask)=1; %temporarily replace w/ a valid colormap index
  122. %make true-color data--using current colormap
  123. realcolor = zeros(siz);
  124. for i = 1:3,
  125. c = cmap(idx,i);
  126. c = reshape(c,siz);
  127. c(nanmask) = nancolor(i); %restore Nan (or nancolor if specified)
  128. realcolor(:,:,i) = c;
  129. end
  130. %apply new true-color color data
  131. %true-color is not supported in painters renderer, so switch out of that
  132. if strcmp(get(gcf,'renderer'), 'painters'),
  133. set(gcf,'renderer','zbuffer');
  134. end
  135. %replace original CData with true-color data
  136. if ~strcmp(g.Type,'patch'),
  137. set(hh,'CData',realcolor);
  138. else
  139. set(hh,'faceVertexCData',permute(realcolor,[1 3 2]))
  140. end
  141. %restore clim (so colorbar will show correct limits)
  142. if ~isempty(parentAx),
  143. set(parentAx,'clim',originalClim)
  144. end
  145. end %loop on indexed-color objects
  146. % ============================================================================ %
  147. % Local functions
  148. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  149. %% getCDataHandles -- get handles of all descendents with indexed CData
  150. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  151. function hout = getCDataHandles(h)
  152. % getCDataHandles Find all objects with indexed CData
  153. %recursively descend object tree, finding objects with indexed CData
  154. % An exception: don't include children of objects that themselves have CData:
  155. % for example, scattergroups are non-standard hggroups, with CData. Changing
  156. % such a group's CData automatically changes the CData of its children,
  157. % (as well as the children's handles), so there's no need to act on them.
  158. error(nargchk(1,1,nargin,'struct'))
  159. hout = [];
  160. if isempty(h),return;end
  161. ch = get(h,'children');
  162. for hh = ch'
  163. g = get(hh);
  164. if isfield(g,'CData'), %does object have CData?
  165. %is it indexed/scaled?
  166. if ~isempty(g.CData) && isnumeric(g.CData) && size(g.CData,3)==1,
  167. hout = [hout; hh]; %#ok<AGROW> %yes, add to list
  168. end
  169. else %no CData, see if object has any interesting children
  170. hout = [hout; getCDataHandles(hh)]; %#ok<AGROW>
  171. end
  172. end
  173. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  174. %% getParentAxes -- return handle of axes object to which a given object belongs
  175. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  176. function hAx = getParentAxes(h)
  177. % getParentAxes Return enclosing axes of a given object (could be self)
  178. error(nargchk(1,1,nargin,'struct'))
  179. %object itself may be an axis
  180. if strcmp(get(h,'type'),'axes'),
  181. hAx = h;
  182. return
  183. end
  184. parent = get(h,'parent');
  185. if (strcmp(get(parent,'type'), 'axes')),
  186. hAx = parent;
  187. else
  188. hAx = getParentAxes(parent);
  189. end
  190. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  191. %% checkArgs -- Validate input arguments
  192. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  193. function [h, nancolor] = checkArgs(args)
  194. % checkArgs Validate input arguments to freezeColors
  195. nargs = length(args);
  196. error(nargchk(0,3,nargs,'struct'))
  197. %grab handle from first argument if we have an odd number of arguments
  198. if mod(nargs,2),
  199. h = args{1};
  200. if ~ishandle(h),
  201. error('JRI:freezeColors:checkArgs:invalidHandle',...
  202. 'The first argument must be a valid graphics handle (to an axis)')
  203. end
  204. % 4/2010 check if object to be frozen is a colorbar
  205. if strcmp(get(h,'Tag'),'Colorbar'),
  206. if ~exist('cbfreeze.m'),
  207. warning('JRI:freezeColors:checkArgs:cannotFreezeColorbar',...
  208. ['You seem to be attempting to freeze a colorbar. This no longer'...
  209. 'works. Please read the help for freezeColors for the solution.'])
  210. else
  211. cbfreeze(h);
  212. return
  213. end
  214. end
  215. args{1} = [];
  216. nargs = nargs-1;
  217. else
  218. h = gca;
  219. end
  220. %set nancolor if that option was specified
  221. nancolor = [nan nan nan];
  222. if nargs == 2,
  223. if strcmpi(args{end-1},'nancolor'),
  224. nancolor = args{end};
  225. if ~all(size(nancolor)==[1 3]),
  226. error('JRI:freezeColors:checkArgs:badColorArgument',...
  227. 'nancolor must be [r g b] vector');
  228. end
  229. nancolor(nancolor>1) = 1; nancolor(nancolor<0) = 0;
  230. else
  231. error('JRI:freezeColors:checkArgs:unrecognizedOption',...
  232. 'Unrecognized option (%s). Only ''nancolor'' is valid.',args{end-1})
  233. end
  234. end

转载自:https://blog.csdn.net/u012366767/article/details/82703670

相关文章:

  • 2022-02-08
  • 2021-12-16
  • 2021-10-25
  • 2021-09-24
  • 2022-01-07
  • 2021-06-18
  • 2021-07-18
  • 2021-04-09
猜你喜欢
  • 2021-12-15
  • 2022-01-07
  • 2021-11-25
  • 2022-01-16
  • 2022-02-21
相关资源
相似解决方案