Making a function to plot SVD

11 Ansichten (letzte 30 Tage)
Larry Ellison
Larry Ellison am 1 Mär. 2021
Bearbeitet: Aashray am 26 Mär. 2025
Write a Matlab function that, given a real 2×2 matrixAand a figure id, plots the right singularvectorsv1andv2and the unit circle in one subplot and the left singular vectorsu1andu2and thecorresponding ellipse in another subplot
Here is the basic outline of the code that i do have.
function plot_svd(A, figId)
Step 1: calc and plot the SVD of A
figure(figId)
subplot(1, 2, 1)
subplot(1, 2, 2)
Step 2: plot the unit circle and the corresponding ellipses
figure(figId)
subplot(1, 2, 1)
hold on;
subplot(1, 2, 2)
I really need help.

Antworten (1)

Aashray
Aashray am 26 Mär. 2025
Bearbeitet: Aashray am 26 Mär. 2025
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:
  1. “svd” function: https://www.mathworks.com/help/matlab/ref/double.svd.html
  2. “figure” function: https://www.mathworks.com/help/matlab/ref/figure.html
  3. “subplot” function: https://www.mathworks.com/help/matlab/ref/subplot.html
  4. “quiver” function: https://www.mathworks.com/help/matlab/ref/quiver.html

Kategorien

Mehr zu Linear Algebra finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by