-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBlogMidpointMethodTimeStep.html
More file actions
413 lines (340 loc) · 11.7 KB
/
Copy pathBlogMidpointMethodTimeStep.html
File metadata and controls
413 lines (340 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<!DOCTYPE html>
<html>
<head>
<title>Encino Sim Class : The Midpoint Method Time Step</title>
<style type="text/css" title="currentStyle">
@import "css/SimClass.css";
</style>
<script src='http://processingjs.org/js/processing.min.js' type='text/javascript'/></script>
<!Install the MathJax stuff so we can show LaTeX>
<script type="text/javascript"
src="https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
</head>
<body>
<h2>Higher-Order Difference Equations</h2>
In our previous class, we were able to fix the instability of our spring
system by switching from the never-correct Forward Euler time step to the
Euler-Cromer time step, which made our simulation stable, but did not achieve
an accurate result over time when compared to the exact answer.
<p>
The solutions we've explored so far are primarily explicit time integrations,
in that we're only using values from previous points in time to solve for
points at future points in time. When we consider, later, implicit time
integration, where values at the current time are solved self-referentially,
things will get a lot more complex. So, we'd like to continue exploring
techniques that are fundamentally explicit in nature.
<p>
The approach that all of the higher-order explicit methods will take will be
to use lower-order approximations to compute temporary values, which will be
used to compute better derivatives that will then be used to integrate forward.
The first, and most common higher-order method is called "The Midpoint Method".
<h2>The Midpoint Method</h2>
Let's approximate the velocity at half-way through our time step, rather than
all the way to the new time, just using the forward finite-difference
approximation we used with Forward Euler:
<div class="LaTexEquation">
\(
a( t ) = f( t, x( t ) ) \\
x( t + \frac{\Delta t}{2} ) \approx x( t ) + \frac{\Delta t}{2} v( t ) \\
v( t + \Delta t ) \approx v( t ) + \Delta t a( t ) \\
x( t + \Delta t ) \approx x( t + \frac{\Delta t}{2} ) + \frac{\Delta t}{2} v( t + \frac{\Delta t}{2} )
\)
</div>
Now we've got one extra step of computation at each time step, computing
a midpoint position. This isn't a huge improvement over what we had before,
and here's what it looks like in code:
<pre><code>// Time Step function.
void TimeStep( float i_dt )
{
// Compute acceleration from current position.
float A = ( -Stiffness / BobMass ) * State[StatePositionX];
// Update position half-way.
State[StatePositionX] += ( i_dt/2.0 ) * State[StateVelocityX];
// Update velocity based on acceleration.
State[StateVelocityX] += i_dt * A;
// Update position half-way.
State[StatePositionX] += ( i_dt/2.0 ) * State[StateVelocityX];
// Update current time.
State[StateCurrentTime] += i_dt;
}
</code></pre>
<p>
And here that is:
<p>
<script type="application/processing" data-processing-target="pjsSpringSimp5">
float Stiffness = 5.0;
float BobMass = 0.5;
int StateSize = 3;
float[] InitState = new float[StateSize];
float[] State = new float[StateSize];
int StateCurrentTime = 0;
int StatePositionX = 1;
int StateVelocityX = 2;
int WindowWidthHeight = 300;
float WorldSize = 2.0;
float PixelsPerMeter;
float OriginPixelsX;
float OriginPixelsY;
void setup()
{
// Create initial state.
InitState[StateCurrentTime] = 0.0;
InitState[StatePositionX] = 0.65;
InitState[StateVelocityX] = 0.0;
// Copy initial state to current state.
// notice that this does not need to know what the meaning of the
// state elements is, and would work regardless of the state's size.
for ( int i = 0; i < StateSize; ++i )
{
State[i] = InitState[i];
}
// Set up normalized colors.
colorMode( RGB, 1.0 );
// Set up the stroke color and width.
stroke( 0.0 );
//strokeWeight( 0.01 );
// Create the window size, set up the transformation variables.
size( WindowWidthHeight, WindowWidthHeight );
PixelsPerMeter = (( float )WindowWidthHeight ) / WorldSize;
OriginPixelsX = 0.5 * ( float )WindowWidthHeight;
OriginPixelsY = 0.5 * ( float )WindowWidthHeight;
textSize( 24 );
}
// Draw our State, with the unfortunate units conversion.
void DrawState()
{
// Compute end of arm.
float SpringEndX = PixelsPerMeter * State[StatePositionX];
// Compute the CORRECT position.
float sqrtKoverM = sqrt( Stiffness / BobMass );
float x0 = InitState[StatePositionX];
float v0 = InitState[StateVelocityX];
float t = State[StateCurrentTime];
float CorrectPositionX = ( x0 * cos( sqrtKoverM * t ) ) +
( ( v0 / sqrtKoverM ) * sin( sqrtKoverM + t ) );
// Compute draw pos for "correct"
float CorrectEndX = PixelsPerMeter * CorrectPositionX;
// Draw the spring.
strokeWeight( 1.0 );
line( 0.0, 0.0, SpringEndX, 0.0 );
// Draw the spring pivot
fill( 0.0 );
ellipse( 0.0, 0.0,
PixelsPerMeter * 0.03,
PixelsPerMeter * 0.03 );
// Draw the spring bob
fill( 1.0, 0.0, 0.0 );
ellipse( SpringEndX, 0.0,
PixelsPerMeter * 0.1,
PixelsPerMeter * 0.1 );
// Draw the correct bob in blue
fill( 0.0, 0.0, 1.0 );
ellipse( CorrectEndX, -PixelsPerMeter * 0.25,
PixelsPerMeter * 0.1,
PixelsPerMeter * 0.1 );
}
// Time Step function.
void TimeStep( float i_dt )
{
// Compute acceleration from current position.
float A = ( -Stiffness / BobMass ) * State[StatePositionX];
// Update position half-way.
State[StatePositionX] += ( i_dt/2.0 ) * State[StateVelocityX];
// Update velocity based on acceleration.
State[StateVelocityX] += i_dt * A;
// Update position half-way.
State[StatePositionX] += ( i_dt/2.0 ) * State[StateVelocityX];
// Update current time.
State[StateCurrentTime] += i_dt;
}
// Processing Draw function, called every time the screen refreshes.
void draw()
{
// Time Step.
TimeStep( 1.0/24.0 );
// Clear the display to a constant color.
background( 0.75 );
// Label.
fill( 1.0 );
text( "Midpoint Method", 10, 30 );
pushMatrix();
// Translate to the origin.
translate( OriginPixelsX, OriginPixelsY );
// Draw the simulation
DrawState();
}
// Reset function. If the key 'r' is released in the display,
// copy the initial state to the state.
void keyReleased()
{
if ( key == 114 )
{
for ( int i = 0; i < StateSize; ++i )
{
State[i] = InitState[i];
}
}
}
</script>
<canvas id="pjsSpringSimp5"> </canvas>
<p>
So really we've actually made things worse with this simple higher-order
approximation. An alternative midpoint-method approach is called
<a href="http://en.wikipedia.org/wiki/Verlet_integration#Velocity_Verlet">
Velocity Verlet</a>, and it looks like this:
<div class="LaTexEquation">
\(
a( t ) = f( t, x( t ) ) \\
v( t + \frac{\Delta t}{2} ) \approx v(t) + \frac{\Delta t}{2} a( t ) \\
x( t + \Delta t ) \approx x( t ) + \Delta t v( t + \frac{\Delta t}{2} ) \\
a( t + \Delta t ) = f( t+\Delta t, x( t + \Delta t ) )\\
v( t + \Delta t ) \approx v( t + \frac{\Delta t}{2} ) + \frac{\Delta t}{2} a( t + \Delta t )
\)
</div>
And here's what that looks like in code:
<p>
<pre><code>
// Time Step function.
void TimeStep( float i_dt )
{
// Compute acceleration from current position.
float A = ( -Stiffness / BobMass ) * State[StatePositionX];
// Update velocity half-way.
State[StateVelocityX] += ( i_dt/2.0 ) * A;
// Update position.
State[StatePositionX] += i_dt * State[StateVelocityX];
// Re-compute acceleration.
A = ( -Stiffness / BobMass ) * State[StatePositionX];
// Update velocity half-way.
State[StateVelocityX] += ( i_dt/2.0 ) * A;
// Update current time.
State[StateCurrentTime] += i_dt;
}
</code></pre>
<p>
And here it is running:
<p>
<script type="application/processing" data-processing-target="pjsSpringSimp6">
float Stiffness = 5.0;
float BobMass = 0.5;
int StateSize = 3;
float[] InitState = new float[StateSize];
float[] State = new float[StateSize];
int StateCurrentTime = 0;
int StatePositionX = 1;
int StateVelocityX = 2;
int WindowWidthHeight = 300;
float WorldSize = 2.0;
float PixelsPerMeter;
float OriginPixelsX;
float OriginPixelsY;
void setup()
{
// Create initial state.
InitState[StateCurrentTime] = 0.0;
InitState[StatePositionX] = 0.65;
InitState[StateVelocityX] = 0.0;
// Copy initial state to current state.
// notice that this does not need to know what the meaning of the
// state elements is, and would work regardless of the state's size.
for ( int i = 0; i < StateSize; ++i )
{
State[i] = InitState[i];
}
// Set up normalized colors.
colorMode( RGB, 1.0 );
// Set up the stroke color and width.
stroke( 0.0 );
//strokeWeight( 0.01 );
// Create the window size, set up the transformation variables.
size( WindowWidthHeight, WindowWidthHeight );
PixelsPerMeter = (( float )WindowWidthHeight ) / WorldSize;
OriginPixelsX = 0.5 * ( float )WindowWidthHeight;
OriginPixelsY = 0.5 * ( float )WindowWidthHeight;
textSize( 24 );
}
// Draw our State, with the unfortunate units conversion.
void DrawState()
{
// Compute end of arm.
float SpringEndX = PixelsPerMeter * State[StatePositionX];
// Compute the CORRECT position.
float sqrtKoverM = sqrt( Stiffness / BobMass );
float x0 = InitState[StatePositionX];
float v0 = InitState[StateVelocityX];
float t = State[StateCurrentTime];
float CorrectPositionX = ( x0 * cos( sqrtKoverM * t ) ) +
( ( v0 / sqrtKoverM ) * sin( sqrtKoverM + t ) );
// Compute draw pos for "correct"
float CorrectEndX = PixelsPerMeter * CorrectPositionX;
// Draw the spring.
strokeWeight( 1.0 );
line( 0.0, 0.0, SpringEndX, 0.0 );
// Draw the spring pivot
fill( 0.0 );
ellipse( 0.0, 0.0,
PixelsPerMeter * 0.03,
PixelsPerMeter * 0.03 );
// Draw the spring bob
fill( 1.0, 0.0, 0.0 );
ellipse( SpringEndX, 0.0,
PixelsPerMeter * 0.1,
PixelsPerMeter * 0.1 );
// Draw the correct bob in blue
fill( 0.0, 0.0, 1.0 );
ellipse( CorrectEndX, -PixelsPerMeter * 0.25,
PixelsPerMeter * 0.1,
PixelsPerMeter * 0.1 );
}
// Time Step function.
void TimeStep( float i_dt )
{
// Compute acceleration from current position.
float A = ( -Stiffness / BobMass ) * State[StatePositionX];
// Update velocity half-way.
State[StateVelocityX] += ( i_dt/2.0 ) * A;
// Update position.
State[StatePositionX] += i_dt * State[StateVelocityX];
// Re-compute acceleration.
A = ( -Stiffness / BobMass ) * State[StatePositionX];
// Update velocity half-way.
State[StateVelocityX] += ( i_dt/2.0 ) * A;
// Update current time.
State[StateCurrentTime] += i_dt;
}
// Processing Draw function, called every time the screen refreshes.
void draw()
{
// Time Step.
TimeStep( 1.0/24.0 );
// Clear the display to a constant color.
background( 0.75 );
// Label.
fill( 1.0 );
text( "Velocity Verlet", 10, 30 );
pushMatrix();
// Translate to the origin.
translate( OriginPixelsX, OriginPixelsY );
// Draw the simulation
DrawState();
}
// Reset function. If the key 'r' is released in the display,
// copy the initial state to the state.
void keyReleased()
{
if ( key == 114 )
{
for ( int i = 0; i < StateSize; ++i )
{
State[i] = InitState[i];
}
}
}
</script>
<canvas id="pjsSpringSimp6"> </canvas>
<p>
Not bad! Now we're cooking with gas! Still not perfect, though. The next class
will explore the fourth-order Runge Kutta approach, and begin to consider
implicit methods.
</body>