Luca Thiede - 1 year ago 105

Python Question

I am totally new to python, and try to integrate following ode:

$\dot{x} = -2x-y^2$

$\dot{y} = -y-x^2

This results in an array with everything 0 though

What am I doing wrong? It is mostly copied code, and with another, not coupled ode it worked fine.

`import numpy as np`

import matplotlib.pyplot as plt

from scipy.integrate import ode

def fun(t, z):

"""

Right hand side of the differential equations

dx/dt = -omega * y

dy/dt = omega * x

"""

x, y = z

f = [-2*x-y**2, -y-x**2]

return f

# Create an `ode` instance to solve the system of differential

# equations defined by `fun`, and set the solver method to 'dop853'.

solver = ode(fun)

solver.set_integrator('dopri5')

# Set the initial value z(0) = z0.

t0 = 0.0

z0 = [0, 0]

solver.set_initial_value(z0, t0)

# Create the array `t` of time values at which to compute

# the solution, and create an array to hold the solution.

# Put the initial value in the solution array.

t1 = 2.5

N = 75

t = np.linspace(t0, t1, N)

sol = np.empty((N, 2))

sol[0] = z0

# Repeatedly call the `integrate` method to advance the

# solution to time t[k], and save the solution in sol[k].

k = 1

while solver.successful() and solver.t < t1:

solver.integrate(t[k])

sol[k] = solver.y

k += 1

# Plot the solution...

plt.plot(t, sol[:,0], label='x')

plt.plot(t, sol[:,1], label='y')

plt.xlabel('t')

plt.grid(True)

plt.legend()

plt.show()

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Your initial state (`z0`

) is `[0,0]`

. The time derivative (`fun`

) for this initial state is also `[0,0]`

. Hence, for this initial condition, `[0,0]`

is the correct solution for all times.

If you change your initial condition to some other value, you should observe more interesting result.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**