简体   繁体   中英

Getting an unsupported operand type error

Trying to find the Hill function and Hill Coefficient of two functions however the code that I wrote isn't working. The code should be pretty straightforward, I am just defining two functions, a(x) and g(x) and asking the code to use defined inverses in order to find out the Hill Coefficient and Hill Function.

my code:

import numpy as np
from numpy import log as ln
from matplotlib import pyplot as plt
from random import randint
from mpmath import *
import sympy as sym

u = 4
a = randint(1,u)
b = randint(1,u)
c = randint(1,u)
d = randint(1,u)
p = 11*b
m = randint(p,100)

print(f"c1 = {a}")
print(f"n1 = {b}")
ab = a*b
print('y = {}x where y = {} as {} < x'.format(a, ab, b))

print(f"k1 = {c}")
print(f"c2 = {d}")
print(f"n2 = {m}")
dc = d-c
print('y = {}(x-{})/(x+{})'.format(m, c, dc))
ma = m*a
print('y = {}(x-{})/(x+{})'.format(ma, c, dc))

mp.dps = 15
mp.pretty = False

# figure layout
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Hill Function for f(x) and g(x) and f(g(x))
def f(x):
    return a*x
def g(x):
    return m*(x-c)/(d+x-c)
def c(x):
    return a*m*(x-c)/(d+x-c)


# Estimating EC100 for f(x) and g(x)
x = np.linspace(0, 10, 100)
xmax1 = x[np.argmax(f(x))]
ymax1 = f(x).max()
print(f"xmax1 = {xmax1}")
print(f"ymax1 = {ymax1}")

xmax2 = x[np.argmax(g(x))]
ymax2 = g(x).max()
print(f"xmax2 = {xmax2}")
print(f"ymax2 = {ymax2}")

xmax3 = x[np.argmax(c(x))]
ymax3 = c(x).max()
print(f"xmax3 = {xmax3}")
print(f"ymax3 = {ymax3}")

# EC90 and EC10 for f(x), g(x) and f(g(x))
def EC1(x):
    return x/a

y90_1 = ymax1*0.9
y10_1 = ymax1*0.1
EC90_1 = EC1(y90_1)
EC10_1 = EC1(y10_1)
print(f"EC10_1 = {EC10_1}")
print(f"EC90_1 = {EC90_1}")

def EC2(x):
    return ((x*c)-(m*c)-(x*d))/(x-m)

y90_2 = ymax2*0.9
y10_2 = ymax2*0.1
EC90_2 = EC2(y90_2)
EC10_2 = EC2(y10_2)
print(f"EC10_2 = {EC10_2}")
print(f"EC90_2 = {EC90_2}")

def EC3(x):
    return ((x*c)-(a*m*c)-(x*d))/(x-(a*m))

y90_3 = ymax3*0.9
y10_3 = ymax3*0.1
print(f"y90_3 = {y90_3}")
print(f"y10_3 = {y10_3}")
EC90_3 = EC3(y90_3)
EC10_3 = EC3(y10_3)
print(f"EC10_3 = {EC10_3}")
print(f"EC90_3 = {EC90_3}")


# Hill Coefficient for f(x) and g(x)
H_1 = ln(81)/(ln(EC90_1/EC10_1))
H_2 = ln(81)/(ln(EC90_2/EC10_2))
H_3 = ln(81)/(ln(EC90_3/EC10_3))
print(f"Hill's Coefficient for f(x) = {H_1}")
print(f"Hill's Coefficient for g(x) = {H_2}")
print(f"Hill's Coefficient for f(g(x)) = {H_3}")

# Plotting
plt.plot(x, f(x), color = 'red')
plt.plot(x, g(x), color = 'blue')
plt.plot(x, c(x), color = 'green')
#plt.show()

my error:

Traceback (most recent call last):
  File "/Users/*****/Documents/Python/Counterexample_Proof/main.py", line 52, in <module>
    xmax2 = x[np.argmax(g(x))]
  File "/Users/*****/Documents/Python/Counterexample_Proof/main.py", line 40, in g
    return m*(x-c)/(d+x-c)
TypeError: unsupported operand type(s) for -: 'float' and 'function'

You define c twice. The first is a float, the second is a function c(x) . Even though it's defined after g(x) , g(x) isn't executed via the argmax function until after Python redefines c as a function instead of a float.

Rename c(x) to avoid unwanted naming collisions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM