I think i have problem with pause command
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
sim ts
el 8 de Oct. de 2020
Comentada: Walter Roberson
el 9 de Oct. de 2020
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
0 comentarios
Respuesta aceptada
Walter Roberson
el 8 de Oct. de 2020
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 comentarios
Walter Roberson
el 9 de Oct. de 2020
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
el 9 de Oct. de 2020
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)
Más respuestas (2)
Luciano Garim
el 8 de Oct. de 2020
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 comentario
Walter Roberson
el 8 de Oct. de 2020
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.
Ver también
Categorías
Más información sobre Animation en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!