e454.m

来自「matlab算法集 matlab算法集」· M 代码 · 共 40 行

M
40
字号
%----------------------------------------------------------------------
% Example 4.5.4: Least Squares Fit with Noise                    
%----------------------------------------------------------------------

% Initialize
 
   clc
   clear
   n = 100;
   m = 3;
   noise = 0.5;
   b = 3.2;
   x = linspace (0,b,n)';
   y = randu (n,1,-noise,noise);
   X = [x x];
   Y = zeros (n,2);

% Initialize arrays 

   fprintf ('Example 4.5.4: Least-Squares Fit with Noise\n');
   for i = 1 : n
      c = x(i);
      y(i) = y(i) + c*(c-1)*(c-3);
      Y(i,2) = y(i);
   end
      
% Find least-squares fit 
   
   a = polyfit (x,y,m);
   Y(:,1) = polyval (a,X(:,1));
   graphxy (X,Y,'Least-Squares Fit with Noise','x','y','d')
%----------------------------------------------------------------------







⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?