function exercise9task2
% Data
x=[1.02,0.95,0.87,0.77,0.67,0.56,0.44,0.30,0.16,0.01]';
y=[0.39,0.32,0.27,0.22,0.18,0.15,0.13,0.12,0.13,0.15]';
n = length(x);
% Least Squares
A = [y.^2 , x.*y , x , y , ones(n,1)];
beta = x.^2;
alpha = (A'*A)\(A'*beta);
% Plot
[X,Y]=meshgrid(-1:0.01:1.4,0:0.01:1.4);
Z=alpha(1)*Y.^2 + alpha(2)*X.*Y + alpha(3)*X + alpha(4)*Y + alpha(5) - X.^2;
contour(X,Y,Z,[0 0],'Linewidth',2)
hold on
plot(x,y,'ro')
% Perturbed data
x2 = x + 0.01*rand(n,1)-0.005;
y2 = y + 0.01*rand(n,1)-0.005;
% Least Squares
A = [y2.^2 , x2.*y , x2 , y2 , ones(n,1)];
beta = x2.^2;
alpha = (A'*A)\(A'*beta);
% Plot
[X,Y]=meshgrid(-1:0.01:1.4,0:0.01:1.4);
Z=alpha(1)*Y.^2 + alpha(2)*X.*Y + alpha(3)*X + alpha(4)*Y + alpha(5) - X.^2;
contour(X,Y,Z,[-1 1],50)
hold on
plot(x2,y2,'b*')
end