Permalink
Browse files

more examples

  • Loading branch information...
moritz committed Aug 6, 2010
1 parent 0706fa1 commit 71c566cdef7b565765bb085bf92652f69f3ab177
Showing with 123 additions and 0 deletions.
  1. +23 −0 examples/free-fall.pl
  2. +27 −0 examples/spring-damping.pl
  3. +37 −0 examples/spring-freq.pl
  4. +36 −0 examples/spring.pl
View
@@ -0,0 +1,23 @@
+use v6;
+BEGIN { @*INC.push: '../lib' };
+
+use Math::Model;
+
+my $m = Math::Model.new(
+ derivatives => {
+ velocity => 'height',
+ acceleration => 'velocity',
+ },
+ variables => {
+ acceleration => { $:gravity }, # m / s**2
+ gravity => { -9.81 }, # m / s**2
+ },
+ initials => {
+ height => 50, # m
+ velocity => 0, # m/s
+ },
+ captures => ('height', 'velocity'),
+);
+
+$m.integrate(:from(0), :to(4.2), :min-resolution(0.2));
+$m.render-svg('free-fall.svg', :title('Free falling'));
View
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl6
+use v6;
+BEGIN { @*INC.push: 'lib', '../lib' };
+
+use Math::Model;
+
+my $m = Math::Model.new(
+ derivatives => {
+ velocity => 'height',
+ acceleration => 'velocity',
+ },
+ variables => {
+ acceleration => { $:gravity + $:spring + $:damping },
+ gravity => { -9.81 },
+ spring => { - 2 * $:height },
+ damping => { - 0.2 * $:velocity },
+ },
+ initials => {
+ height => 0,
+ velocity => 0,
+ },
+ captures => ('height', 'time'),
+ numeric-error => 0.001,
+);
+
+$m.integrate(:from(0), :to(20), :min-resolution(5));
+$m.render-svg("spring-damping.svg", :title("Spring with damping"));
View
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl6
+use v6;
+BEGIN { @*INC.push: '../lib' };
+
+use Math::Model;
+
+sub MAIN($freq) {
+ my $m = Math::Model.new(
+ derivatives => {
+ velocity => 'height',
+ acceleration => 'velocity',
+ },
+ variables => {
+ acceleration => { $:gravity + $:spring + $:damping + $:ext_force },
+ gravity => { -9.81 },
+ spring => { - 2 * $:height },
+ damping => { - 0.2 * $:velocity },
+ ext_force => { sin(2 * pi * $:time * $freq) },
+ },
+ initials => {
+ height => 0,
+ velocity => 0,
+ },
+ captures => ('height', 'time'),
+ numeric-error => 0.001,
+ );
+
+ my %h = $m.integrate(:from(0), :to(70), :min-resolution(5));
+ $m.render-svg("spring-freq-$freq.svg", :title("Spring with damping, external force at $freq"));
+ $m = Any;
+
+ my @ampl = (%h<time>.flat Z=> %h<height>.flat).grep({.key >= 50})».value;
+ my $min = @ampl.min;
+ my $max = @ampl.max;
+
+ say "res: $freq\t{$max - $min}";
+}
View
@@ -0,0 +1,36 @@
+use v6;
+BEGIN { @*INC.push: '../lib' };
+
+use Math::Model;
+
+for 0.05, *+0.02 ... 0.4 -> $freq {
+ my $m = Math::Model.new(
+ derivatives => {
+ velocity => 'height',
+ acceleration => 'velocity',
+ },
+ variables => {
+ acceleration => { $:gravity + $:spring + $:damping + $:ext_force },
+ gravity => { -9.81 },
+ spring => { - 2 * $:height },
+ damping => { - 0.5 * $:velocity },
+ ext_force => { sin(2 * pi * $:time * $freq) },
+ },
+ initials => {
+ height => 0,
+ velocity => 0,
+ },
+ captures => ('height', 'time'),
+ numeric-error => 0.001,
+ );
+
+ my %h = $m.integrate(:from(0), :to(50), :min-resolution(5));
+ $m.render-svg("spring-freq-$freq.svg", :title("Spring with damping, external force at $freq"));
+ $m = Any;
+
+ my @ampl = (%h<time>.flat Z=> %h<height>.flat).grep({.key >= 30})».value;
+ my $min = @ampl.min;
+ my $max = @ampl.max;
+
+ say "res: $freq\t{$max - $min}";
+}

0 comments on commit 71c566c

Please sign in to comment.