如何在绘制总体模型时修复“ **或pow()不支持的操作数类型:'builtin_function_or_method'和'float'”?

时间:2019-07-09 12:18:27

标签: python

作为其要求的一部分,我正在尝试为微分方程主题绘制指数和逻辑人口模型,这是我的代码,到目前为止,在运行它时出现错误,我应该为此替换/执行什么操作跑?

import numpy as np
import matplotlib.pyplot as plt
import math
from IPython.display import clear_output

print("Utilize Which Growth Model of Population? (Type A or B)")
print()
print("A Exponential Growth Model")
print("B Logistic Growth Model")
print()
A = int(1)
# Exponential Growth Model
B = int(2)
# Logistic Growth Model
C = input("Growth Model of choice : ")
print()

if C == "A":
    # Definition of Parameters
    print("The Differential Equation of your chosen growth model is P'(t) = r*P(t)")
    print()
    print("Where r = growth parameter")
    print("Where P(t) = total population at a certain time t")
    print("Where t = time")
    print()
    # Explanation of Differential Equation
    print("This equation can be considered as the exponential differential equation")
    print("because its solution is P(t) = P(0)*e^r*t ; where P(0) = Initial Population")
    print()
    print("This equation can be portrayed by using this graph : ")

    # Graph Code
    x, y = np.meshgrid(np.linspace(-50, 50, 10), np.linspace(-50, 50, 10))
    r = float(input("Encode Growth Parameter :"))
    t = float(input("At how many years do you want to solve? :"))
    P = float(input("Encode Population Count :"))
    P = y
    t = x
    x = np.asarray(x, dtype="float64")
    Un = u / P * (math.exp ** (r * t))
    Vn = u / P * (math.exp ** (r * t))
    plt.quiver(x, y, Un, Vn)
    plt.plot([8, 12, 25, 31], [1, 16, 20, 40])
    plt.show()

if C == "B":
    print("The Differential Equation of your chosen growth model is y' = k*y*(M-y)")
    print()
    print("Where k = slope of the function")
    print("Where y = y-value at the specific point")
    print("Where M = limit of y as x approaches infinity")
    print()
    print("This equation is derived using *** ")

我在python中运行了这段代码,我希望它能够像在网上的示例中看到的那样运行,但是相反,我得到了“ **或pow()不支持的操作数类型:'builtin_function_or_method'和'float' “这是什么意思?

1 个答案:

答案 0 :(得分:1)

您使用math.exp作为数字,但实际上是一个函数:exp(x)返回e升至x的幂。

请参阅:

In [1]: import math

In [2]: type(math.exp)
Out[2]: builtin_function_or_method

In [3]: math.exp(1)
Out[3]: 2.718281828459045

In [4]: math.exp(2)
Out[4]: 7.38905609893065

使用math.exp**(r*t)代替math.exp(r*t)