MATLAB: Problem in creating animation plot 2D

animation plot 2d

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

Best Answer

Put figure out side of the for loops
figure('color','white');