Using MATLAB for plotting singular vectors is a really good choice as MATLAB provides you with pre-implemented “svd” function which returns you the U, S, and V metrices. Also, it helps you easily plot the obtained vectors. You may refer to the following piece of code for plotting the same:
function plot_svd(A, figId)
% Calculating the SVD of A
[U, S, V] = svd(A);
% Creating the figure
figure(figId);
% Subplot 1: Plotting the right singular vectors on the unit circle
subplot(1, 2, 1);
hold on;
theta = linspace(0, 2*pi, 100);
plot(cos(theta), sin(theta), 'k--', 'LineWidth', 1);
quiver(0, 0, V(1, 1), V(2, 1), 0, 'LineWidth', 2, 'MaxHeadSize', 1, 'Color', 'r');
quiver(0, 0, V(1, 2), V(2, 2), 0, 'LineWidth', 2, 'MaxHeadSize', 1, 'Color', 'b');
axis equal;
xlim([-1.5, 1.5]);
ylim([-1.5, 1.5]);
title('Right Singular Vectors and Unit Circle');
xlabel('x');
ylabel('y');
legend('Unit Circle', 'v_1', 'v_2');
% Subplot 2: Plotting the left singular vectors and the corresponding ellipse
subplot(1, 2, 2);
hold on;
angle = linspace(0, 2*pi, 100);
ellipse_x = S(1, 1) * cos(angle); % Major axis
ellipse_y = S(2, 2) * sin(angle); % Minor axis
plot(ellipse_x, ellipse_y, 'k--', 'LineWidth', 1);
quiver(0, 0, U(1, 1), U(2, 1), 0, 'LineWidth', 2, 'MaxHeadSize', 1, 'Color', 'r');
quiver(0, 0, U(1, 2), U(2, 2), 0, 'LineWidth', 2, 'MaxHeadSize', 1, 'Color', 'b');
axis equal;
xlim([-1.5, 1.5] * max(S(:)));
ylim([-1.5, 1.5] * max(S(:)));
title('Left Singular Vectors and Ellipse');
xlabel('x');
ylabel('y');
legend('Ellipse', 'u_1', 'u_2');
hold off;
end
The above function can be called by just passing a matrix A as the input argument and any desired figure id:
A = [1,2;3,4];
figId = 2;
plot_svd(A, figId);
The “quiver” function used in the above function is used for creating a quiver plot and displays vectors in the form of arrows. It requires a tuple as starting coordinates (x, y) and another tuple for vector components (u, v) in the x and y directions.
The below plot is the desired plot:

The following documentation links might be helpful:
- “svd” function: https://www.mathworks.com/help/matlab/ref/double.svd.html
- “figure” function: https://www.mathworks.com/help/matlab/ref/figure.html
- “subplot” function: https://www.mathworks.com/help/matlab/ref/subplot.html
- “quiver” function: https://www.mathworks.com/help/matlab/ref/quiver.html
