-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpend.text
410 lines (180 loc) · 5.65 KB
/
pend.text
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
program Pendulum;
{Solves the double pendulum problem}
const
dim = 2;{number of pendula}
dt = 0.01; {time increment}
pi = 3.1415926535;
g = -980; {acceleration of
gravity}
horiz = 160; {point of suspension of top pendulum}
vert = 200; {point of suspension of top pendulum}
type
vector = array[1..dim] of real;
var
theta, thetadot, thetadoubledot: vector; {angles w/r/t positive
vertical}
x, y, d: vector; {center of mass space
coordinates,distance from end of pendulum}
mass, inertia, length: vector; {mass, moment of
inertia,length of pendula}
Iprime, Vprime, A, B: vector; {useful coefficients}
sine, cosine: vector;
{sine[theta],cosine[theta]}
t, Icross, sinedelta, cosinedelta, denom: real; {time, useful coeffiecients}
endx1, endx2, endy1, endy2: real; {coords. of pendulum
endpts.}
newendx1, newendx2, newendy1, newendy2: real; {newly calculated
coordinates}
k1, k2, v1, v2, totalenergy: real;
r: rect;
{off-screen stuff}
oldPort: GrafPtr;
offScreen: GrafPort;
procedure InitOffScreen;
begin
GetPort(oldPort);
OpenPort(@offScreen);
PenMode(srcXOR);
SetPort(oldPort);
with offScreen, portBits, bounds do
begin
portRect := thePort^.portRect;
bounds := portRect;
rowBytes := (((right - left) + 15) div 16) * 2;
baseAddr := NewPtr(rowBytes * Longint(bottom - top));
if MemError <> 0 then
begin
ShowText;
WriteLn('NewPtr failed in InitOffScreen!');
SysBeep(60);
Halt;
end;
end;
end;
procedure EraseOffScreen;
begin
SetPort(@offScreen);
EraseRect(thePort^.portRect);
SetPort(oldPort);
end;
procedure update (var a: vector; b: vector);
var
i: integer;
begin
for i := 1 to dim do
a[i] := a[i] + dt * b[i];
end; {update}
procedure draw_pendulum;
begin
SetPort(@offScreen);
MoveTo(horiz, vert); {move to orgin}
LineTo(trunc(endx1), trunc(endy1)); {draw pendulum 1}
LineTo(trunc(endx2), trunc(endy2)); {draw pendulum 2}
SetPort(oldPort);
end;
procedure initvals;
begin {initvals}
t := 0;
mass[1] := 200; {grams}
mass[2] := 200; {grams}
length[1] := 100; {cm}
length[2] := 80; {cm}
inertia[1] := 10000; {gm-cm.cm}
inertia[2] := 10000; {gm-cm.cm}
d[1] := 4 * length[1] / 5;
d[2] := 4 * length[2] / 5;
{computed intermediate coefficients}
Iprime[1] := mass[1] * sqr(d[1]) + mass[2] * sqr(length[1]) + inertia[1];
Iprime[2] := mass[2] * sqr(d[2]) + inertia[2];
Icross := mass[2] * length[1] * d[2];
Vprime[1] := g * (mass[1] * d[1] + mass[2] * length[1]);
Vprime[2] := g * mass[2] * d[2];
A[1] := Vprime[1] / Iprime[1];
A[2] := Vprime[2] / Iprime[2];
B[1] := Icross / Iprime[1];
B[2] := Icross / Iprime[2];
{initial conditions}
theta[1] := pi / 2;
theta[2] := pi;
thetadot[1] := 0;
thetadot[2] := 0;
thetadoubledot[1] := 0;
thetadoubledot[2] := 0;
{compute useful things for main loop}
sine[1] := sin(theta[1]);
sine[2] := sin(theta[2]);
cosine[1] := cos(theta[1]);
cosine[2] := cos(theta[2]);
sinedelta := sin(theta[1] - theta[2]);
cosinedelta := cos(theta[1] - theta[2]);
denom := 1 + B[1] * B[2] * cosinedelta * cosinedelta;
{compute endpoints for drawing procedure}
endx1 := horiz + length[1] * sine[1];
endy1 := vert + length[1] * cosine[1];
endx2 := endx1 + length[2] * sine[2];
endy2 := endy1 + length[2] * cosine[2];
end; {initvals}
procedure calculate;
begin
{compute energies}
k1 := 0.5 * (mass[1] * sqr(d[1] * thetadot[1]) + inertia[1] *
sqr(thetadot[1]));
k2 := 0.5 * (mass[2] * (sqr(length[1] * thetadot[1]) + sqr(d[2] *
thetadot[2]) + 2 * length[1] * d[2] * thetadot[1] * thetadot[2] * cos(theta[1]
- theta[2])) + inertia[2] * sqr(thetadot[2]));
v1 := mass[1] * g * d[1] * cos(theta[1]);
v2 := mass[2] * g * (length[1] * cos(theta[1]) + d[2] * cos(theta[2]));
totalenergy := k1 + k2 + v1 + v2;
{now calculate the accelerations}
thetadoubledot[1] := (A[1] * sine[1] - B[1] * thetadot[2] * thetadot[2] *
sinedelta - B[1] * (A[2] * sine[2] + B[2] * thetadot[1] * thetadot[1] *
sinedelta) * cosinedelta) / (denom);
thetadoubledot[2] := (A[2] * sine[2] + B[2] * thetadot[1] * thetadot[1] *
sinedelta - B[2] * (A[1] * sine[1] - B[1] * thetadot[2] * thetadot[2] *
sinedelta) * cosinedelta) / (denom);
{then update angular velocities and angles}
update(thetadot, thetadoubledot);
update(theta, thetadot);
{calculate multiply-used stuff for next time}
sine[1] := sin(theta[1]);
sine[2] := sin(theta[2]);
cosine[1] := cos(theta[1]);
cosine[2] := cos(theta[2]);
sinedelta := sin(theta[1] - theta[2]);
cosinedelta := cos(theta[1] - theta[2]);
denom := 1 + B[1] * B[2] * cosinedelta;
{compute endpoints for drawing procedure}
newendx1 := horiz + length[1] * sine[1];
newendy1 := vert + length[1] * cosine[1];
newendx2 := newendx1 + length[2] * sine[2];
newendy2 := newendy1 + length[2] * cosine[2];
end;
{calculate}
begin {main}
ShowDrawing; {open drawing window}
InitOffScreen; {set up off-screen
bitmap}
EraseOffScreen; {erase off-screen
bitmap}
initvals;
{initialize}
draw_pendulum; {draw pendulum in
initial position}
CopyBits(offScreen.portBits, thePort^.portBits, offScreen.portRect,
thePort^.portRect, srcCopy, nil);
{main loop}
repeat
t := t + dt;
calculate;
draw_pendulum; {erase pendulum}
{update drawing coordinates}
endx1 := newendx1;
endx2 := newendx2;
endy1 := newendy1;
endy2 := newendy2;
draw_pendulum; {draw it back in new
position}
CopyBits(offScreen.portBits, thePort^.portBits, offScreen.portRect,
thePort^.portRect, srcCopy, nil);
until Button
end.