二次多项式曲面公式

总共有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

  • 添加噪声,随机选择 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]);

  • 用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

  • 完整程序
  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');

  • 使用cftool工具拟合

  • 附录:freezeColors函数可从https://ww2.mathworks.cn/matlabcentral/fileexchange/7943-freezecolors-unfreezecolors下载
  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 (iversen@nsi.edu) 3/23/05
  48. %
  49. % Changes:
  50. % JRI (iversen@nsi.edu) 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. % john_iversen@post.harvard.edu
  72. appdatacode = 'JRI__freezeColorsData';
  73. [h, nancolor] = checkArgs(varargin);
  74. %gather all children with scaled or indexed CData
  75. cdatah = getCDataHandles(h);
  76. %current colormap
  77. cmap = colormap;
  78. nColors = size(cmap,1);
  79. cax = caxis;
  80. % convert object color indexes into colormap to true-color data using
  81. % current colormap
  82. for hh = cdatah',
  83. g = get(hh);
  84. %preserve parent axis clim
  85. parentAx = getParentAxes(hh);
  86. originalClim = get(parentAx, 'clim');
  87. % Note: Special handling of patches: For some reason, setting
  88. % cdata on patches created by bar() yields an error,
  89. % so instead we'll set facevertexcdata instead for patches.
  90. if ~strcmp(g.Type,'patch'),
  91. cdata = g.CData;
  92. else
  93. cdata = g.FaceVertexCData;
  94. end
  95. %get cdata mapping (most objects (except scattergroup) have it)
  96. if isfield(g,'CDataMapping'),
  97. scalemode = g.CDataMapping;
  98. else
  99. scalemode = 'scaled';
  100. end
  101. %save original indexed data for use with unfreezeColors
  102. siz = size(cdata);
  103. setappdata(hh, appdatacode, {cdata scalemode});
  104. %convert cdata to indexes into colormap
  105. if strcmp(scalemode,'scaled'),
  106. %4/19/06 JRI, Accommodate scaled display of integer cdata:
  107. % in MATLAB, uint * double = uint, so must coerce cdata to double
  108. % Thanks to O Yamashita for pointing this need out
  109. idx = ceil( (double(cdata) - cax(1)) / (cax(2)-cax(1)) * nColors);
  110. else %direct mapping
  111. idx = cdata;
  112. %10/8/09 in case direct data is non-int (e.g. image;freezeColors)
  113. % (Floor mimics how matlab converts data into colormap index.)
  114. % Thanks to D Armyr for the catch
  115. idx = floor(idx);
  116. end
  117. %clamp to [1, nColors]
  118. idx(idx<1) = 1;
  119. idx(idx>nColors) = nColors;
  120. %handle nans in idx
  121. nanmask = isnan(idx);
  122. idx(nanmask)=1; %temporarily replace w/ a valid colormap index
  123. %make true-color data--using current colormap
  124. realcolor = zeros(siz);
  125. for i = 1:3,
  126. c = cmap(idx,i);
  127. c = reshape(c,siz);
  128. c(nanmask) = nancolor(i); %restore Nan (or nancolor if specified)
  129. realcolor(:,:,i) = c;
  130. end
  131. %apply new true-color color data
  132. %true-color is not supported in painters renderer, so switch out of that
  133. if strcmp(get(gcf,'renderer'), 'painters'),
  134. set(gcf,'renderer','zbuffer');
  135. end
  136. %replace original CData with true-color data
  137. if ~strcmp(g.Type,'patch'),
  138. set(hh,'CData',realcolor);
  139. else
  140. set(hh,'faceVertexCData',permute(realcolor,[1 3 2]))
  141. end
  142. %restore clim (so colorbar will show correct limits)
  143. if ~isempty(parentAx),
  144. set(parentAx,'clim',originalClim)
  145. end
  146. end %loop on indexed-color objects
  147. % ============================================================================ %
  148. % Local functions
  149. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  150. %% getCDataHandles -- get handles of all descendents with indexed CData
  151. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  152. function hout = getCDataHandles(h)
  153. % getCDataHandles Find all objects with indexed CData
  154. %recursively descend object tree, finding objects with indexed CData
  155. % An exception: don't include children of objects that themselves have CData:
  156. % for example, scattergroups are non-standard hggroups, with CData. Changing
  157. % such a group's CData automatically changes the CData of its children,
  158. % (as well as the children's handles), so there's no need to act on them.
  159. error(nargchk(1,1,nargin,'struct'))
  160. hout = [];
  161. if isempty(h),return;end
  162. ch = get(h,'children');
  163. for hh = ch'
  164. g = get(hh);
  165. if isfield(g,'CData'), %does object have CData?
  166. %is it indexed/scaled?
  167. if ~isempty(g.CData) && isnumeric(g.CData) && size(g.CData,3)==1,
  168. hout = [hout; hh]; %#ok<AGROW> %yes, add to list
  169. end
  170. else %no CData, see if object has any interesting children
  171. hout = [hout; getCDataHandles(hh)]; %#ok<AGROW>
  172. end
  173. end
  174. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  175. %% getParentAxes -- return handle of axes object to which a given object belongs
  176. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  177. function hAx = getParentAxes(h)
  178. % getParentAxes Return enclosing axes of a given object (could be self)
  179. error(nargchk(1,1,nargin,'struct'))
  180. %object itself may be an axis
  181. if strcmp(get(h,'type'),'axes'),
  182. hAx = h;
  183. return
  184. end
  185. parent = get(h,'parent');
  186. if (strcmp(get(parent,'type'), 'axes')),
  187. hAx = parent;
  188. else
  189. hAx = getParentAxes(parent);
  190. end
  191. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  192. %% checkArgs -- Validate input arguments
  193. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  194. function [h, nancolor] = checkArgs(args)
  195. % checkArgs Validate input arguments to freezeColors
  196. nargs = length(args);
  197. error(nargchk(0,3,nargs,'struct'))
  198. %grab handle from first argument if we have an odd number of arguments
  199. if mod(nargs,2),
  200. h = args{1};
  201. if ~ishandle(h),
  202. error('JRI:freezeColors:checkArgs:invalidHandle',...
  203. 'The first argument must be a valid graphics handle (to an axis)')
  204. end
  205. % 4/2010 check if object to be frozen is a colorbar
  206. if strcmp(get(h,'Tag'),'Colorbar'),
  207. if ~exist('cbfreeze.m'),
  208. warning('JRI:freezeColors:checkArgs:cannotFreezeColorbar',...
  209. ['You seem to be attempting to freeze a colorbar. This no longer'...
  210. 'works. Please read the help for freezeColors for the solution.'])
  211. else
  212. cbfreeze(h);
  213. return
  214. end
  215. end
  216. args{1} = [];
  217. nargs = nargs-1;
  218. else
  219. h = gca;
  220. end
  221. %set nancolor if that option was specified
  222. nancolor = [nan nan nan];
  223. if nargs == 2,
  224. if strcmpi(args{end-1},'nancolor'),
  225. nancolor = args{end};
  226. if ~all(size(nancolor)==[1 3]),
  227. error('JRI:freezeColors:checkArgs:badColorArgument',...
  228. 'nancolor must be [r g b] vector');
  229. end
  230. nancolor(nancolor>1) = 1; nancolor(nancolor<0) = 0;
  231. else
  232. error('JRI:freezeColors:checkArgs:unrecognizedOption',...
  233. 'Unrecognized option (%s). Only ''nancolor'' is valid.',args{end-1})
  234. end
  235. end

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

Matlab--二次多项式曲面拟合相关推荐

  1. 【Matlab学习手记】二次多项式曲面拟合

    二次多项式曲面公式 总共有6个系数. 绘制曲面图形时,一般给定x和y的取值(一维数组),然后对x和y网格化成二维数组X和Y,将X和Y代入公式,即可得到曲面的数值,最后用surf函数显示. 实例 给定一 ...

  2. 用Matlab求二次多项式,matlab二次多项式拟合

    用matlab做散点的二次曲线拟合_数学_自然科学_专业资料.例 对下面一组数据作二次多项式拟合 xi 0.1 0.2 0.4 0.5 0.6 0.7 0.8 0.9 1 yi 1.978 ..... ...

  3. matlab二次多项式插值,MATLAB应用——多项式与插值函数

    数学建模是用数学方法解决各种实际问题的桥梁,它已经渗透到各个领域,而且发挥出越来越重要的作用.面对自然科学和工程应用中的难题,大部分人无从入手,而个别人却能短时间内给出切实可行的解决方案,其差别往往在 ...

  4. 【C++】【MATLAB】三元二次多项式拟合求极值点原理+代码

    一.需求描述 本人最近需要对多个3维数据进行曲线的拟合,并且找到极大值点. 难点: 1.一组数据有125个点,每个点有3个坐标值(x,y,z),以及一个对应的得分值t.x,y,z范围不限,t的范围是0 ...

  5. 有限元方法数学理论之分片二次多项式空间逼近理论

    在一个月之前我已经对有限元方法数学理论之分片一次多项式空间逼近理论进行了讲解,由于中间有点事情所以对二次多项式情况没有进行解说,下面开始正文. 其实思想跟一次多项式的情况一样,只不过这里在一个小区间上 ...

  6. matlab三次拟合多项式,matlab三次多项式拟合

    暂无评价|0人阅读|0次下载|举报文档 DSC曲线的三次样条拟合与Matlab实现_能源/化工_工程科技_专业资料.DSC曲线拟合与Matlab实现第... 人阅读|次下载 曲线拟合的最小二乘法mat ...

  7. 肤色检测算法 - 基于二次多项式混合模型的肤色检测

    1.二次多项式混合模型 二次多项式混合模型首先有SORIANO提出,此后CHIANG对此进行了改进.改进后的模型由两个R-G平面的二次多项式和一个圆方程构成: 在以上三个方程的基础上,肤色区域可以通过 ...

  8. ML之LiR2PolyR4PolyR:使用线性回归LiR、二次多项式回归2PolyR、四次多项式回归4PolyR模型在披萨数据集上拟合(train)、价格回归预测(test)

    ML之LiR&2PolyR&4PolyR:使用线性回归LiR.二次多项式回归2PolyR.四次多项式回归4PolyR模型在披萨数据集上拟合(train).价格回归预测(test) 目录 ...

  9. ML之LiR2PolyR:使用线性回归LiR、二次多项式回归2PolyR模型在披萨数据集上拟合(train)、价格回归预测(test)

    ML之LiR&2PolyR:使用线性回归LiR.二次多项式回归2PolyR模型在披萨数据集上拟合(train).价格回归预测(test) 目录 输出结果 设计思路 核心代码 输出结果 设计思路 ...

最新文章

  1. 如何线程安全的使用HashMap
  2. hbase读写流程及缓存机制
  3. SpringCloud个人笔记-01-Eureka初体验
  4. TypeError系列之:TypeError: only size-1 arrays can be converted to Python scalars
  5. apk源码查看工具_如何查看Linux命令工具的源码?
  6. 【剑指offer】面试题04:二维数组中的查找(java)
  7. 在IDEA中设置自己的名字和时间
  8. IIS Tomcat共享80端口
  9. zookeeper3.3.6 伪分布式安装
  10. 热带鱼水族箱屏幕保护程序
  11. 2004年中国十大暴利行业
  12. android 中文冒号,Kotlin中双冒号::使用方法
  13. 可口可乐市场调查失败的原因_经典案例可口可乐一次市场调研失败的教训
  14. 一个程序员的真实故事上
  15. Kernel panic - not syncing: IO-APIC + timer doesn‘t work解决办法
  16. 【echarts】在柱状图上方显示数值
  17. 苹果电脑MACbook Air快捷键大全
  18. 纯千兆电口和自适应电口的区别
  19. TFN F7 光时域反射仪 给您不一样体验
  20. Android studio新手:实现最新版QQ登陆界面

热门文章

  1. 面试官系统精讲Java源码及大厂真题 - 26 惊叹面试官:由浅入深手写队列
  2. DreamFactory入门指南 - 第1章REST和DreamFactory简介
  3. 容器编排技术 -- Kubernetes 使用 PodPreset 将信息注入 Pods
  4. 使用Docker制作zentao禅道镜像
  5. Docker安装RabbitMQ(docker-compose.yml)
  6. 如何在两个服务器之间迁移MySQL数据库
  7. BGP——软收敛(讲解+配置命令)
  8. cookie的设置与取值
  9. 【安卓开发】启动另一个 Activity
  10. 给你的网站添加运行时间