tv=[0:2:10]';
yv=[1;1.6;1.4;0.6;0.2;0.8];

clf;
pp=plot(tv,yv,'ko');
set(pp,'MarkerFaceColor','k');
hold on;

A=[ones(6,1), sin(2*pi*tv/12), cos(2*pi*tv/12)];
c=A\yv;
g=@(t) c(1)+c(2)*sin(2*pi*t/12)+c(3)*cos(2*pi*t/12);
ttv=0:0.01:10;
plot(ttv,g(ttv),'--');
n=6;
f=@(c) c(1)+c(2)*sin(c(4)*tv)+c(3)*cos(c(4)*tv)-yv;
J=@(c) [ones(n,1),sin(c(4)*tv),cos(c(4)*tv),c(2)*cos(c(4)*tv).*tv-c(3)*sin(c(4)*tv).*tv]

x=[c(1:3);2*pi/12];
TOL=1e-10;
h=inf;
while (norm(h)>TOL)
  h=J(x)\f(x);  % vi löser ett linjärt _överbestämt_ ekvationssystem
  x=x-h;
  norm(h)
end
c=x;
g=@(t) c(1)+c(2)*sin(c(4)*t)+c(3)*cos(c(4)*t);

plot(ttv,g(ttv),'r-');
legend('data','linj. anpassn', 'olinj. anpassn')
grid on
xlabel('t')
ylabel('Hoejd')