How can i animate the code below?
조회 수: 2 (최근 30일)
이전 댓글 표시
t1=0:0.01:1;
i=0;
m1=0;
while(i==0)
for i=1:99
y1(i)=1.*t1(i);
end
for i= 100:200
y1(i)=1;
end
y=[zeros(1,200+m1) y1 zeros(1,801-m1)];
%y=y1+y2;
t2=-2:0.01:10;
plot(t,y)
ylim([0 2]);
xlim([-2 10]);
m1=m1+200;
j=input('enter 0 4 times');
end
댓글 수: 2
Akira Agata
2017년 8월 18일
Could you reshape your code to "easy-to-read" style? Trying copy&past your code to MATLAB, but it does not work.
Geoff Hayes
2017년 8월 18일
Souvik - what are you trying to animate? When plot is called, you are passing an invalid parameter t (which doesn't exist). Even if I replace it with t1, it is of a different dimension than y. Please clarify what you are trying to do.
답변 (1개)
MG Poirot
2017년 8월 18일
편집: MG Poirot
2017년 8월 18일
If I'm interpreting your question corretly (and fixed your code properly) I faced the same problem years ago: animating plots withing a for/while loop.
Back then I found a solution with two options. The solution is to call Matlab the draw the figure before the next loop, this can be done either by the drawnow command or the pause(s) command, if you would like to lower the drawspeed.
Your script would look like:
t1=0:0.01:1;
i=0;
m1=0;
while(i==0)
for i=1:99
y1(i)=1.*t1(i);
end
for i= 100:200
y1(i)=1;
end
y=[zeros(1,200+m1) y1 zeros(1,801-m1)];
%y=y1+y2;
t2=-2:0.01:10;
plot(t2,y)
ylim([0 2]);
xlim([-2 10]);
m1=m1+200;
%drawnow
pause(0.1)
i = 0
end
Resulting in the animation of a forward propagating wave. I love to know what you think and solution addresed your question properly.
-mgp
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Animation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!