% Scoring
%
%  Failing to account for equal singular values, -1.
%  Wrong formula for x, up to -4.



function x = EllipsePointCVL(A,lambda)
% A is a 2x2 matrix and lambda satisfies 0<=lambda<=1.
% x is a 2-vector with unit 2-norm length with the property that
%          norm(A*x) = lambda*s1 + (1-lambda)*s2
% where s1 and s2 are the largest and smallest singular values of A.

% Compute the SVD...
[U,S,V] = svd(A);
sig1 = S(1,1); v1 = V(:,1);
sig2 = S(2,2); v2 = V(:,2);

% Let x = c*v1 + s*v2
% From the handout we know norm(Ax)^2 = (c*sig1)^2 + (s*sig2)^2 =
%        c^2(sig1^2-sig2^2) + sig2^2.
% Using the equation norm(Ax)^2 = (lambda*sig1 + (1-lambda)*sig2)^2
%    we conclude that 
if sig1>sig2
   c = sqrt( ((lambda*sig1+(1-lambda)*sig2)^2 - sig2^2)/(sig1^2 - sig2^2));
   x = c*v1 + sqrt(1-c^2)*v2;
else
   % Any unit vector works so..
   x = [1;0];
end
