How can I create an animation from a quiver plot into a separate figure?

35 views (last 30 days)
I have a code to show velocity of blue points focused towards a point of interest which is the red plus. I am able to view the quiver normally, but would like to add movement in another figure. Am I able to see the blue points follow this path to the red plus? How would I achieve this?
You will need objectFlow.m:
function [Vxi,Vyi,Psi,Phi] = objectFlow(xi,yi,ui,vi,S,X,Y)
% doubletFlow Calculates the x and y component of the path vector due to a
% doublet flow
% Uses Laplacian Transforms to calculate x and y components from global
% corrdinates X,Y, the source location xi,yi, and strength S
r = sqrt((X-xi).^2+(Y-yi).^2);
theta = atan2(Y-yi,X-xi)-atan2(vi,ui);
Vr = -S.*cos(theta)./(2.*pi.*r.^2);
Vt = -S.*sin(theta)./(2.*pi.*r.^2);
Vxi = Vr.*cos(theta)+Vt.*cos(theta+pi/2);
Vyi = Vr.*sin(theta)+Vt.*sin(theta+pi/2);
Psi = -S.*sin(theta)./(2.*pi.*r);
Phi = (S.*cos(theta))./(2.*pi.*r);
Here is my code so far:
close all;
%Robot Team Spawn is random------------------------------------------------
n = 10; %Number of Robots
XY = 1 * rand(2,n); %Next point, right now completely random
f1 = figure;
f2 = figure;
for i=1:n
grid on;
hold on
axis([0 1 0 1])
pause(.3)%how fast or slow each point plots
%Invader Team Spawn is random----------------------------------------------
a = 1; %Number of Invaders
AB = 1 * rand(2,a); %Next point, right now completely random
for i=1:a
hold on
axis([0 1 0 1])
pause(.3) %how fast or slow each point plots
%Goal Spawn is fixed-------------------------------------------------------
c = 0.5;
d = 0.5;
%Quiver Plot---------------------------------------------------------------
U = 1*(AB(1) - .5);
V = 1*(AB(2) - .5);
for t = 1:200
[U2,V2,PSI11,PHI2] = objectFlow(AB(1),AB(2),U,V,.1,XY(1,:),XY(2,:));
hold on;
hold on;
XY(1,:) = XY(1,:) + U2*.1;
XY(2,:) = XY(2,:) + V2*.1;

Answers (1)

Aghamarsh Varanasi
Aghamarsh Varanasi on 17 Jun 2021
To create an animation by plotting random points, you need to capture each frame of the figure and write to a 'gif' file. For more information and sample code you can refer to this community post.
Note that: you might want to set the x, y axis limit using xlim, ylim to set the frame as a constant. If the limits also tend to me random, you can omit line 2 of the script in the blog post.


Find more on Vector Fields in Help Center and File Exchange





Community Treasure Hunt

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

Start Hunting!

Translated by