According to my understanding of your question, you want to visualize the trajectory (the circular paths) of each rotating vector in the Fourier series animation. This will help to visualize how each harmonic contributes to the final path over time.
To achieve this, we can add a circular trail to every vector using MATLAB plotting functions. We can precompute the circle points using “sin” and “cos” and use the “plot” function to draw each circle in real time. Also one enhancement is storing the plot handles in “gobjects” so that they get updated efficiently within the loop.
You may refer to the code below for more clarity:
%% Load 2D Line Data
clear; close all
s = load('coord.mat');
position = complex(s.coord(:,1), s.coord(:,2));
Ndata = length(position); % Number of Data Points
Ncoef = 20;
%% FFT and Extract Coefficients
tmp_coef = fft(position);
idx1 = 1:Ncoef/2;
idx2 = Ndata:-1:Ndata-Ncoef/2+1;
tmp_idx = [idx1;idx2];
k_idx = tmp_idx(:)';
Fcoef = tmp_coef(k_idx);
%% Precompute Points from Inverse FFT
pos_Ncoef = zeros(Ndata,1);
pos_NcoefMatrix = zeros(Ndata, Ncoef);
for jj = 1:Ndata
pos_Ncoef(jj) = (exp(1i*2*pi*(k_idx-1)*(jj-1)/Ndata) * Fcoef) / Ndata;
pos_NcoefMatrix(jj,:) = (exp(1i*2*pi*(k_idx-1)*(jj-1)/Ndata) .* Fcoef.') / Ndata;
end
pos_cumsum = cumsum(pos_NcoefMatrix, 2);
%% Set Up Plot with Initial Vectors
figure(2); clf
plot(real(position), imag(position), 'k', 'LineWidth', 1.5); hold on
axis equal
xlim([-2 2]); ylim([-2 2]);
legend("Original Plot", "Fourier Series Vectors", "Fourier Series Plot");
% Initial vector display
x0 = real(pos_cumsum(1,1:end-1));
y0 = imag(pos_cumsum(1,1:end-1));
u0 = real(pos_NcoefMatrix(1,2:end));
v0 = imag(pos_NcoefMatrix(1,2:end));
h2 = quiver(x0, y0, u0, v0, ...
'AutoScale', 'off', ...
'LineStyle', '-', ...
'LineWidth', 0.5, ...
'Color', [0 0 0], ...
'MaxHeadSize', 1);
Z = plot(real(pos_Ncoef(1)), imag(pos_Ncoef(1)), 'r', 'LineWidth', 1.5);
%% Add Initial Trajectory Circles <-- NEW BLOCK
theta = linspace(0, 2*pi, 100);
numCircles = Ncoef - 1;
circleHandles = gobjects(numCircles, 1);
for k = 1:numCircles
radius = abs(pos_NcoefMatrix(1,k+1));
cx = real(pos_cumsum(1,k));
cy = imag(pos_cumsum(1,k));
xCirc = radius * cos(theta) + cx;
yCirc = radius * sin(theta) + cy;
circleHandles(k) = plot(xCirc, yCirc, '-', 'Color', [0.2 0.8 0.2]); % greenish
end
%% Animate Vectors and Circles <-- MODIFIED BLOCK
for iter = 1:Ndata
xIter = real(pos_cumsum(iter,1:end-1));
yIter = imag(pos_cumsum(iter,1:end-1));
uIter = real(pos_NcoefMatrix(iter,2:end));
vIter = imag(pos_NcoefMatrix(iter,2:end));
h2.XData = xIter;
h2.YData = yIter;
h2.UData = uIter;
h2.VData = vIter;
Z.XData = real(pos_Ncoef(1:iter));
Z.YData = imag(pos_Ncoef(1:iter));
%% <-- NEW: Update each circle's position
for k = 1:numCircles
radius = abs(pos_NcoefMatrix(iter,k+1));
cx = xIter(k);
cy = yIter(k);
xCirc = radius * cos(theta) + cx;
yCirc = radius * sin(theta) + cy;
circleHandles(k).XData = xCirc;
circleHandles(k).YData = yCirc;
end
drawnow
end
I have marked the modified parts for better understanding.
Finally, the desired plot is obtained.

I am also attaching documentation links of the functions used:


