scipy TypeError:不支持**或pow()的操作数类型:“浮点”和“三次云形缐”

xqkwcwgp  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(149)

我尝试使用scipy解微分方程,但无法通过生成的TypeError。我已经查看过,但不确定如何解决此问题,因为**运算符是用来表示指数的。我该如何解决此问题?
以下是重现错误所需的 Dataframe :

# Opening the packages
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 *

# Here is the data
data = {'day': [1,
                2,
                3,
                4,
                5,
                6,
                7,
                8,
                9,
                10,
                11,
                12,
                13,
                14,
                15,
                16,
                17,
                18,
                19,
                20,
                21,
                22,
                23,
                24,
                25,
                26,
                27,
                28,
                29,
                30,
                31,
                32,
                33,
                34,
                35,
                36,
                37,
                38,
                39,
                40,
                41,
                42,
                43,
                44,
                45,
                46,
                47,
                48,
                49,
                50,
                51,
                52,
                53,
                54,
                55,
                56,
                57,
                58,
                59,
                60,
                61,
                62,
                63,
                64,
                65,
                66,
                67,
                68,
                69,
                70,
                71,
                72,
                73,
                74,
                75,
                76,
                77,
                78,
                79,
                80,
                81,
                82,
                83,
                84,
                85,
                86,
                87,
                88,
                89,
                90,
                91,
                92,
                93],
       'soil_temp': [18.15,
                17.5,
                19.1,
                20.3,
                19.75,
                17.7,
                15.2,
                15.45,
                14.3,
                12.45,
                12.75,
                14.55,
                16.55,
                18.3,
                19,
                19,
                18.8,
                17.45,
                17.15,
                17.4,
                19.9,
                19.85,
                21.4,
                22.05,
                21.75,
                19.9,
                21.9,
                23.45,
                24.65,
                24.4,
                25.1,
                24.75,
                25.2,
                25.45,
                25.75,
                26.35,
                26.5,
                24.8,
                24.55,
                25.95,
                26.35,
                23.9,
                22.2,
                21.2,
                21.9,
                23.4,
                25.45,
                25.75,
                25.25,
                25.65,
                26.4,
                25.7,
                25,
                26.1,
                27,
                26.75,
                26.95,
                26.55,
                25.9,
                26.2,
                27.15,
                28.25,
                27.95,
                27.25,
                26.5,
                27.45,
                27.55,
                27.8,
                28.4,
                28.8,
                28.05,
                25.05,
                25.15,
                25.45,
                25.3,
                22.95,
                22.6,
                25.1,
                25.95,
                26.3,
                26.55,
                26.25,
                27.15,
                27.75,
                28.2,
                25.45,
                25,
                25.1,
                25.15,
                25.15,
                26.05,
                26.2,
                27.45]}

# Create DataFrame
df = pd.DataFrame(data)

下面是生成错误的代码:

# Define parameters
alpha = 52.875
beta = 13.345
gamma = -1.44
delta = 2.29
constant = 60.589
g = 80.64
g2 = 1.04

# Define model
def model(a,t,om):
    # Cubic spline
    day = df['day'].to_numpy()
    temp = df['soil_temp'].to_numpy()
    cubT = CubicSpline(day, temp, bc_type='natural',extrapolate=False)
    d_cubT = CubicSpline.derivative(cubT)

    # Model parameters
    bigfrac = (t/(((alpha-(beta*(-delta+(gamma*om))))/constant)*(g -(g2**cubT))))
    smallfrac = (t*(alpha-(beta*(-delta+(gamma*om)))/constant))

    # Derivative equation
    k = (0.5**bigfrac) * log(0.5) * (((bigfrac - smallfrac - (g2**cubT) * log(g2) * d_cubT)) / (bigfrac**2))
    dherb_dt = -k*a
    return dherb_dt

# Initial condition
a0 = 4.271 

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

om = 0.3
y1 = odeint(model, a0, t, args=(om,))

# Plot
plt.plot(t,y1, 'r-', linewidth = 2, label = 'om = 0.3')
plt.xlabel("xlabel")
plt.ylabel("ylabel")
plt.legend()
plt.show()

将显示以下错误消息:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [1], in <cell line: 233>()
    230 t = np.linspace(0, 90) # 90 days
    232 om = 0.3
--> 233 y1 = odeint(model, a0, t, args=(om,))
    235 # Plot
    236 plt.plot(t,y1, 'r-', linewidth = 2, label = 'om = 0.3')

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 [1], in model(a, t, om)
    215 d_cubT = CubicSpline.derivative(cubT)
    217 # Model parameters
--> 218 bigfrac = (t/(((alpha-(beta*(-delta+(gamma*om))))/constant)*(g -(g2**cubT))))
    219 smallfrac = (t*(alpha-(beta*(-delta+(gamma*om)))/constant))
    221 # Derivative equation

TypeError: unsupported operand type(s) for ** or pow(): 'float' and 'CubicSpline'

如何解决此问题?

8yoxcaq7

8yoxcaq71#

CubicSpline.derivative(nu)的自变量是导数的阶数,1表示一阶导数,2表示二阶导数,以此类推,你需要对自变量求值,以得到给定输入下导数的数值。

相关问题