problem in creating animation plot 2D
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
更多回答(1 个)
Shivaraj Durairaj
2020-2-12
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 个评论
Shivaraj Durairaj
2020-2-12
编辑:Shivaraj Durairaj
2020-2-12
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!