I think i have problem with pause command

조회 수: 14 (최근 30일)
sim ts
sim ts 2020년 10월 8일
댓글: Walter Roberson 2020년 10월 9일
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

채택된 답변

Walter Roberson
Walter Roberson 2020년 10월 8일
It is not obvious that you are filling the area that you would want to fill, but this at least finished quickly enough.
tspan =0:0.01:4;
y0 = 0;
[t,y] = ode45(@(t,y) -10*t+20, tspan, y0);
plot(t,y);
axis([0 10 -5 25])
grid on
hold on
drawnow();
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');
grid on
for time = 1 : 400
ypsos=y(time);
p=fill(x1,y1+ypsos,[0 0.41 0.53]);
pause(0.009)
drawnow
delete(p);
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 10월 9일
If the idea is "real time" animation, then:
start = tic;
for time = 1 : 400
ypsos=y(time);
p=fill(x1,y1+ypsos,[0 0.41 0.53]);
if time ~= 400
pause(y(time+1) - toc(start));
end
delete(p);
end
Outputs will tend to be slightly late, but there should not be any cumulative drift as the time is compared to the start time rather than assuming that we started this round on time.
Walter Roberson
Walter Roberson 2020년 10월 9일
cla reset
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
start = tic;
p = fill(x1, y1, [0 0.41 0.53]);
for time = 1 : 400
ypsos=y(time);
p.YData = y1+ypsos;
pause(t(time+1)-toc(start));
end
toc(start)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Luciano Garim
Luciano Garim 2020년 10월 8일
Hi, sim ts.
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
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
sim ts 2020년 10월 9일
I think that on this simple problem it is a litte bit irelevant what kind of loop you will use. My problem is that i want the body to start from Y0=0 --->t0=0 second and to stop again at Yf=0--->tf=4 second but when i measue the time it doesn't corresponding on my result and i try to get smaller pause like pause(0.001) and t doesn't change.
  댓글 수: 3
sim ts
sim ts 2020년 10월 9일
I use Run and Time to measure the duration of the animation. If you see the ode45 plot on x axis is the time and on y is the altitude so by seeing the plot you guess that the object start to moving upwords at t=0 and the maximun altitude is y=20 at t=2 second and the simulation ands when the object reach y=0 at t=4. My problem is that on the animation i can't have the same time and i get bigger.
sim ts
sim ts 2020년 10월 9일
also to measure the time i use tic toc commands
tic
while time<=400
time=time+1;
ypsos=y(z)
p=fill(x1,y1+ypsos,[0 0.41 0.53])
pause(0.01)
drawnow
delete(p);
z=z+1;
end
toc

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by