%% Analyse de la fonction de variance et la fonction d'information
%
% Table of Contents
% Surfaces
% Designs
%     Star design + factorial
% Model: Linear answer
% y=a_o+a_1x_1+a_2x_2

syms x1 x2
f1=[1,x1,x2]

% Model: Linear with interactions
% y=a_o+a_1\ x_1+a_2\ x_2+a_{12}\ x_1\ x_2

f2=[1,x1,x2,x1*x2]

% Model: quadratic
% y=a_o+a_1\ x_1+a_2\ x_2+a_{12}\ x_1\ x_2 + a_{11}\ x_1^2 + a_{22}\ x_2^2

f3=[1,x1,x2,x1*x2,x1^2,x2^2]

% Model: quadratic with cubic interactions
% y=a_o+a_1\ x_1+a_2\ x_2+a_{12}\ x_1\ x_2 + a_{11}\ x_1^2 + a_{22}\ x_2^2 + a_{112}\ x_1^2\ * x_2 + a_{122}\ x_1\ * x_2^2

f4=[1,x1,x2,x1*x2,x1^2,x2^2,x1^2*x2,x1*x2^2]

% Model: Cubic
% y=a_o+a_1\ x_1+a_2\ x_2+a_{12}\ x_1\ x_2 + a_{11}\ x_1^2 + a_{22}\ x_2^2 + a_{112}\ x_1^2\ * x_2 + a_{122}\ x_1\ * x_2^2 + a_{111}\ x_1^3 + a_{222}\ x_2^3

f5=[1,x1,x2,x1*x2,x1^2,x2^2,x1^2*x2,x1*x2^2,x1^3,x2^3]

%
%% Surfaces

alpha=randn(10,1);

fsurf(x1,x2,f1*alpha(1:3),[-1,1,-1,1])
title('Linear response')
xlabel('x_1')
ylabel('x_2')
zlabel('y')

fsurf(x1,x2,f2*alpha(1:4),[-1,1,-1,1])
title('Linear with interactions')
xlabel('x_1')
ylabel('x_2')
zlabel('y')


fsurf(x1,x2,f3*alpha(1:6),[-1,1,-1,1])
title('Quadratic')
xlabel('x_1')
ylabel('x_2')
zlabel('y')


fsurf(x1,x2,f4*alpha(1:8),[-1,1,-1,1])
title('Quadratic + a_{ijj}')
xlabel('x_1')
ylabel('x_2')
zlabel('y')

fsurf(x1,x2,f5*alpha,[-1,1,-1,1])
title('Cubic')
xlabel('x_1')
ylabel('x_2')
zlabel('y')

%
%% Designs

% star
Es=[0 0
    -1 0
    1 0
    0 -1
    0 1];

% Modèle linéaire

Xs1=x2fx(Es,"linear");
Vs1=f1/(Xs1.'*Xs1)*f1.'
fsurf(x1,x2,Vs1,[-1,1,-1,1])
title('Variance of the linear model with a star design')
xlabel('x_1')
ylabel('x_2')
zlabel('y')

hold on
plot(Es(:,1),Es(:,2),'ok','MarkerFaceColor','red')
hold off
% fact
Ef=[-1 -1
    -1 1
    1 -1
    1 1]
% modèle linéaire
Xf1=x2fx(Ef,"linear");
Vf1=f1/(Xf1.'*Xf1)*f1.'
fsurf(x1,x2,Vf1,[-1,1,-1,1])
title('Variance of the linear model with a factorial design')
xlabel('x_1')
ylabel('x_2')
zlabel('y')

hold on
plot(Ef(:,1),Ef(:,2),'ok','MarkerFaceColor','red')
hold off
% modèle avec interactions
Xf2=x2fx(Ef,"interaction");
Vf2=f2/(Xf2.'*Xf2)*f2.'
fsurf(x1,x2,Vf2,[-1,1,-1,1])
title('Variance of the interaction model with a factorial design')
xlabel('x_1')
ylabel('x_2')
zlabel('y')

hold on
plot(Ef(:,1),Ef(:,2),'ok','MarkerFaceColor','red')
hold off

%
%% Star design + factorial

Efs=[Es;Ef]
% modèle linéaire
Xfs1=x2fx(Efs,"linear");
Vfs1=f1/(Xfs1.'*Xfs1)*f1.'
fsurf(x1,x2,Vf1,[-1,1,-1,1])
title('Variance of the linear model with a 2^3 design')
xlabel('x_1')
ylabel('x_2')
zlabel('y')

hold on
plot(Efs(:,1),Efs(:,2),'ok','MarkerFaceColor','red')
hold off
% modèle avec interactions
Xfs2=x2fx(Efs,"interaction");
Vfs2=f2/(Xfs2.'*Xfs2)*f2.'
fsurf(x1,x2,Vfs2,[-1,1,-1,1])
title('Variance of the interaction model with a 2^3 design')
xlabel('x_1')
ylabel('x_2')
zlabel('y')

hold on
plot(Efs(:,1),Efs(:,2),'ok','MarkerFaceColor','red')
hold off

%
%%