Help with implementing viscircles

조회 수: 7 (최근 30일)
Crazy duck
Crazy duck 2020년 10월 16일
답변: Aashray 2025년 4월 28일
Hello, I'm wondering if there is a simple way to add a trajectory to all of my vectors in my last plot. I'm not the brightest with MATLAB and I have been trying to play around with viscircles already, I just dont know how I would implement it into my code correctly, specficially I don't exactly know how I would be able to update each circle continuously throughout all of the vectors complete motion.
For reference, I have attached what my current plot looks like, the second image shows what I am trying to achieve.
Thank you for your time and any help.
Current Plot:
What I am trying to achieve (trajectories of each line):
%%Loading 2D Line Data
clear; close all
s = load('coord.mat');
position = complex(s.coord(:,1),s.coord(:,2));
Ndata = length(position); % Number of Data Point
Ncoef = 20;
%% CFFT
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);
%% Inverse Fourier Transform and Calculation of Reproduced Coordinate Points
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_Ncoef = sum(pos_NcoefMatrix,2); % pos_Ncoef is same as this equation.
%% Drawing Reproduced Coordinate Points
figure(1)
plot(real(position),imag(position),'r');
hold on
plot(real(pos_Ncoef),imag(pos_Ncoef),'b');
hold off
axis equal
ax=gca;
xlim([-1.2 1.2])
ylim([-1.3 1.1])
legend("Origial","Fourier Series Iteration")
%% Vector Display of Each Coefficient Component
figure(2);
Rg=plot(real(position),imag(position),'LineWidth',2);
hold on
axis equal
ax=gca;
% Cumulative sum of each coefficient component is taken from the lower frequency.
pos_cumsum = cumsum(pos_NcoefMatrix,2);
x0 = real(pos_cumsum(1,1:end-1)); % origin x
y0 = imag(pos_cumsum(1,1:end-1)); % origin y
u0 = real(pos_NcoefMatrix(1,2:end)); % x elements
v0 = imag(pos_NcoefMatrix(1,2:end)); % y elements
h2=quiver(x0, y0, u0, v0,...
'AutoScale','off',...
'LineStyle','-',...
'LineWidth',0.5,...
'Color',[0 0 0],...
'MaxHeadSize',1);
%% Moving Display of Each Coefficient Component
Z=plot(position(1),'r','LineWidth',1.5); % Reference line
legend("Origial Plot","Fourier Series Vectors","Fourier Series Plot")
for iter = 1:numel(position)-1
xIter = real(pos_cumsum(iter,1:end-1)); % origin x
yIter = imag(pos_cumsum(iter,1:end-1)); % origin y
uIter = real(pos_NcoefMatrix(iter,2:end)); % x element
vIter = imag(pos_NcoefMatrix(iter,2:end)); % y element
zeta = [uIter vIter];
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));
drawnow
end

답변 (1개)

Aashray
Aashray 2025년 4월 28일
Hello @Crazy duck,
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:
  1. “plot” : https://www.mathworks.com/help/matlab/ref/plot.html
  2. gobjects”: https://www.mathworks.com/help/matlab/ref/gobjects.html
  3. drawnow” : https://www.mathworks.com/help/matlab/ref/drawnow.html

카테고리

Help CenterFile Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by