0

I have the coupled ODE, but when i run this code i get this error: TypeError: 'float' object is not subscriptable

The error occurs on the line^ return array([-si*p[0], Here is my code:

from scipy.integrate import ode
import matplotlib.pyplot as plt
import warnings
si = 1/25 #0.053
fi = 1/23 #0.05
def deriv(p,t): 
    return array([-si* p[0], 
              si * p[0] - fi * p[1], 
              fi * p[1] - fi * p[2], 
              fi * p[2] - fi * p[3], 
              fi * p[3] - fi * p[4], 
              fi * p[4] - fi * p[5], 
              fi * p[5] - si * p[6], 
              si * p[6]])

backend = 'dopri5'

t0 = 0
t1 = 2000.0
p_init = array([1, 0, 0, 0, 0, 0, 0, 0])
r = .01

solver = ode(deriv).set_integrator(backend, nsteps=1)
solver.set_initial_value(p_init, t0).set_f_params(r)
solver._integrator.iwork[2] = -1
sol = []
warnings.filterwarnings("ignore", category=UserWarning)
while solver.t < t1:
solver.integrate(t1, step=True)
sol.append([solver.t, solver.p])
warnings.resetwarnings()
sol = array(sol)

plt.plot(sol[:,0], sol[:,1], 'b.-')
plt.show()

Where is the mistake?

  • I think you're going to need to explain a bit more about what you're trying to do here, but it looks like the arguments you're passing to `deriv` are in the wrong order and should be more like `deriv(t, p, r)`, with the independent variable first (see [the docs](http://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.ode.html#scipy.integrate.ode)) – xnx Dec 16 '15 at 09:09
  • @xnx thank you, it really was a mistake. May be can help me in data reshaping? I've get the result of integration, but the structure of answer is unacceptable for me. The answer: list[t, array[y1, y2, ... ]] I need to get answer in this form: list[t, y1, y2, ...] without the arrays in second column. Thank you for the help. – Vasya Pravdin Dec 16 '15 at 10:29
  • @xnx I solved the problem and got the right answer. Thank you! – Vasya Pravdin Dec 16 '15 at 12:59
  • I'm glad you got it to work. If you think it would be useful to other people, you should consider posting your own answer. – xnx Dec 16 '15 at 13:25

1 Answers1

1

So, error was caused by the incorrect order of input variables in deriv. Here is the correct code:

from scipy.integrate import ode
from pylab import *
import seaborn
import numpy as np
import pandas as pd
import warnings

si = 1/25 #0.053
fi = 1/23 #0.05
def deriv(t,y,r): #correct order of input variables
    return np.array([-si* y[0], 
              si * y[0] - fi * y[1], 
              fi * y[1] - fi * y[2], 
              fi * y[2] - fi * y[3], 
              fi * y[3] - fi * y[4], 
              fi * y[4] - fi * y[5], 
              fi * y[5] - si * y[6], 
              si * y[6]])

backend = 'dopri5'
t0 = 0
t1 = 400.0
y_init = np.array([1, 0, 0, 0, 0, 0, 0, 0])
r = .01

solver = ode(deriv).set_integrator(backend, nsteps=1)
solver.set_initial_value(y_init, t0).set_f_params(r)
solver._integrator.iwork[2] = -1

ans = []
warnings.filterwarnings("ignore", category=UserWarning)
while solver.t < t1:
    solver.integrate(t1, step=True)
    ans.append(np.append(solver.t, solver.y)) #collect the answer on every slice of time

df = pd.DataFrame(ans)
df.columns = ['t', 'y0', 'y1','y2', 'y3', 'y4', 'y5', 'y6', 'y7'] #create DataFrame 

for i in df.columns.values[1:]:
    plot(df['t'], df[i]) #plotting
  • You don't need the pandas package if the derivatives are returned as a list and the same for y_init. Also there's no "r" variable in your derivs function so replace r by si, fi. Furthermore, you should be careful when defining your si and fi variables: currently your code sets them to be identically zero. Replace si = 1/25 (integer division) by si = 1./25 (float division). – Dai May 11 '16 at 10:15
  • Oh and further to my comment about the pandas package, you can delete the lines with df and just do: y = np.array(ans), plot(y[:,0], y[:,1:], '-'), show() – Dai May 11 '16 at 10:21