No BSD License  

Highlights from
Introduction à Matlab (deuxième édition)

from Introduction à Matlab (deuxième édition) by Jean-Thierry
tous les m-fichiers relatifs à la deuxième édition de l'Introduction à Matlab

mlsq(x, y, q, percent)
          function [sol, chi2] = mlsq(x, y, q, percent)
          %mlsq      calcul de l'approximante aux moindres carres
          %            suivant la methode de la moindre mediane
          %            function [sol chi2] = mlsq( x, y, q, percent)
          %
          %            q       : pourcentage d'erreur souhaite : dfaut 0.05
          %            percent : pourcentage de donnees aberrantes (0 < q <= 0.5)
          %                      defaut 0.5;

          if nargin < 4
            percent = 0.5;
          end;
          if nargin < 3
            q = 0.05;
          end;
          n = length(x);
          nbtry = max(min(n,fix(log(q)./log(1-(1-percent).^2))+1),5);
          meds = zeros(1,nbtry);
          for i=1:nbtry
            perm = randperm(n);
            i1 = perm(1);
            i2 = perm(2);
            a(i) = (y(i2)-y(i1))./(x(i2)-x(i1));
            b(i) = (y(i1).*x(i2) -y(i2).*x(i1))./(x(i2)-x(i1));
            z = sort( abs(y-a(i).*x - b(i)));
            meds(i) = z(n/2);
          end;
          [m i0] = min(meds);
          goods = abs(y-a(i0).*x - b(i0)) <= m;
          [sol chi2] = linfit(x(goods), y(goods));

Contact us at files@mathworks.com