简体   繁体   中英

Solving The Roessler Oscillator using Runge-Kutta algorithm python

I am having some trouble with this question. I am given this system of equations

dx / dt = -y -z
dy / dt = x + a * y
dz / dt = b + z * (x - c)

and default values a=0.1, b=0.1, c=14 and also the Runge-Kutta algorithm:

def rk4(f, xvinit, Tmax, N):
        T = np.linspace(0,Tmax,N+1)
        xv = np.zeros( (len(T), len(xvinit)) )
        xv[0] = xvinit
        h = Tmax / N
        for i in range(N):
            k1 = f(xv[i])
            k2 = f(xv[i] + h/2.0*k1)
            k3 = f(xv[i] + h/2.0*k2)
            k4 = f(xv[i] + h*k3)
            xv[i+1] = xv[i] + h/6.0 *( k1 + 2*k2 + 2*k3 + k4)
        return T, xv

I need to solve this system from t=0 to t=100 in time steps of 0.1 and using initial conditions (0,0,0)=(0,0,0) at =0 I'm not really sure where to begin on this, I've tried defining a function to give the Oscillator:

def roessler(xyx, a=0.1, b=0.1, c=14):
    xyx=(x,y,x)
    dxdt=-y-z
    dydt=x+a*y
    dzdt=b+z*(x-c)
    return dxdt ,dydt ,dzdt       

which returns the right side of the equation with default values, i've then tried to solve by replacing f with roessler and filling in values for xvinit,Tmax and N with values i'm given but it's not working. Any help is appreciated sorry if some of this is formatted wrong i'm new here.

Well, you almost got it already. Changing your roessler function to the following

def roessler(xyx, a=0.1, b=0.1, c=14):
    x, y, z = xyx
    dxdt=-y-z
    dydt=x+a*y
    dzdt=b+z*(x-c)
    return np.array([dxdt, dydt, dzdt])

and then calling

T, sol = rk4(roessler, np.array([0, 0, 0]), 100, 1000)

makes it work.

Taking aside the typo in the first line of your roessler function, the key to solving this is to understand that you have a system of differential equations, ie, you need to work with vectors . While you already had the input as the vector correct, you also need to make the output of roessler a vector and put in the initial value with the appropriate shape.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM