Upload
asad-naeem
View
226
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Programming
Citation preview
Lecture 7: Error in Simulations
Lecture 7
Bouncing Ball
Floor level
Bouncing Ball with Gravity - 1%% initializations
figure; axis equal; axis([0 100 0 100]);
curLoc = [50, 80];
curVel = [0.9, -0.1];
radius = 2;
hBall = rectangle('position', [curLoc, 2*radius, 2*radius], ...
'curvature', [1 1], 'facecolor', [0.5 1 0.9]);
%% main loop
while 1
%update position and draw
curVel(2) = curVel(2) - 0.1;
curLoc = curLoc + curVel;
set(hBall, 'position', [curLoc, 2*radius, 2*radius]);
%handle collisions
if (curLoc(1) > (100 - 2 * radius)) | (curLoc(1) < 0)
curVel(1) = -curVel(1);
end;
if (curLoc(2) > (100 - 2* radius)) | (curLoc(2) < 0)
curVel(2) = -curVel(2);
end;
pause(0.0001);
end;
Why Does the Ball Loose Energy?
The code is written
such that the velocity of
each location is
assumed as constant for
this interval, where as in
reality it is not.
Results in incorrect
estimation
Bouncing Ball with Gravity - 1%% initializations
figure; axis equal; axis([0 100 0 100]);
curLoc = [50, 80];
curVel = [0.9, -0.1];
radius = 2;
hBall = rectangle('position', [curLoc, 2*radius, 2*radius], ...
'curvature', [1 1], 'facecolor', [0.5 1 0.9]);
%% main loop
while 1
%update position and draw
curVel(2) = curVel(2) - 0.1;
curLoc = curLoc + curVel;
set(hBall, 'position', [curLoc, 2*radius, 2*radius]);
%handle collisions
if (curLoc(1) > (100 - 2 * radius)) | (curLoc(1) < 0)
curVel(1) = -curVel(1);
end;
if (curLoc(2) > (100 - 2* radius)) | (curLoc(2) < 0)
curVel(2) = -curVel(2);
end;
pause(0.0001);
end;
Velocity update
Position update
Example: Update vy before y Let
y = 9 and vy = -3.9
Blow up the picture
After every 0.2 sec vy = vy -0.02 = -3.92 y = 9.00 3.92*0.2 = 8.22
vy = vy -0.02 = -3.94 y = 8.22 3.94*0.2 = 7.42
vy = vy -0.02 = -3.96 y = 7.42 3.96*0.2 = 6.34
vy = vy -0.02 = -3.98 y = 6.34 3.98*0.2 = 5.84
vy = vy -0.02 = -4.00 y = 5.84 4.00*0.2 = 5.04
Total deduction: 3.96
After 1 sec vy = vy -0.1 = -4
y = y + vy = 9 4 = 5.0
Bouncing Ball with Gravity - 1
Bouncing Ball with Gravity - 2%% initializations
figure; axis equal; axis([0 100 0 100]);
curLoc = [50, 80];
curVel = [0.9, -0.1];
radius = 2;
hBall = rectangle('position', [curLoc, 2*radius, 2*radius], ...
'curvature', [1 1], 'facecolor', [0.5 1 0.9]);
%% main loop
while 1
%update position and draw
curLoc = curLoc + curVel;
curVel(2) = curVel(2) - 0.1;
set(hBall, 'position', [curLoc, 2*radius, 2*radius]);
%handle collisions
if (curLoc(1) > (100 - 2 * radius)) | (curLoc(1) < 0)
curVel(1) = -curVel(1);
end;
if (curLoc(2) > (100 - 2* radius)) | (curLoc(2) < 0)
curVel(2) = -curVel(2);
end;
pause(0.0001);
end;
Position update
Velocity update
Example: Update y before vy Let
y = 9 and vy = -3.9
Blow up the picture
After every 0.2 sec vy = vy -0.02 = -3.92 y = 9.00 3.92*0.2 = 8.22
vy = vy -0.02 = -3.94 y = 8.22 3.94*0.2 = 7.42
vy = vy -0.02 = -3.96 y = 7.42 3.96*0.2 = 6.34
vy = vy -0.02 = -3.98 y = 6.34 3.98*0.2 = 5.84
vy = vy -0.02 = -4.00 y = 5.84 4.00*0.2 = 5.04
Total deduction: 3.96
After 1 sec y = y + vy = 9 3.9 = 5.1
vy = vy -0.1 = -4
Bouncing Ball with Gravity - 2
Bouncing Ball with Gravity
Update vy before y Update y before vy
vy = vy -0.1 = -4
y = y + vy = 9 4 = 5.0
y = y + vy = 9 3.9 = 5.1
vy = vy -0.1 = -4
Bouncing Ball with Gravity Let
y = 9 and vy = -3.9
After 1 sec Update vy before y
vy = vy -0.1 = -4
y = y + vy = 9 4 = 5.0
Update y before vy y = y + vy = 9 3.9 = 5.1
vy = vy -0.1 = -4
Actual y should have been y = 5.04
Mean of velocities i.e. mean of initial and final velocities vi = vy vf = vy 0.1
Vmean = (vi + vf )/2
= (vy + vy 0.1)/2
= (2vy 0.1)/2
= -3.95
y = y + vy = 7 3.95 = 5.05
Bouncing Ball with Gravity - 3%% initializations
figure; axis equal; axis([0 100 0 100]);
curLoc = [50, 80];
curVel = [0.9, -0.1];
radius = 2;
hBall = rectangle('position', [curLoc, 2*radius, 2*radius], ...
'curvature', [1 1], 'facecolor', [0.5 1 0.9]);
%% main loop
while 1
%update position and draw
curLoc = curLoc + (2*curVel - [0,0.1])/2;
curVel(2) = curVel(2) - 0.1;
set(hBall, 'position', [curLoc, 2*radius, 2*radius]);
%handle collisions
if (curLoc(1) > (100 - 2 * radius)) | (curLoc(1) < 0)
curVel(1) = -curVel(1);
end;
if (curLoc(2) > (100 - 2* radius)) | (curLoc(2) < 0)
curVel(2) = -curVel(2);
end;
pause(0.0001);
end;
Position update
with mean velocity
Velocity update
Next: Errors in Physical Simulations
Still the simulation is not perfectly accurate
Note what happens at the time of the bounce from the floor
Floor level
Bouncing Ball
When center of ball goes outside the board (light blue), we have to test bounce from top, left,
right or bottom
Careful Bounces Left wall
At the end of a time step the ball bounces off of the left wall.
Its horizontal velocity becomes opposite of what it was
Its position also is reflected off the left wall.
Careful Bounces
Examples So new value of x should
be 28
Additional distance
wall x = 20 12 = 8
New value
wall +8 = 28
So
x = wall + (wall-x)
x = 20
x =12 x =28
Careful Bounces
Examples So new value of x should
be 8
Additional distance
wall x = 0 (-8) = 8
New value
wall + (wall x) = 0 +8 = 8
So
x = wall + (wall-x)
x = 0
x =-8 x =8
Careful Bounces Right wall
Examples Additional distance
x+2*r - wall
Updated value
wall - (x+2*r wall)
Left corner Additional -2*r
So
x = wall - (x+2*r - wall ) 2*r
Careful Bounces Left Wall
x =wall + (wall x)
Bottom Wall y =wall + (wall y)
Right wall x = wall - (x+2*r - wall ) 2*r
Top Wall y = wall (y+2*r - wall ) 2*r
Bouncing Ball Main Loopwhile 1
%update position and draw
curLoc = curLoc + curVel;
%handle collisions
if (curLoc(1) < 0)
curLoc(1) = -curLoc(1);
curVel(1) = -curVel(1);
elseif (curLoc(1) > (100 - 2 * radius))
curLoc(1) = 100 - (curLoc(1) + 2*radius - 100) - 2*radius;
curVel(1) = -curVel(1);
end;
if (curLoc(2) < 0)
curLoc(2) = - curLoc(2);
curVel(2) = -curVel(2);
elseif (curLoc(2) > (100 - 2* radius))
curLoc(2) = 100 - (curLoc(2) + 2*radius - 100) - 2*radius;
curVel(2) = -curVel(2);
end;
set(hBall, 'position', [curLoc, 2*radius, 2*radius]);
pause(0.0001);
end;
Typical Structure
Initialize graphics Display scene at t=0
Begin loop e.g. for t = 0.05:0.05:10 Compute scene at time t
pause(0.05)
Display the scene
EndTime limit of up to 1/20th of a
second for real-time graphics
For complex scenes, this could take
a large amount of time
and could vary
Better Structure
Initialize graphics
Start Stopwatch
for t = 0:0.05:10 Compute scene at time t
pause(0.05) Wait for real-time to catch up to simulation time
Display the scene
End
Better Structure
Initialize graphics
tic
for t = 0:0.05:10 Compute scene at time t
pause(t toc)
Display the scene
End