我制作了此代码,我问是否有人可以帮助我重复该功能,因为我需要使用不同的K2值

发布于 2025-02-10 03:09:44 字数 727 浏览 1 评论 0原文

我执行了此代码,需要使用不同的K2值,并且仅使用一个函数。无需重复相同的函数3次。由于K2会影响每个图的曲线。

import numpy as np 
import matplotlib.pyplot as plt
from scipy.integrate import odeint

def F(Y,t):
  k2=[3,6,9]
  T1=15
  p1=15
  p2=4
  for i in k2:
      return np.array([ -p1*Y[1]*Y[0] -p2*Y[2]*Y[0] , p1*Y[1]*Y[0]-Y[1]*(1/T1) , p2*Y[2]*Y[0]-Y[2]*(1/T1)+p2*k2*Y[2]*Y[1]*(1/T1)])

T=np.linspace(0,140,100)
conditions_initiales = [0.8 , 0.01 ,10**-7]
Y=odeint(F,np.array(conditions_initiales),T)
u=Y[:,0]                 
y2=Y[:,2]
plt.plot(T,y2)                  
plt.legend(['y2','z2','v2'])
plt.grid()
plt.xlabel('Temps en jours ')
plt.title("La propagation de deux variants ")
plt.show()

这个代码给了我这个错误类型:不能将序列乘以“ numpy.float64”类型

i did this code and i need to use different values of k2 and only use one function .Without repeating the same function 3 times .Because k2 influence the curve of every graph.

import numpy as np 
import matplotlib.pyplot as plt
from scipy.integrate import odeint

def F(Y,t):
  k2=[3,6,9]
  T1=15
  p1=15
  p2=4
  for i in k2:
      return np.array([ -p1*Y[1]*Y[0] -p2*Y[2]*Y[0] , p1*Y[1]*Y[0]-Y[1]*(1/T1) , p2*Y[2]*Y[0]-Y[2]*(1/T1)+p2*k2*Y[2]*Y[1]*(1/T1)])

T=np.linspace(0,140,100)
conditions_initiales = [0.8 , 0.01 ,10**-7]
Y=odeint(F,np.array(conditions_initiales),T)
u=Y[:,0]                 
y2=Y[:,2]
plt.plot(T,y2)                  
plt.legend(['y2','z2','v2'])
plt.grid()
plt.xlabel('Temps en jours ')
plt.title("La propagation de deux variants ")
plt.show()

and this code gave me this error TypeError: can't multiply sequence by non-int of type 'numpy.float64'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浴红衣 2025-02-17 03:09:44

您可以使用odeint()通过args参数传递额外参数:

def F(Y, t, k2):
    T1 = 15
    p1 = 0.15
    p2 = 0.4
    return np.array([-p1*Y[1]*Y[0]-p2*Y[2]*Y[0], p1*Y[1]*Y[0]-Y[1]*(1/T1), p2*Y[2]*Y[0]-Y[2]*(1/T1)+p2*k2*Y[2]*Y[1]*(1/T1)])

T = np.linspace(0, 140, 100)
conditions_initiales = np.array([0.8, 0.01, 10**-7])
Y = odeint(F, conditions_initiales, T, args=(0.3,))
Z = odeint(F, conditions_initiales, T, args=(0.6,))
V = odeint(F, conditions_initiales, T, args=(0.9,))

You can pass extra arguments through odeint() with the args parameter:

def F(Y, t, k2):
    T1 = 15
    p1 = 0.15
    p2 = 0.4
    return np.array([-p1*Y[1]*Y[0]-p2*Y[2]*Y[0], p1*Y[1]*Y[0]-Y[1]*(1/T1), p2*Y[2]*Y[0]-Y[2]*(1/T1)+p2*k2*Y[2]*Y[1]*(1/T1)])

T = np.linspace(0, 140, 100)
conditions_initiales = np.array([0.8, 0.01, 10**-7])
Y = odeint(F, conditions_initiales, T, args=(0.3,))
Z = odeint(F, conditions_initiales, T, args=(0.6,))
V = odeint(F, conditions_initiales, T, args=(0.9,))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文