Bouncing Ball Avi Problem

2 次查看(过去 30 天)
Matthew Alsheikh
Matthew Alsheikh 2011-10-10
Hello all,
So my assignment was to implement RK2 to solve the DE: dv=dt = g v(0) = v0
And make it play in an .avi. I believe I've done everything correctly except implementing the whole movie part of it. Here's my code: % Boolean turned on for making a movie: Make_Movie=1;
% Geometry of the closed container:
Right_Wall = 1;
Left_Wall = 1;
Bottom_Wall = 1;
Top_Wall = 1;
% Physical parameters:
g =.0981;
alpha=1.2 ;
beta =.99 ;
% Acceleration:
acceleration_x= 0;
acceleration_y=- g;
% Parameters for time evolution:
final_time=100;
dt =.03;
% Initial time:
t=0;
% Initial velocity vector:
v_hor= .3;
v_ver= 0;
% Original position of the ball:
Radius=.05 ;
x=.5 ;
y= 1-Radius;
count=0;
if Make_Movie
fig=figure;
aviobj = avifile('Bouncing_Ball.avi');
end
while t<final_time && count < 2000
if t+dt>final_time
dt=final_time-t;
end
t=t+dt;
v_hor=v_hor+acceleration_x*dt;
v_ver=v_ver+acceleration_y*dt;
% Treatment at the bottom wall:
if (y+v_ver*dt) < Bottom_Wall+Radius && Bottom_Wall % would cross the wall.
% Position the object on the wall and Draw:
x=x + v_hor*dt;
y=Bottom_Wall+Radius;
Draw_Disk(x,y,Radius); axis([0 1, 0 1]); pause(dt/10);
if Make_Movie
F = getframe(fig); aviobj = addframe(aviobj,F);
end
% Define the velocity to reflect how the ball is bouncing off:
v_ver=-alpha*v_ver;
v_hor= beta*v_hor;
end
% Treatment of the other walls:
%Treatment at the Right wall:
if (y+v_hor*dt) < Right_Wall+Radius && Right_wall
%Position object on wall & Draw
y=y + v_ver*dt;
x=Right_Wall+Radius;
Draw_Disk(x,y,Radius); axis([0 1, 0 1]); pause(dt/10);
if Make_Movie
F = getframe(fig); aviobj = addframe(aviobj,F);
end
% Define velocity to reflect ball bouncing off:
v_ver=alpha*v_ver;
v_hor=-beta*v_hor;
end
%Treatment of Left wall:
if(y+v_hor*dt) < Left_Wall+Radius && Left_wall
y = y+v_ver*dt;
x=Left_Wall+Radius;
Draw_Disk(x,y,Radius); axis([0 1, 0 1]); pause(dt/10);
if Make_Movie
F = getframe(fig); aviobj = addframe(aviobj,F);
end
% Define velocity to reflect ball bouncing off:
v_ver=alpha*v_ver;
v_hor=-beta*v_hor;
end
%Treatment of Top Wall:
if (y+v_ver*dt) < Top_Wall + Radius && Top_Wall
x = c+v_hor*dt;
y=Top_Wall+Radius;
Draw_Disk(x,y,Radius); axis([0 1, 0 1]); pause(dt/10);
if Make_Movie
F = getframe(fig); aviobj = addframe(aviobj,F);
end
%Define Velocity to reflect ball bouncing off:
v_ver=-alpha*v_ver;
v_hor=beta*v_hor;
end
% General case, i.e. when there is no wall:
x=x+v_hor*dt;
y=y+v_ver*dt;
Draw_Disk(x,y,Radius); axis([0 1, 0 1]); pause(dt/10);
if Make_Movie
F = getframe(fig); aviobj = addframe(aviobj,F);
end
count=count+1;
end
if Make_Movie
close(fig);
aviobj = close(aviobj);
end
The problem I'm getting is a blank 1x1 graph is showing up and that's all. After I quit out of the program, this error pops up: ??? Operation terminated by user during ==> getframe at 35
In ==> HW2 at 109 F = getframe(fig); aviobj = addframe(aviobj,F);
Any ideas? P.S. I'm using 7.10.0 if that makes a difference.

回答(1 个)

Walter Roberson
Walter Roberson 2011-10-10
You do not happen to include the code for Draw_Disk, but in context it appears it is supposed to create a graphic of some sort. You do not, however, pass fig in to it, and you do not tell the axes() call immediately after that to work on fig.
It is always safer to specifically create your figure, and to specifically create your axes against the parent figure, and to specifically parent your graphic against that axes. For example,
fig = figure();
ax = axes(fig, ....);
plot(ax, rand(5,10));

类别

Help CenterFile Exchange 中查找有关 General Applications 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by