hda has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I am trying to use Math::RungeKutta to work with a couple of differential equations but I can't figure out how to define the equations in a way that the module can understand them. The perldoc is not terribly clear on this. Any hint will be appreciated.

Thanks in advance

  • Comment on How to define equations for Math::RungeKutta?

Replies are listed 'Best First'.
Re: How to define equations for Math::RungeKutta?
by moritz (Cardinal) on Nov 02, 2008 at 21:03 UTC
    You don't exactly write down the equation, but you write a piece of perl code that evaluates the equation.

    If your system of equations is of this form:

    f'(x) = x * y g'(x) = x + y;

    You write in your program:

    sub dydt { my ($t, $x, $y) = @_; return ($x * $y, $x + $y); }

    And then proceed as the example in the docs shows.

    Math::RungeKutta doesn't deal with symbolic equations, but expects you to do the evaluation instead.

      Thanks! That's what I needed!
Re: How to define equations for Math::RungeKutta?
by GrandFather (Saint) on Nov 02, 2008 at 21:03 UTC

    The documentation looks reasonable to me. You supply a "function" and a vector of initial values. The evaluation process calls the function you supplied with a time value and a reference to the vector you supplied. Your function should return a list of values corresponding to the values of the vector at the time passed into your function.

    If this still doesn't make sense you could show us your function and we may be able to provide sample code.


    Perl reduces RSI - it saves typing
Re: How to define equations for Math::RungeKutta?
by Illuminatus (Curate) on Nov 02, 2008 at 21:01 UTC
    Perhaps you could post a few samples of failed attempts? That would provide a starting point.