Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixes formatting errors
  • Loading branch information
JJ committed Oct 7, 2018
1 parent 2edd1d8 commit 603ff5c
Showing 1 changed file with 54 additions and 54 deletions.
108 changes: 54 additions & 54 deletions doc/Language/math.pod6
Expand Up @@ -235,45 +235,45 @@ wherever possible.
=head1 Numerical integration of ordinary differential equations
Perl 6 is an amazing programming language, and of course, you can do a
lot of cool maths with it. A great amount of work during an applied
mathematician's work is to simulate the models they create. For this
reason, in every coding language, a numerical integrator is a must-have.
Learning about how to do this in Perl 6 can easily be a good way to have
a nice goal while learning a new coding language.
Perl 6 is an amazing programming language, and of course, you can do a
lot of cool maths with it. A great amount of work during an applied
mathematician's work is to simulate the models they create. For this
reason, in every coding language, a numerical integrator is a must-have.
Learning about how to do this in Perl 6 can easily be a good way to have
a nice goal while learning a new coding language.
=head2 Requirements
In Perl 6 there are some modules in the ecosystem that can do the job we
want in a very easy way :
=item L<C<Math::Model>|https://github.com/moritz/Math-Model>
which lets you write mathematical and physical models in an easy and
which lets you write mathematical and physical models in an easy and
natural way.
=item L<C<Math::RungeKutta>|https://github.com/moritz/Math-RungeKutta>
Runge-Kutta integration for systems of ordinary, linear differential
Runge-Kutta integration for systems of ordinary, linear differential
equations.
For this example we are going to use
For this example we are going to use
L<C<Math::Model>|https://github.com/moritz/Math-Model> because its
useful sintaxis, but remember that this module requires
useful sintaxis, but remember that this module requires
L<C<Math::RungeKutta>|https://github.com/moritz/Math-RungeKutta> as well.
Simply install them with I<zef> before using these examples.
In the future it should be a nice idea to add other modules.
=head2 Malthus model
Let's start with the I<'Hello World'> of mathematical Ecology:
L<Malthusian growth model|https://en.wikipedia.org/wiki/Malthusian_growth_model>.
Let's start with the I<'Hello World'> of mathematical Ecology:
L<Malthusian growth model|https://en.wikipedia.org/wiki/Malthusian_growth_model>.
A Malthusian growth model, sometimes called a simple exponential growth
model, is essentially exponential growth based on the idea of the
model, is essentially exponential growth based on the idea of the
function being proportional to the speed to which the function grows.
The equation, then, looks like this:
The equation, then, looks like this:
I<dx/dt = g*x>
I<x(0) = x_0>
I<x(0) = x_0>
Where I<g> is the population growth rate, sometimes called Malthusian
Where I<g> is the population growth rate, sometimes called Malthusian
parameter.
How can we translate that into Perl 6? Well Math::Model bring some help
Expand Down Expand Up @@ -304,37 +304,37 @@ To fully understand what is going on, let me explain it step by step.
=head3 Step by step explanation.
=begin item
First we load the module that make
=begin item
First we load the module that make
the calculations: L<C<Math::Model>|https://github.com/moritz/Math-Model>.
=begin code :skip-test<Snippet for explanantion purposes>
use Math::Model;
=end code
=end item
=end item
=begin item
=begin item
We create the model to add all the information in it.
=begin code :skip-test<Snippet for explanantion purposes>
my $m = Math::Model.new(
=end code
=end item
=end item
=begin item
=begin item
We declare the derivatives that are in our model. In this case, if
you remember our equation, we have our variable I<x> and its derivative
you remember our equation, we have our variable I<x> and its derivative
I<x'> (usually know as the velocity).
=begin code :skip-test<Snippet for explanantion purposes>
derivatives => {
velocity => 'x',
},
=end code
=end item
=end item
=begin item
After that, we declare how our models evolve. We just need
formulas for the derivatives that are not also integration variables
=begin item
After that, we declare how our models evolve. We just need
formulas for the derivatives that are not also integration variables
(in this case, only I<x>), and for other variables we use in the formulas
(the growth rate).
Expand All @@ -344,12 +344,12 @@ variables => {
growth_constant => { 1 }, # basal growth rate
},
=end code
=end item
=end item
=begin item
Finally we declare our initial conditions and use I<captures> to
tell L<C<Math::Model>|https://github.com/moritz/Math-Model> which
=begin item
Finally we declare our initial conditions and use I<captures> to
tell L<C<Math::Model>|https://github.com/moritz/Math-Model> which
variable or variables to record while the simulation is running.
=begin code :skip-test<Snippet for explanantion purposes>
Expand All @@ -358,12 +358,12 @@ initials => {
},
captures => ('x'),
=end code
=end item
=end item
At this point our model is set. We need to run the simulation and render
a cool plot about our results:
=begin code
=begin code
$m.integrate(:from(0), :to(8), :min-resolution(0.5));
$m.render-svg('population growth malthus.svg', :title('population growth'));
=end code
Expand All @@ -379,19 +379,19 @@ Looks great! But to be honest, it is quit unrepresentative. Let's explore
=head2 Logistic model
Resources aren't infinite and our population is not going to grow
forever. P-F Verhulst thought the same thing, so he presented the
Resources aren't infinite and our population is not going to grow
forever. P-F Verhulst thought the same thing, so he presented the
L<logistic model|https://en.wikipedia.org/wiki/Logistic_function>. This
model is a common model of population growth, where the rate of
model is a common model of population growth, where the rate of
reproduction is proportional to both the existing population and the
amount of available resources, all else being equal.
It looks like this:
It looks like this:
I<dx/dt = g*x*(1-x/k)>
I<x(0)=x_0>
where the constant g defines the growth rate and k is the carrying
capacity.
capacity.
Modifying the above code we can simulate its behaviour in time:
=begin code
Expand All @@ -404,7 +404,7 @@ my $m = Math::Model.new(
variables => {
velocity => { $:growth_constant * $:x - $:growth_constant * $:x * $:x / $:k },
growth_constant => { 1 }, # basal growth rate
k => { 100 }, # carrying capacity
k => { 100 }, # carrying capacity
},
initials => {
x => 3,
Expand All @@ -419,27 +419,27 @@ $m.render-svg('population growth logistic.svg', :title('population growth'));
Let's look at our cool plot:
L<link to the image|https://rawgit.com/thebooort/perl-6-math-model-tutorial/master/population%20growth%20logistic.svg>
As you can see population growths till a maximum.
As you can see population growths till a maximum.
=head2 Alle Effect
Interesting, isn't it? Even if these equations seem basic they are
linked to a lot of behaviours in our world, like tumor growth. But,
Interesting, isn't it? Even if these equations seem basic they are
linked to a lot of behaviours in our world, like tumor growth. But,
before end, let me show you a curious case.
Logistic model could be accurate but... What happens when, from a
certain threshold, the population size is so small that the survival
Logistic model could be accurate but... What happens when, from a
certain threshold, the population size is so small that the survival
rate and / or the reproductive rate drops due to the individuals' inhability
to find other ones?
to find other ones?
Well, this is and interesting phenomenon described by W.C.Alle, usually
Well, this is and interesting phenomenon described by W.C.Alle, usually
known as L<Allee effect|https://en.wikipedia.org/wiki/Allee_effect>.
A simple way to obtain different behaviours associated with this effect
is to use the logistic model as a departure, add some terms, and get a
A simple way to obtain different behaviours associated with this effect
is to use the logistic model as a departure, add some terms, and get a
cubic growth model like this one:
I<dx/dt=r*x*(x/a-1)*(1-x/k)>
where all the constants are the same as before and A is called
where all the constants are the same as before and A is called
I<critical point>.
Our code would be:
Expand All @@ -454,8 +454,8 @@ my $m = Math::Model.new(
variables => {
velocity_x => { $:growth_constant * $:x *($:x/$:a -1)*(1- $:x/$:k) },
growth_constant => { 0.7 }, # basal growth rate
k => { 100 }, # carrying capacity
a => { 15 }, # critical point
k => { 100 }, # carrying capacity
a => { 15 }, # critical point
},
initials => {
x => 15,
Expand All @@ -467,13 +467,13 @@ $m.integrate(:from(0), :to(100), :min-resolution(0.5));
$m.render-svg('population growth alle.svg', :title('population growth'));
=end code
Try to execute this one yourself to see the differents behaviours that
Try to execute this one yourself to see the differents behaviours that
arise when you change the initial condition around the critical point!
=head2 Extra info.
Do you like physics? Check the original post by the creator of the
modules that have been used, Moritz Lenz:
Do you like physics? Check the original post by the creator of the
modules that have been used, Moritz Lenz:
L<https://perlgeek.de/blog-en/perl-6/physical-modelling.html>.
Expand Down

0 comments on commit 603ff5c

Please sign in to comment.