function P4()
% Tests Der
clc
n = 10;
A = pascal(n);
[U,S,V] = svd(A);
f = V(:,1); g = U(:,1);
Smallestalfa = Der(A,f,g)
f = V(:,n); g = U(:,n);
Largestalfa = Der(A,f,g)


function mu = Der(A,f,g)
% Assume A is n-by-n (nonsingular), f is n-by-1, and f is n-by-1
% Let X(t) be the inverse of A(t) = A + tf*g'
% mu = norm(dX/dt)(0),'fro')

% From the hints, dX/dt = -X(t)f*g'*X(t). Thus, we need the F-norm of 
%
%                 X(0)*f*g'*X(0) = (inv(A)*f)*(g'*inv(A))
%
% if  u = inv(A)*f   then u solves Au = f
% if  v = g'*inv(A)  then v solves A'*v = g

[L,U,P] = lu(A);

% Get u via L*y = P*f and Uu = y...
u = U\(L\(P*f));
% Get v via (P'*L*U)'*v = g, i.e., U'z = g, L'y = z; v = P*y
v = P*(L'\(U'\g));
mu = norm(u*v','fro');  %or equivalently, alfa = norm(u,2)*norm(v,2)

% -2 for computing the inverse, i.e.,
% X = U\(L\(P*eye(n,n))); mu = X*f*g'*X
%-2 for having O(n) system solves, only tworequired

% 1 for upper bound comment
% let A = USV' be svd of A. 
% SVD says largest solution to Au = f if f = V(:,n)
% A' = VSU'
% SVD says largest solution to A'v = g if g = U(:,n) 
% The norm of these largest solutions ar 1/S(n,n)
% So max mu = 1/S(n,n)^2