% "Matrix times circle equals ellipse": 

% Generate a circle of points with 1 degree separation
circ = zeros(2, 360); %a 2×360 zero array
deg = [ 1 : 1 : 360 ];
circ(1, :) = cos(deg*pi / 180); %pi is a built-in constant
circ(2, :) = sin(deg*pi / 180);

% Define A to be your nonsingular 2×2 matrix 
% (do not choose the identity matrix, it is not interesting.)
A = [ 0.5 -2; -1 0.3]; % for instance ...

% Now compute the SVD of A:
[U, S, V] = svd(A);

% plot A times circle as follows:
hold on % concatenate all subsequent plots on the same graph
axis equal
plot(circ(1,:), circ(2,:), 'o');
Acirc = A*circ;
plot(Acirc(1,:), Acirc(2,:), 'o');
% plot original basis vectors
origBasis = [ V(:,1) [ 0 0 ]' V(:,2) ]; 
plot(origBasis(1,:), origBasis(2,:), '--');

% The major and minor axes of the ellipse are given by 
% the left singular vectors of A (the columns of U) or, 
% equivalently, by the eigenvectors of AA'.
% Add them to your plot as follows:
U_line = [ U(:,1)*S(1,1) [ 0 0 ]' U(:,2)*S(2,2) ]; 
plot(U_line(1,:), U_line(2,:));

basis = [ V(:,1) [ 0 0 ]' V(:,2) ]; 
plot(basis(1,:), basis(2,:), '--');

hold off


