Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

A rather confusing TypeError: cannot unpack non-iterable float object raised

I have a problem when generating two plots using solve_ivp. The problem occurs at the line that unpack concentrations from the array. What can I do to fix it? Thanks a lot for your replies in advance!

#Modules needed
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp

def reaction_prime(c, t):
    # Define rate constants
    k1 = 100  # Unit: s^-1 * M^-1
    k2 = 0.5  # Unit: s^-1 * M^-1
    k3 = 0.1  # Unit: s^-1
    k4 = 0.2  # Unit: s^-1
    k5 = 0.3  # Unit: s^-1

    # Define initial concentrations
    cA, cB, cC, cD, cE, cF, cG, cH, cI = c  # Unpack concentrations from the array

    # Define reaction rates
    dcAdt = (-1) * k1 * cA * cB + k2 * cC
    dcBdt = (-1) * k1 * cA * cB + k2 * cC
    dcCdt = k1 * cA * cB - k2 * cC - k3 * cC * cD
    dcDdt = k3 * cC * cD - k4 * cE * cF - k5 * cE * cH
    dcEdt = k4 * cE * cF + k5 * cE * cH - k3 * cC * cD
    dcFdt = (-1) * k4 * cE * cF
    dcGdt = k4 * cE * cF
    dcHdt = (-1) * k5 * cE * cH
    dcIdt = k5 * cE * cH

    # Output every rates
    return [dcAdt, dcBdt, dcCdt, dcDdt, dcEdt, dcFdt, dcGdt, dcHdt, dcIdt]

# Define initial concentration of every components
c_ini = [0.1, 0.1, 0, 0.01, 0, 0.1, 0, 0.1, 0]

# Define reaction time
t_r = np.linspace(0, 100, 500)

# Modeling using solve_ivp
c_predict = solve_ivp(reaction_prime, [0, 100], y0=c_ini, t_eval=t_r)

# Plot generation
plt.plot(t_r, c_predict.y[0], label='[A]')
plt.plot(t_r, c_predict.y[1], label='[B]')
plt.plot(t_r, c_predict.y[2], label='[C]')
plt.plot(t_r, c_predict.y[3], label='[D]')
plt.plot(t_r, c_predict.y[4], label='[E]')
plt.plot(t_r, c_predict.y[5], label='[F]')
plt.plot(t_r, c_predict.y[6], label='[G]')
plt.plot(t_r, c_predict.y[7], label='[H]')
plt.plot(t_r, c_predict.y[8], label='[I]')
plt.xlabel('t, Time')
plt.ylabel('c, Concentration')
plt.legend()
plt.title('Relatively COMPLEX Situation')
plt.show()

In another simple codes I wrote, it runs smoothly, no Typeerror raised. I have no idea about why the previous one cannot work…

# Modules needed
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp

def reaction(t, c):
    # Define rate constants
    k1 = 100  # Unit: s^-1 * M^-1
    k2 = 0.5  # Unit: s^-1 * M^-1
    k3 = 0.1  # Unit: s^-1

    # Define concentrations of every components
    cA, cB, cC, cD = c

    # Define reaction rates
    dcAdt = (-1) * k1 * cA * cB + k2 * cC
    dcBdt = (-1) * k1 * cA * cB + k2 * cC
    dcCdt = k1 * cA * cB - k2 * cC - k3 * cC
    dcDdt = k3 * cC

    # Return every rates as a list
    return [dcAdt, dcBdt, dcCdt, dcDdt]

# Define initial rates of every components
c_initial = [0.01, 0.02, 0, 0]

# Define reaction time
t_reaction = np.linspace(0, 100, 500)

# Modeling using solve_ivp
c_predict = solve_ivp(reaction, [0, 100], y0=c_initial, t_eval=t_reaction)

# Plot generation
plt.plot(t_reaction, c_predict.y[0], label='[A]')
plt.plot(t_reaction, c_predict.y[1], label='[B]')
plt.plot(t_reaction, c_predict.y[2], label='[C]')
plt.plot(t_reaction, c_predict.y[3], label='[D]')
plt.xlabel('t, Time')
plt.ylabel('c, Concentration')
plt.legend()
plt.title('Relatively Simple Situation')
plt.show

The error for the former codes:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-50-8b328c41d85b> in <cell line: 38>()
     36 
     37 # Modeling using solve_ivp
---> 38 c_predict = solve_ivp(reaction_prime, [0, 100], y0=c_ini, t_eval=t_r)
     39 
     40 # Plot generation

4 frames
<ipython-input-50-8b328c41d85b> in reaction_prime(c, t)
     13 
     14     # Define initial concentrations
---> 15     cA, cB, cC, cD, cE, cF, cG, cH, cI = c  # Unpack concentrations from the array
     16 
     17     # Define reaction rates

TypeError: cannot unpack non-iterable float object

>Solution :

It does not work now because you changed the function signature. Check the documentation:

The calling signature is fun(t, y), where t is a scalar and y is an ndarray with len(y) = len(y0).

Use def reaction_prime(t, c):

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading