精华内容
下载资源
问答
  • 二次多项式拟合算法matlab

    热门讨论 2010-07-30 16:36:48
    二次多项式拟合算法matlab,希望对需要的人给以一定的参考!
  • MATLAB中的多项式拟合

    千次阅读 2019-01-20 18:58:59
    下面来给大家介绍一下如何在MATLAB中进行多项式拟合多项式拟合是用一个多项式展开去拟合包含数个分析格点的一小块分析区域中的所有观测点,得到观测数据的客观分析场。展开系数用最小二乘拟合确定。但此方法的区域...

    下面来给大家介绍一下如何在MATLAB中进行多项式拟合,多项式拟合是用一个多项式展开去拟合包含数个分析格点的一小块分析区域中的所有观测点,得到观测数据的客观分析场。展开系数用最小二乘拟合确定。但此方法的区域多项式拟合并不稳定,当资料缺测时更是如此,而且会导致分析在拟合的各个区域之间不连续。具体介绍如下:

    1、首先打开MATLAB软件,在其主界面新建编辑器文件,输入下列代码:

    x=[1 2 3 4 5 6 7 8 9];
    y=[9 7 6 3 -1 2 5 7 20];
    P=polyfit(x, y, 3);
    xi=0:2:10;
    yi=polyval(P, xi);
    plot(xi, yi, x, y, 'r*');

    2、代码命名保存至自定义路径下,点击运行,结果如下:

    如图所示,我们拟合的曲线是围绕图中红色带星号的点进行的,红色新号点表示的就是原始数据,可以看到我们拟合的效果还是有很大误差的,因此只能在一定误差范围内使用多项式拟合,且根据精度可适当调整其误差大小。请继续关注!!!

    展开全文
  • % 多次多项式方程 % 误差项求和方程,这里是提供的值对应的函数值。 % 分别对不同系数求导数: % f(x) = a2*x^2 + a1*x + a0 A = []; % 最小二乘法求拟合曲线系数 for k=1:1:3 for g=1:1:4 sumdata1 ...

     %  多次多项式方程

    %  误差项求和方程,这里y_{i}是提供的x_{i}值对应的函数值。

    % 分别对不同系数a_{0}\rightarrowa_{k}求导数:

    展开全文
  • Matlab学习手记】二次多项式曲面拟合

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

    二次多项式曲面公式

        总共有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工具拟合

    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 subsequent changes to the colormap (or caxis) will not change the
    %       colors of these 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
    %       based 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
    %       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; freezeColors etc...
    %
    %       Note: colorbars must also be frozen. Due to Matlab 'improvements' this can
    %				no longer be done with freezeColors. Instead, please
    %				use the function CBFREEZE by Carlos Adrian Vargas Aguilera
    %				that can be downloaded from the MATLAB File Exchange
    %				(http://www.mathworks.com/matlabcentral/fileexchange/24371)
    %
    %       h=colorbar; cbfreeze(h), or simply cbfreeze(colorbar)
    %
    %       For additional examples, see test/test_main.m
    %
    %   Side effect on render mode: freezeColors does not work with the painters
    %       renderer, because 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 Iversen (iversen@nsi.edu) 3/23/05
    %
    
    %   Changes:
    %   JRI (iversen@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 Preserves NaN colors. Hidden option (v 2.2, not uploaded)
    %   JRI 3/17/07  Preserve 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 preserving 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 represented by NaN in the indexed color
    %   data, which renders transparently. This transparency will be preserved
    %   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 uses, but please retain the following:
    %   Original Author:
    %   John Iversen, 2005-10
    %   john_iversen@post.harvard.edu
    
    appdatacode = 'JRI__freezeColorsData';
    
    [h, nancolor] = checkArgs(varargin);
    
    %gather all children with scaled or indexed CData
    cdatah = getCDataHandles(h);
    
    %current colormap
    cmap = colormap;
    nColors = size(cmap,1);
    cax = caxis;
    
    % convert object color indexes into colormap to true-color data using 
    %  current colormap
    for hh = cdatah',
        g = get(hh);
        
        %preserve parent axis clim
        parentAx = getParentAxes(hh);
        originalClim = get(parentAx, 'clim');    
       
        %   Note: Special handling of patches: For some reason, setting
        %   cdata on patches created by bar() yields an error,
        %   so instead we'll set facevertexcdata instead for patches.
        if ~strcmp(g.Type,'patch'),
            cdata = g.CData;
        else
            cdata = g.FaceVertexCData; 
        end
        
        %get cdata mapping (most objects (except scattergroup) have it)
        if isfield(g,'CDataMapping'),
            scalemode = g.CDataMapping;
        else
            scalemode = 'scaled';
        end
        
        %save original indexed data for use with unfreezeColors
        siz = size(cdata);
        setappdata(hh, appdatacode, {cdata scalemode});
    
        %convert cdata to indexes into colormap
        if strcmp(scalemode,'scaled'),
            %4/19/06 JRI, Accommodate scaled display of integer cdata:
            %       in MATLAB, uint * double = uint, so must coerce cdata to double
            %       Thanks to O Yamashita for pointing this need out
            idx = ceil( (double(cdata) - cax(1)) / (cax(2)-cax(1)) * nColors);
        else %direct mapping
            idx = cdata;
            %10/8/09 in case direct data is non-int (e.g. image;freezeColors)
            % (Floor mimics how matlab converts data into colormap index.)
            % Thanks to D Armyr for the catch
            idx = floor(idx);
        end
        
        %clamp to [1, nColors]
        idx(idx<1) = 1;
        idx(idx>nColors) = nColors;
    
        %handle nans in idx
        nanmask = isnan(idx);
        idx(nanmask)=1; %temporarily replace w/ a valid colormap index
    
        %make true-color data--using current colormap
        realcolor = zeros(siz);
        for i = 1:3,
            c = cmap(idx,i);
            c = reshape(c,siz);
            c(nanmask) = nancolor(i); %restore Nan (or nancolor if specified)
            realcolor(:,:,i) = c;
        end
        
        %apply new true-color color data
        
        %true-color is not supported in painters renderer, so switch out of that
        if strcmp(get(gcf,'renderer'), 'painters'),
            set(gcf,'renderer','zbuffer');
        end
        
        %replace original CData with true-color data
        if ~strcmp(g.Type,'patch'),
            set(hh,'CData',realcolor);
        else
            set(hh,'faceVertexCData',permute(realcolor,[1 3 2]))
        end
        
        %restore clim (so colorbar will show correct limits)
        if ~isempty(parentAx),
            set(parentAx,'clim',originalClim)
        end
        
    end %loop on indexed-color objects
    
    
    % ============================================================================ %
    % Local functions
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% getCDataHandles -- get handles of all descendents with indexed CData
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    function hout = getCDataHandles(h)
    % getCDataHandles  Find all objects with indexed CData
    
    %recursively descend object tree, finding objects with indexed CData
    % An exception: don't include children of objects that themselves have CData:
    %   for example, scattergroups are non-standard hggroups, with CData. Changing
    %   such a group's CData automatically changes the CData of its children, 
    %   (as well as the children's handles), so there's no need to act on them.
    
    error(nargchk(1,1,nargin,'struct'))
    
    hout = [];
    if isempty(h),return;end
    
    ch = get(h,'children');
    for hh = ch'
        g = get(hh);
        if isfield(g,'CData'),     %does object have CData?
            %is it indexed/scaled?
            if ~isempty(g.CData) && isnumeric(g.CData) && size(g.CData,3)==1, 
                hout = [hout; hh]; %#ok<AGROW> %yes, add to list
            end
        else %no CData, see if object has any interesting children
                hout = [hout; getCDataHandles(hh)]; %#ok<AGROW>
        end
    end
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% getParentAxes -- return handle of axes object to which a given object belongs
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    function hAx = getParentAxes(h)
    % getParentAxes  Return enclosing axes of a given object (could be self)
    
    error(nargchk(1,1,nargin,'struct'))
    %object itself may be an axis
    if strcmp(get(h,'type'),'axes'),
        hAx = h;
        return
    end
    
    parent = get(h,'parent');
    if (strcmp(get(parent,'type'), 'axes')),
        hAx = parent;
    else
        hAx = getParentAxes(parent);
    end
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% checkArgs -- Validate input arguments
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    function [h, nancolor] = checkArgs(args)
    % checkArgs  Validate input arguments to freezeColors
    
    nargs = length(args);
    error(nargchk(0,3,nargs,'struct'))
    
    %grab handle from first argument if we have an odd number of arguments
    if mod(nargs,2),
        h = args{1};
        if ~ishandle(h),
            error('JRI:freezeColors:checkArgs:invalidHandle',...
                'The first argument must be a valid graphics handle (to an axis)')
        end
        % 4/2010 check if object to be frozen is a colorbar
        if strcmp(get(h,'Tag'),'Colorbar'),
          if ~exist('cbfreeze.m'),
            warning('JRI:freezeColors:checkArgs:cannotFreezeColorbar',...
                ['You seem to be attempting to freeze a colorbar. This no longer'...
                'works. Please read the help for freezeColors for the solution.'])
          else
            cbfreeze(h);
            return
          end
        end
        args{1} = [];
        nargs = nargs-1;
    else
        h = gca;
    end
    
    %set nancolor if that option was specified
    nancolor = [nan nan nan];
    if nargs == 2,
        if strcmpi(args{end-1},'nancolor'),
            nancolor = args{end};
            if ~all(size(nancolor)==[1 3]),
                error('JRI:freezeColors:checkArgs:badColorArgument',...
                    'nancolor must be [r g b] vector');
            end
            nancolor(nancolor>1) = 1; nancolor(nancolor<0) = 0;
        else
            error('JRI:freezeColors:checkArgs:unrecognizedOption',...
                'Unrecognized option (%s). Only ''nancolor'' is valid.',args{end-1})
        end
    end
    
    
    

     

    展开全文
  • 一种商品的需求量与价格有一定关系。现对一定时期内的商品价格(x)与需求量(y)进行观察,取得一下样本数据,分别作出上述数据点的二次、三次、四次多项式拟合曲线f(x),绘出图形
  • Matlab--二次多项式曲面拟合

    千次阅读 2019-03-15 10:08:07
    二次多项式曲面公式 总共有6个系数。 绘制曲面图形时,一般给定x和y的取值(一维数组),然后对x和y网格化成二维数组X和Y,将X和Y代入公式,即可得到曲面的数值,最后用surf函数显示。 实例&amp;nbsp; &...
    二次多项式曲面公式

    总共有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工具拟合

    
     
    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 多项式拟合

    千次阅读 2019-09-14 15:19:04
    文章目录一次多项式二次多项式 一次多项式 x = [0.33, 1.12, 1.41, 1.71, 2.19] y = [0.68, 0.91, 1.15, 1.83, 2.07] %拟合 p1 = polyfit(x, y, 1) % 用x 和 y 拟合多项式, 1 表示一次多项式 % 输出 0....
  • 本文实例讲述了Python实现的多项式拟合功能。分享给大家供大家参考,具体如下: # -*- coding: utf-8 -*- #! python2 import numpy as np import matplotlib.pyplot as plt from pylab import mpl mpl.rcParams['...
  • matlab开发-拉格朗日多项式和二次多项式。从定义出发构造拉格朗日插值多项式。
  • matlab 最小二乘法拟合多项式怎么进行精度的优化啊数据为clear all;clcx=[0.240951631 0.883715287 0.88708334 0.91275251 0.932122682 0.94019212 0.953360145 0.953360145];y=[6.057750...
  • 最小二乘法的基本原理和多项式拟合matlab实现.doc
  • 使用matlab进行多项式拟合

    万次阅读 2015-10-14 21:06:30
    上头说可以用matlab拟合,这个之前还真没用过,用了一下还真是好用。好了,废话不多说了,简单举个例子试验一下。 如下图所示: 在matlab的命令行窗口里面写,当然如果数据较多和为了保存程序也可以在文件中写...
  • MATLAB 线性回归多项式拟合+预测区间、...对该数据进行线性回归(1次多项式拟合)并且绘制预测区间和置信度为95%的置信区间 拟合参数: 拟合绘制: 、多项式拟合polyfit 1、语法 p=polyfit(x,y,n):最小二乘法计算拟
  • 多项式拟合——用Matlab实现并分析

    万次阅读 多人点赞 2015-11-10 16:44:41
    多项式拟合——用Matlab是实现并分析 1、问题  编程实现多项式拟合例子,体会overfitting。 2、方法  可以使用matlab中的方法实现多项式拟合。polyfit(x,y,n),其中n表示预测的多项式为n。[p,S] = polyfit(x,...
  • 多项式拟合。 然后画出函数曲线来。以及给出函数形式。 求导。 再画出导数的曲线。以及给出导数函数形式。 matlab代码: clc close all clear %% 给出一列数 xx = 1:100; yy = xx.^3 - xx.^2; first = 1; ...
  • Matlab 多项式曲线拟合polyfit

    万次阅读 多人点赞 2018-08-18 12:01:04
     多项式曲线拟合 常见语法  a = polyfit ( x, y, n) 说明  a=polyfit(x,y,n)中参数n为x的最高阶,返回值a是n阶函数的系数,a是一个长度为n+1的行向量, 即拟合出来的公式形式应为:  示例 用多项式...
  • 拟合拟合问题的目标是寻求一个函数(曲线),使得该曲线在某种准则下与所有的数据点最为接近,即曲线拟合的最好(最小化损失函数)。必须有函数表达式、不一定通过所有数据点。、模型适用范围或案例功能:...
  • 有数据如下,对其进行曲线拟合: x=[0;0.4;1.2;2;2.8;3.6;4.4;5.2;6;7.2;8;9.2;10.4;11.6;12.4;13.6;14.4;15]; y=[1;0.85;...使用二次多项式进行曲线拟合 close all; clc; clear; x=[0;0.4;1.2;2;2.8;
  • 本文使用 Zhihu On VSCode 创作并发布0 前言自动驾驶开发中经常涉及到多项式曲线拟合,本文详细描述了使用最小二乘法进行多项式曲线拟合的数学原理,通过样本集构造范德蒙德矩阵,将一元 N 次多项式非线性回归问题...
  • 拟合算法曲线拟合问题的提法是,已知一组(维)数据,即平面上的 n 个点(xi , yi ) ,i = 1,2,L,n , xi 互不相同,寻求一个函数(曲线) y = f (x) ,使 f (x) 在某种准则下与所有数据点最为接近,即曲线拟合得...
  • [更新 2014-02-19:修复 polyfitBM 中的错误,添加 root 和 select-terms 工具] 此提交包含四个类似于 POLYFIT 的便捷多项式拟合函数。 1. POLYFITZERO - 将多项式拟合到数据,强制 y 截距为零。 2. POLYFITB - 强制...
  • @这是我第一使用 (0基础)Matlab 来对 多项式拟合 进行一个公式原理上的编写。 @帮我一个好集美写的,目前大二,她数学专业。 @期间学习了一些Matlab语言的基本语法和简单功能。 @记录一下,欢迎下方评论交流不足...
  • 多项式拟合 例:已知的数据点来自f(x)=(x2+3x+5)e−5x*sinx,用多项式拟合的方法在不同的阶次下进行拟合。 >> x0=0:.1:1; y0=(x0.^2-3*x0+5).*exp(-5*x0).*sin(x0); >> p3=polyfit(x0,y0,3); vpa...
  • 介绍多项式曲线拟合的基本理论,对多项式数据拟合原理进行了全方面的理论阐述,同时也阐述了曲线拟合的基本原理及多项式曲线拟合模型的建立。具体记录了多项式曲线拟合的具体步骤,在建立理论的基础上具体实现多...
  • Matlab——多项式拟合

    万次阅读 2012-09-06 20:42:41
    多项式拟合指令】 polyfit(X,Y,N):多项式拟合,返回降幂排列的多项式系数。 polyval(P,xi):计算多项式的值。 其中,X,Y是数据点的值;N是拟合的最高幂;P是返回的多项式系数;xi是要求的点的横坐标。 ...
  • 曲线拟合多项式函数+MATLAB实例)

    千次阅读 2021-05-09 16:46:35
    这里我们主要考虑用多项式函数去进行曲线拟合 (1)polyfit 函数 功能: 求得结果为最小二乘法拟合多项式系数。 格式: p = polyfit(X,Y,n); X,Y为进行拟合的样本数据,类型为矩阵。n为拟合多项式次数,看散点...
  • C++——多项式拟合

    千次阅读 多人点赞 2018-07-20 15:37:32
    C++——多项式拟合 目标:利用C++对txt或者xml中的数据,进行高阶或低阶多项式拟合  为方便以后查找,代码以及详细资料已打包,并上传至云盘(链接:https://pan.baidu.com/s/1bvUBIoxv7Avxeq_Cz6xOZQ 密码:u9qe...
  • 多项式拟合一般方程法详细推导

    千次阅读 2020-09-20 16:16:24
    多项式拟合之一般方程法 文章目录多项式拟合之一般方程法1 什么是多项式拟合?2 怎样拟合?3 求bbb正交矩阵QR分解施密特正交化matlab测试函数 之前经常会用到多项式拟合来拟合函数关系,十分好用,得出的表达式使用...
  • 分段七次多项式插值(MATLAB实现)

    千次阅读 2020-06-06 14:40:49
      给定n+1n+1n+1个点序列(ti,pi)(t_i,p_i)(ti​,pi​),利用分段七次多项式插值,使得分段多项式经过所有点序列。其中,tit_iti​必须单调递增,i=0,1,...,ni=0,1,...,ni=0,1,...,n。 、推导步骤   起点处一阶...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 2,901
精华内容 1,160
关键字:

matlab二次多项式拟合

matlab 订阅