Skip to content

Commit 5233df3

Browse files
Merge pull request #1 from leios/master
updating fork
2 parents 21408c7 + 766c1a5 commit 5233df3

18 files changed

+997
-29
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* [Taylor Series](chapters/mathematical_background/taylor_series.md)
1414
* [Fundamental Algorithms](chapters/fundamental_algorithms/fundamental_algorithms.md)
1515
* [Tree Traversal](chapters/fundamental_algorithms/tree_traversal.md)
16+
* [Euclidean Algorithm](chapters/fundamental_algorithms/euclidean_algorithm/euclidean.md)
1617
* [Computational Mathematics](chapters/computational_mathematics/computational_mathematics.md)
1718
* [Matrix Methods](chapters/computational_mathematics/matrix_methods/matrix_methods.md)
1819
* [Gaussian Elimination](chapters/computational_mathematics/matrix_methods/gaussian_elimination.md)

chapters/computational_physics/verlet.md

Lines changed: 238 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,6 @@ class Verlet(Simulation):
293293
self.obj.pos = self.obj.pos * 2 - self.prev_pos + self.obj.acc * self.dt * self.dt
294294
self.prev_pos = temp_pos
295295

296-
297296
class Stormer_Verlet(Simulation):
298297

299298
def step(self):
@@ -303,7 +302,6 @@ class Stormer_Verlet(Simulation):
303302

304303
self.obj.vel += self.obj.acc * self.dt
305304

306-
307305
class Velocity_Verlet(Simulation):
308306

309307
def step(self):
@@ -332,3 +330,241 @@ if __name__ == "__main__":
332330
main()
333331

