% Task2

clear all
clc



% Parameters
q1 = 1;
q2 = 3;
q3 = 2;
b1 = 0.5;
b2 = 4;
b3 = 2;


B = [b1 b3; b3 b2];

% % orthonormal basis: 
% G1 = [1 0 ;0 0];
% G2 = [0 0 ;0 1];
% G3 = [0 1/sqrt(2); 1/sqrt(2) 0 ];
% 
% alpha = 2;
% v = @(alpha) [cos(alpha);sin(alpha)];
% 
% v_alpha = v(alpha);
% 
% TP = v_alpha*v_alpha'
% 
% L = stVenant(TP,mu,lambda);
% 
% % Frobenius-product
% Frob = trace(L'*B)
% %compute Q(v_alpha x v_alpha)
% Q = q1*v_alpha(1)^4 + q2*v_alpha(2)^4 + 2*q3*v_alpha(1)^2*v_alpha(2)^2;
% compr = Frob/Q
% 
% % r = @(alpha) trace(stVenant(v(alpha)*v(alpha)',mu,lambda)'*B)/select(v(alpha),1,1)

% r = compute_r(alpha,mu,lambda,B,q1,q2,q3)
% F = compute_F(alpha,mu,lambda,B,q1,q2,q3)

x = 0:0.01:2*pi;




% y = compute_F(x,mu,lambda,B,q1,q2,q3);
y1 = arrayfun(@(alpha)compute_F(alpha,B,q1,q2,q3),x,'UniformOutput', false);
y1 = cell2mat(y1);


% Parameters
q1 = 1;
q3 = 3;
q2 = 4;
b1 = 8;  %4    change if b1 > b2... 
b2 = 4;  %1
b3 = 0;  %0
B = [b1 b3; b3 b2];
y2 = arrayfun(@(alpha)compute_F(alpha,B,q1,q2,q3),x,'UniformOutput', false);
y2 = cell2mat(y2);


% Parameters
q1 = 5;
q3 = 8;
q2 = 9;
b1 = 5;  %2
b2 = 1;  %0.5
b3 = 0;  %0
B = [b1 b3; b3 b2];
y3 = arrayfun(@(alpha)compute_F(alpha,B,q1,q2,q3),x,'UniformOutput', false);
y3 = cell2mat(y3);


% Parameters
q1 =5;
q3 = 8;
q2 = 9;
b1 = 7;   %6-7
b2 = 0.5; %0.5
b3 = 5;   %5
B = [b1 b3; b3 b2];
y4 = arrayfun(@(alpha)compute_F(alpha,B,q1,q2,q3),x,'UniformOutput', false);
y4 = cell2mat(y4);






figure 
plot(x,y1,'b')
hold on 
plot(x,y2,'r','LineStyle' ,'--')
hold on 
plot(x,y3,'g')
hold on 
plot(x,y4,'black','LineStyle' ,':','LineWidth',1.0)
title('plot of F(alpha)')
legend('Setting 1', 'Setting 2', 'Setting 3', 'Setting 4')
xlabel('alpha')
axis([0 2*pi+1 -80 10])
% axis equal 
hold on

% TODO: compute and plot minimizers

fun = @(alpha) compute_F(alpha,B,q1,q2,q3);
[t,f] = fminunc(fun,0)
plot(t,f,'*')
hold on 
% [t,f] = fminunc(fun,pi/2)
% plot(t,f,'*')
% hold on 