Permalink
Browse files

fix up x axis selection; another example

  • Loading branch information...
1 parent 7a0caaa commit fdd1cd4d2d4aa8d1521965c2d693e9ccb1ef4734 @moritz committed Jun 20, 2010
Showing with 35 additions and 5 deletions.
  1. +25 −0 examples/vertical-throw.pl
  2. +8 −3 lib/Math/Model.pm
  3. +2 −2 scratch.pl
View
@@ -0,0 +1,25 @@
+use v6;
+BEGIN { @*INC.push: '../lib' };
+
+use Math::Model;
+
+
+my $m = Math::Model.new(
+ derivatives => {
+ y_velocity => 'y',
+ y_acceleration => 'y_velocity',
+ },
+ variables => {
+ y_acceleration => { $:force / $:mass },
+ mass => { 1 }, # kg
+ force => { -9.81 }, # N = kg m / s**2
+ },
+ initials => {
+ y => 0, # m
+ y_velocity => 20, # m/s
+ },
+ captures => <y y_velocity>,
+);
+
+$m.integrate(:from(0), :to(4.2), :min-resolution(0.2));
+$m.render-svg('throw-vertically.svg', :x-axis('time'));
View
@@ -119,11 +119,16 @@ method integrate($from = 0, $to = 10, $min-resolution = ($to - $from) / 20) {
%!results;
}
-method render-svg($filename, :$x-axis = 'time', :$width = 800, :$height = 600, :$title = 'Model output') {
+method render-svg(
+ $filename,
+ :$x-axis = 'time',
+ :$width = 800,
+ :$height = 600,
+ :$title = 'Model output') {
my $f = open $filename, :w
or die "Can't open file '$filename' for writing: $!";
- my @values = map { %!results{$_} }, @.captures;
- my @x = $x-axis eq 'time' ?? @!time !! %!results{$x-axis};
+ my @values = map { %!results{$_} }, @.captures.grep({ $_ ne $x-axis});
+ my @x = $x-axis eq 'time' ?? @!time !! %!results{$x-axis}.flat;
my $svg = SVG::Plot.new(
:$width,
:$height,
View
@@ -21,5 +21,5 @@
captures => <height>,
);
-$m.integrate(:from(0), :to(50), :min-resolution(1));
-$m.render-svg('spring.svg');
+$m.integrate(:from(0), :to(20), :min-resolution(1));
+$m.render-svg('spring.svg', :x-axis('time'));

0 comments on commit fdd1cd4

Please sign in to comment.