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

16 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);
end
Here is my code so far:
clear;
clc;
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;
figure(1);
for i=1:n
plot(XY(1,i),XY(2,i),'Ob','MarkerSize',6,'MarkerFaceColor','b')
grid on;
hold on
axis([0 1 0 1])
pause(.3)%how fast or slow each point plots
end
%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
plot(AB(1,i),AB(2,i),'+r','MarkerSize',6)
hold on
axis([0 1 0 1])
pause(.3) %how fast or slow each point plots
end
%Goal Spawn is fixed-------------------------------------------------------
c = 0.5;
d = 0.5;
plot(c,d,'dk','Markersize',6,'MarkerFaceColor','k')
%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;
quiver(XY(1,:),XY(2,:),U2,V2,0);
hold on;
XY(1,:) = XY(1,:) + U2*.1;
XY(2,:) = XY(2,:) + V2*.1;
end

Answers (1)

Aghamarsh Varanasi
Aghamarsh Varanasi on 17 Jun 2021
Hi,
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.

Categories

Find more on Vector Fields in Help Center and File Exchange

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!