334332
```
333+
334+
335+
336+
#### Haskell
337+
```haskell
338+
-- submitted by Jie
339+
type Position = [Double]
340+
type Speed = [Double]
341+
type Time = Double
342+
type Particle = (Position, Speed, Acceleration, Time)
343+
type Acceleration = [Double]
344+
345+
verletStep :: (Particle -> Acceleration)
346+
-> Time
347+
-> Particle
348+
-> Particle
349+
-> Particle
350+
verletStep acc dt (xOld, _, aOld, _) (x, v, a, t) = (x', v', a', t+dt)
351+
where
352+
x' = zipWith3 (\xOld x a -> 2*x - xOld + a*dt^2 ) xOld x a
353+
v' = zipWith3 (\v a aOld -> v + 0.5*(aOld + a)*dt) v a aOld
354+
a' = acc (x', v', [], t+dt)
355+
356+
trajectory :: (Particle -> Acceleration)
357+
-> Time
358+
-> Particle
359+
-> [Particle]
360+
trajectory acc dt p0@(x, v, a, t0) = t
361+
where
362+
t = p0 : p1 : zipWith (verletStep acc dt) t (tail t)
363+
p1 = (x', v', acc (x', v', [], t0+dt), t0+dt)
364+
x' = zipWith3 (\x v a -> x + v*dt + 0.5*a*dt^2 ) x v a
365+
v' = zipWith (\v a -> v + a*dt) v a
366+
367+
freeFall :: Particle
368+
freeFall = last $ takeWhile (\([x],_,_,_) -> x > 0) $ trajectory acc dt p0
369+
where
370+
p0 = ([5], [0], [-10], 0)
371+
dt = 0.001
372+
acc _ = [-10]
373+
```
374+
375+
#### Scratch
376+
Submitted by Jie
377+
378+
![Scratch 2D implementation](verlet_scratch.png)
379+
380+
Link: [https://scratch.mit.edu/projects/173039394/](https://scratch.mit.edu/projects/173039394/)
381+
382+
#### Matlab
383+
```matlab
384+
% Submitted by P. Mekhail
385+
%% Verlet Integration
386+
%%
387+
% This simulates a ball falling (and bouncing) using Verlet integration.
388+
% It was made under the instruction of our overlord LeiosOS.
389+
% You can find his video here:
390+
% <https://www.youtube.com/watch?v=g55QvpAev0I>
391+
392+
% Parameters to change
393+
n = 400; % Number of steps
394+
x0 = 5; % Ball starting height(in metres)
395+
v0 = 0; % Ball starting velocity (+ive is up)
396+
dt = 0.01; % Time step (in seconds)
397+
eff = 0.4; % Ball efficency when bouncing
398+
A = @(x) -10; % Acceleration as a function of position
399+
bounce = 1; % Do you want the ball to bounce?
400+
401+
% Making position and time vectors
402+
x = zeros(n,1);
403+
t = 0:dt:n*dt-dt;
404+
405+
% Setting the initial conditions
406+
x(1) = x0;
407+
x(2) = x0 + v0*dt + 0.5*A(x0)*dt^2;
408+
409+
% Runnin Verlet Integration
410+
for i = 2:n-1
411+
xnew = 2*x(i)-x(i-1)+A(x(i))*dt^2;
412+
if bounce
413+
if xnew > 0
414+
% If you haven't hit the ground keep going
415+
x(i+1) = xnew;
416+
else
417+
% If you have calculated velocity and invert its sign
418+
v = sqrt(eff)*(xnew-x(i-1))/(2*dt);
419+
x(i+1) = x(i) - v*dt + 0.5*A(x(i))*dt^2;
420+
end
421+
else
422+
x(i+1) = xnew;
423+
end
424+
end
425+
plot(t,x)
426+
title('Ball''s Trajectory')
427+
xlabel('Time (s)'); ylabel('Height (m)');
428+
429+
```
430+
431+
#### LabVIEW
432+
Submitted by P. Mekhail
433+
434+
![Verlet LabVIEW](verlet_labview.png)
435+
436+
#### C
437+
438+
```c
439+
// Submitted by Gathros
440+
#include <stdio.h>
441+
442+
// Simple function for velocity-verlet
443+
void verlet(double pos, double acc, double dt){
444+
445+
// Note that we are using a temp variable for the previous position
446+
double prev_pos, temp_pos, time;
447+
prev_pos = pos;
448+
time = 0;
449+
450+
while (pos > 0){
451+
time += dt;
452+
temp_pos = pos;
453+
pos = pos*2 - prev_pos + acc * dt * dt;
454+
prev_pos = temp_pos;
455+
}
456+
457+
printf("%f\n", time);
458+
459+
}
460+
461+
// Simple function for stormer-verlet
462+
void stormer_verlet(double pos, double acc, double dt){
463+
464+
double prev_pos, temp_pos, time, vel;
465+
prev_pos = pos;
466+
vel = 0;
467+
time = 0;
468+
while (pos > 0){
469+
time += dt;
470+
temp_pos = pos;
471+
pos = pos*2 - prev_pos + acc * dt * dt;
472+
prev_pos = temp_pos;
473+
474+
// The acceleration is constant, so the velocity is straightforward
475+
vel += acc*dt;
476+
}
477+
478+
printf("%f\n", time);
479+
480+
}
481+
482+
// Simple function for velocity-verlet
483+
void velocity_verlet(double pos, double acc, double dt){
484+
485+
double time, vel;
486+
vel = 0;
487+
time = 0;
488+
while (pos > 0){
489+
time += dt;
490+
pos += vel*dt + 0.5*acc * dt * dt;
491+
vel += acc*dt;
492+
}
493+
494+
printf("%f\n", time);
495+
496+
}
497+
498+
int main(){
499+
verlet(5.0, -10, 0.01);
500+
stormer_verlet(5.0, -10, 0.01);
501+
velocity_verlet(5.0, -10, 0.01);
502+
503+
}
504+
```
505+
506+
### JavaScript
507+
508+
```html
509+
<!DOCTYPE html>
510+
<html>
511+
<body>
512+
<script>
513+
function verlet(pos, acc, dt){
514+
515+
var prev_pos, temp_pos, time;
516+
prev_pos = pos;
517+
time = 0;
518+
519+
while (pos > 0){
520+
time += dt;
521+
temp_pos = pos;
522+
pos = pos*2 - prev_pos + acc * dt * dt;
523+
prev_pos = temp_pos;
524+
}
525+
526+
return time;
527+
528+
}
529+
530+
function stormer_verlet(pos, acc, dt){
531+
532+
var prev_pos, temp_pos, time, vel;
533+
prev_pos = pos;
534+
vel = 0;
535+
time = 0;
536+
while (pos > 0){
537+
time += dt;
538+
temp_pos = pos;
539+
pos = pos*2 - prev_pos + acc * dt * dt;
540+
prev_pos = temp_pos;
541+
542+
vel += acc*dt;
543+
}
544+
545+
return time;
546+
547+
}
548+
549+
function velocity_verlet(pos, acc, dt){
550+
551+
var time, vel;
552+
vel = 0;
553+
time = 0;
554+
while (pos > 0){
555+
time += dt;
556+
pos += vel*dt + 0.5*acc * dt * dt;
557+
vel += acc*dt;
558+
}
559+
560+
return time;
561+
562+
}
563+
564+
document.write(verlet(5.0, -10, 0.01) + "<br>");
565+
document.write(stormer_verlet(5.0, -10, 0.01) + "<br>");
566+
document.write(velocity_verlet(5.0, -10, 0.01) + "<br>");
567+
</script>
568+
</body>
569+
</html>
570+
```
Loading
Loading
88.4 KB
Loading
188 KB
Loading
137 KB
Loading
189 KB
Loading
189 KB
Loading
66.1 KB
Loading

0 commit comments

Comments
 (0)