%Minstakvadratanpassad sinuskurva, NAM 6.10.2
%Bestäm a,b,w,t0 i F(t)=a+bsin(w(t-t0))
t=[0.5 0.8 1 1.2 1.5 1.8 2 2.4]';
y=[0.3 0.3 0.5 0.9 1.4 1.1 0.5 0.3]';
a=0.7;b=0.7;w=pi;t0=1.2;c=[a b w t0]';
h=1;format compact
while h>1e-4
    u=w*(t-t0);f=a+b*sin(u)-y;
    J=[ones(8,1) sin(u) b*(t-t0).*cos(u) -w*b*cos(u)];
    delc=-J\f;h=norm(delc,inf);fh=[norm(f) h]
    c=c+delc;c'
    a=c(1);b=c(2);w=c(3);t0=c(4);
end
tt=[0:0.05:3]';Ftt=a+b*sin(w*(tt-t0));
plot(t,y,'o',tt,Ftt)
title('Mätdata och minstakvadratanpassad sinuskurva')
format
