title: How do i get aproximation of a function?
body: <p>I have the following function :
  f (t) = 0.05<em>sin(1000</em>t)+0.5<em>cos(pi</em>t)-0.4*(10*t)
  t are points distributed equidistantly between [0,1] for which i calculate values of f .
  The following pairs (ti,f(ti)) are inputs for aproximation of function f with Lagrange and Least Squares.
  This is what i tried, but doesn't seem to work, because i'm not sure if i'm doing f right</p>
  <pre><code>%Main function
  t=linspace(0,1);
  f = 0.05*sin(1000*t)+0.5*cos(pi*t)-0.4*sin(10*t);
  lagrange(t,f); % returns coeficients of Lagrange polynomial of rank 1
  least_squares(t,f) % returns coeficients of polynomial of rank n using least squares method
  </code></pre>
  <p>This is the lagrange function:</p>
  <pre><code>%Lagrange
  function[L] = lagrange(x,y)
  n = length(x);
  lj= zeros(1,n)
  Lj= zeros(n);
  L=zeros(1,n);
  jr=1:n;
  for j=jr
      mr=jr(jr~=j);%m-range 1&lt;=m&lt;=n, m~=j
      lj=poly(x(mr));
      mult=1/polyval(lj,x(j));
      Lj(j,:)=mult*lj;
  end
  L=y*Lj;
  X=-10:.1:10;
  plot(x,y,'-*','linewidth',1,'markersize',5)
  end
  </code></pre>
  <p>And the least_squares function:</p>
  <pre><code>function [yR] =least_squares(x,y)
  stem(x,y);
  a=[];
  for i=1:length(x)
      a=[a;x(i) 1];
  end
  c=a/y';
  yR=c(1)*x+c(2);
  plot(x,yR,&quot;-*&quot;);
  end
  </code></pre>