problem in creating animation plot 2D
2 views (last 30 days)
Show older comments
jakaria babar
on 12 Feb 2020
Edited: Shivaraj Durairaj
on 12 Feb 2020
I am getting huge number of figure.but not the animation plot.What is the problem in my code? Can you please help me?
%% Animation of a graph a point P(x,y) which is variable connected to three fixed point A(6,9),B(20,38),C(49,25).
clc
clear
close all
M=5000;
x=6:.01:49; % domain x.
y=9:.01:38; % domain y.
for i=1:length(x)
for j=1:length(y)
L=sqrt((x(i)-6)^2+(y(j)-9)^2)+sqrt((x(i)-20)^2+(y(j)-38)^2)+sqrt((x(i)-49)^2+(y(j)-25)^2);
if L<M
M=L;
a=x(i);
b=y(j);
p = [6 20 49]; % x coordinates of three fixed points A, B, and C.
q = [9 38 25]; % y coordinates of three fixed points A, B, and C.
figure('color','white');
hold on;
for n = 1:length(p)
plot([p(n) a],[q(n) b],'color',[0 0 1]);
myChar = char(64+n);
text(p(n)+1,q(n),myChar);
end
plot(p,q,'o','markerfacecolor','k','markeredgecolor','none', ...
'markersize',4);
plot(a,b,'o','markerfacecolor','k','markeredgecolor','none', ...
'markersize',8);
text(a+0.5,b+0.75,'P');
xlabel('x');
ylabel('y');
drawnow
pause = .01;
end
end
end
0 Comments
Accepted Answer
More Answers (1)
Shivaraj Durairaj
on 12 Feb 2020
You must create the figure window outside the for loops.
Use hold on after every plot function except the last plot function to retain the plots for the current frame of the animation. Use hold off after the last plot function to clear the current frame and add plots to the next frame of the animation.
%% Animation of a graph a point P(x,y) which is variable connected to three fixed point A(6,9),B(20,38),C(49,25).
clc
clear
close all
M=5000;
x=6:.1:49; % domain x.
y=9:.1:38; % domain y.
figure('color','white'); % Create figure window
for i=1:length(x)
for j=1:length(y)
L=sqrt((x(i)-6)^2+(y(j)-9)^2)+sqrt((x(i)-20)^2+(y(j)-38)^2)+sqrt((x(i)-49)^2+(y(j)-25)^2);
if L<M
M=L;
a=x(i);
b=y(j);
p = [6 20 49]; % x coordinates of three fixed points A, B, and C.
q = [9 38 25]; % y coordinates of three fixed points A, B, and C.
for n = 1:length(p)
plot([p(n) a],[q(n) b],'color',[0 0 1]);
hold on % hold on
myChar = char(64+n);
text(p(n)+1,q(n),myChar);
end
plot(p,q,'o','markerfacecolor','k','markeredgecolor','none', ...
'markersize',4);
hold on % hold on
plot(a,b,'o','markerfacecolor','k','markeredgecolor','none', ...
'markersize',8);
hold off % hold off
text(a+0.5,b+0.75,'P');
xlabel('x');
ylabel('y');
drawnow
pause = .01;
end
end
end
2 Comments
Shivaraj Durairaj
on 12 Feb 2020
Edited: Shivaraj Durairaj
on 12 Feb 2020
I assume that you want to create a video file from this animation. You can do that by following the steps in the Create AVI File from Animation example.
See Also
Categories
Find more on Animation 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!