Solving two sets of coupled ODEs via matrix form in Python
<p>I want to solve a coupled system of ODEs in matrix form for two sets of variable (i.e. {y} and {m}) which has such a form:</p>

<blockquote>
  <p>y'_n = ((m_n)**2) * y_n+(C * y)_n  ,   m'_n=-4*m_n*y_n  </p>
</blockquote>

<p>where <code>C</code> is a matrix, <code>[2 1, -1 3]</code>.  </p>

<p>On the other hand I want to solve these equations:</p>

<blockquote>
  <p>y'1= m1 ** 2 * y1 + 2 * y1 + y2<br>
   y'2= m2 ** 2 * y2 - y1 + 3 * y3<br>
   m'1= -4 * m1 * y1 ,<br>
  m'2= -4 * m2 * y2<br>
   y1(0)=y2(0)=-15. and m1(0)=m2(0)=0.01</p>
</blockquote>

<p>to finally be able to plot ys and ms versus time via matrix form. I wrote the following program:</p>

<pre><code>import numpy as np
from pylab import plot,show
from scipy.integrate import odeint

C=np.array([[2,1],[-1,3]])
dt=0.001
def dy_dt(Y,time):
 y,m=Y
 m=m+dt*(-4.*m*y)
 dy=m**2*y+np.dot(C,y)
 return dy

m_init=np.ones(2)*0.01
y_init=np.ones(2)*-15.
time=np.linspace(0,4,1/dt)
y0=np.hstack((y_init, m_init))
y_tot=odeint(dy_dt,y0,time)



plot(time,y_tot[0])#y_1
plot(time,y_tot[1])#y_2
plot(time,y_tot[2])#m_1
plot(time,y_tot[3])#m_2
show()
</code></pre>

<p>but I encountered the following error:</p>

<pre><code> y,m=Y   
 ValueError: too many values to unpack 
</code></pre>

<p>Can anybody help me!</p>

<p>Take a look at this to understand what is going on:</p>

<pre><code># we have a collection of different containers, namely list, tuple, set &amp; dictionary
master = [[1, 2], (1, 2), {1, 2}, {1: 'a', 2: 'b'}]

for container in master:
    a, b = container  # python will automatically try to unpack the container to supply a &amp; b with values
    print(a, b)  # all print: 1 2 since a = 1 and b = 2 after the unpacking
</code></pre>

<p>if i have a container with more values than the variables i am trying to supply, i get the "too many values to unpack" error, for example:</p>

<pre><code>container = [1, 2, 3]
a, b = container  # this raises an error, the value 3 has nowhere to go
</code></pre>

<p>you can however say "dump all the rest to b" by:</p>

<pre><code>a, *b = container 
print(a, b)  # -&gt; 1 [2, 3] so a = 1 and b = [2, 3]
</code></pre>

<p>Back to you know, when you say: <code>y, m = Y</code> you have to make sure <code>Y</code> is a container with exactly 2 objects which does not seem to be the case. Lastly, as i said in the comments, you do not seem to call your function <code>dy_dt</code> anywhere.</p>

python numpy scipy odeint