% Eksempel -- Lineaer modell med 2. grads pol.

clear all;

x=(1:0.2:2.4)'; m=8; %antall punkter

y1=ones(m,1)+ 3.75*x - x.^2;  

y = y1 + 0.3*(0.5*ones(m,1)-rand(m,1)); 

figure(7)
plot(x, y,'or')     %plotter  punktene

hold on

t=linspace(0.5, 2.5);

p = 1 + 3.75*t - t.^2;

plot(t,p,'g')  % plotter "utgangspolynomet" 


X=[ones(m,1) x x.^2];     % X er designmatrisen

beta = X'*X \ X'*y    %gir minste kvadraters losning

t=linspace(0.5, 2.5); 

f = beta(1)+ beta(2)*t + beta(3)*t.^2;  % gir minste kvadraters 2.grads polynom


figure(8)

plot(t,f,'b')  % plotter f

hold on

plot(t,p,'g')

hold on

plot(x, y,'or')


    