I think i have problem with pause command
이전 댓글 표시
Hello dudes, i make a small animation on matlab about a body that start from Y=0 with initial velocity. My problem is that on the paper maths shows that the total time of the animation will be 4sec but when i run the programm does seem to be same running time. I think that my error is on the pause command but i can't solve it
tspan =0:0.01:4;
y0 = 0;
[t,y] = ode45(@(t,y) -10*t+20, tspan, y0);
plot(t,y)
grid on
x1=[0, 0, 1, 1];
y1=[0, 0.5, 0.5, 0];
x2=[0, 0, 10, 10];
y2=[0, 0.01, 0.01, 0];
fill(x2,y2,'black');
hold on
axis([0 10 -5 25])
grid on
time=0
z=1
while time<=400
time=time+1;
ypsos=y(z)
p=fill(x1,y1+ypsos,[0 0.41 0.53])
pause(0.009)
drawnow
delete(p);
z=z+1;
end
채택된 답변
추가 답변 (2개)
Luciano Garim
2020년 10월 8일
To solve your problem, you may use the conditional command "if" and "return". Try to execute this code now.
tspan =0:0.01:4;
y0 = 0;
[t,y] = ode45(@(t,y) -10*t+20, tspan, y0);
plot(t,y)
grid on
x1=[0, 0, 1, 1];
y1=[0, 0.5, 0.5, 0];
x2=[0, 0, 10, 10];
y2=[0, 0.01, 0.01, 0];
fill(x2,y2,'black');
hold on
axis([0 10 -5 25])
grid on
time=0
z=1
while time<=1000
time=time+1;
if time>400
return;
end
ypsos=y(z)
p=fill(x1,y1+ypsos,[0 0.41 0.53])
pause(0.009)
drawnow
delete(p);
z=z+1;
end
I hope helped you!
댓글 수: 1
Walter Roberson
2020년 10월 8일
return is not needed. If you want to stop at 400, just change the while limit. if / return approach here has nothing to recommend it over the approach the original person used.
sim ts
2020년 10월 9일
0 개 추천
댓글 수: 3
Walter Roberson
2020년 10월 9일
편집: Walter Roberson
2020년 10월 9일
pause() has no effect on the calculation other than to delay it.
pause() has an effect on the plotting, in that it has the side effect of triggering immediate drawing.
WIth the code I posted, you get an animation of a bar floating upwards and back downwards.
when i measue the time it doesn't corresponding on my result
How are you measuring time?
Are you trying to animate in "real time", something displaying in the screen at elapsed time according to t(time) ?
sim ts
2020년 10월 9일
sim ts
2020년 10월 9일
카테고리
도움말 센터 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!