Scipy.integrate.odeint TypeError: Float object is not callable

Advertisements

I am new to Python, and am trying to solve this differential equation using scipy.interpolate.odeint. However, I keep getting a TypeError. I have looked, and cannot find how to fix this issue to get the odeint module to work.

Below is my code:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import CubicSpline
from scipy.integrate import odeint
from math import *

# Define parameters
alpha = 52.875
beta = 13.345
gamma = -1.44
delta = 2.29

# Define model
def model(h,t,om):
    halflife = alpha - (beta(-delta+gamma*om))
    k = log(2)/halflife
    dherb_dt = -kh
    return dherb_dt

# Initial condition
h0 = 4.271 # mg/kg, assuming a 2.67 pt/acre application of Dual II Magnum

# Time, in days, to interpolate over
t = np.linspace(0, 20)

# Solve ODE
om = 2.5
y1 = odeint(model, h0, t, args=(om,))

# Plot
plt.plot(t,y1)
plt.xlabel("Months")
plt.ylabel("Dose")
plt.show()

However, the following error occurs:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [17], in <cell line: 22>()
     20 # Solve ODE
     21 om = 2.5
---> 22 y1 = odeint(model, h0, t, args=(om,))
     24 # Plot
     25 plt.plot(t,y1)

File ~\anaconda3\envs\agron893\lib\site-packages\scipy\integrate\_odepack_py.py:241, in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg, tfirst)
    239 t = copy(t)
    240 y0 = copy(y0)
--> 241 output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
    242                          full_output, rtol, atol, tcrit, h0, hmax, hmin,
    243                          ixpr, mxstep, mxhnil, mxordn, mxords,
    244                          int(bool(tfirst)))
    245 if output[-1] < 0:
    246     warning_msg = _msgs[output[-1]] + " Run with full_output = 1 to get quantitative information."

Input In [17], in model(h, t, om)
      8 def model(h,t,om):
----> 9     halflife = alpha - (beta(-delta+gamma*om))
     10     k = log(2)/halflife
     11     dherb_dt = -kh

TypeError: 'float' object is not callable

How can I fix this issue so that I can solve the equation?

>Solution :

halflife = alpha - (beta(-delta+gamma*om))

You’re trying to use typical math notation a(b) to multiply a * b, but that’s not how Python syntax works. You have to explicitly use the symbol * to perform multiplication.

To Python, beta(-delta+gamma*om) looks like a function call.

Use this instead:

halflife = alpha - (beta * (-delta+gamma*om))

Leave a ReplyCancel reply