Runge-Kutta code not converging with builtin method

后端 未结 1 792
清酒与你
清酒与你 2020-12-12 03:47

I am trying to implement the runge-kutta method to solve a Lotka-Volterra systtem, but the code (bellow) is not working properly. I followed the recomendations that I found

相关标签:
1条回答
  • 2020-12-12 04:01

    You are doing a very typical error,see for instance How to pass a hard coded differential equation through Runge-Kutta 4 or here Error in RK4 algorithm in Python

    It is either

    k2 = f( x+0.5*h*k1, t+0.5*h )
    ...
    x[i+1]=x[i]+(k1+2*(k2+k3)+k4)*(h/6)
    

    or

    k2 = h*f( x+0.5*k1, t+0.5*h )
    

    and so on, with x[i+1] as it was, but not both variants at the same time.


    Update: A more insidious error is the inferred type of the initial values and in consequence of the array of x vectors. By the original definition, both are integers, and thus

    x = np.array( [ x0 ] * n )    
    

    creates a list of integer vectors. Thus the update step

        x[i+1] = x[i] + ( k1 + 2 * ( k2 + k3 ) + k4 ) * (h/6)
    

    will always round to integer. And since there is a phase where both values fall below 1, the integration stabilizes at zero. Thus modify to

    # initial conditions for the system
    x0 = 500.0
    y0 = 200.0
    

    to avoid that problem.

    0 讨论(0)
提交回复
热议问题