gpt4 book ai didi

python - python 中的总和,其中列表列表呈指数形式

转载 作者:行者123 更新时间:2023-11-28 21:31:33 29 4
gpt4 key购买 nike

我想用 python 编写以下求和 enter image description here

系数如下表所示

cn=[1.2,0.4,0.6]
vn=[1e-6,5e-5,1e-6]
gn=[4.5e3,6.5e3,9e3]
t=np.linspace(0,10,100)

我尝试了以下方法

import numpy as np
cn=[1.2,0.4,0.6]
vn=[1e-6,5e-5,1e-6]
gn=[4.5e3,6.5e3,9e3]
t=np.linspace(0,10,100)
yt=np.sum(cn *np.exp(-vn*(t-gn)**2))

但是收到错误

TypeError: bad operand type for unary -: 'list'

我想知道哪里出错了或者如何完成这项任务

最佳答案

本次运行:

import numpy as np
cn=np.array([1.2,0.4,0.6])
vn=np.array([1e-6,5e-5,1e-6])
gn=np.array([4.5e3,6.5e3,9e3])
t=np.linspace(0,10,3)
yt=np.sum(cn * np.exp(-vn * (t - gn)**2))
  1. 将列表转换为 numpy 数组

  2. 确保矩阵/数组大小兼容,(即不能添加不同长度的数组)

示例:将 int 添加到 python 列表:

cn=[1.2,0.4,0.6]
cn+1
# output: TypeError: can only concatenate list (not "int") to list

将 int 添加到 numpy 数组:

cn=np.array([1.2,0.4,0.6])
cn+1
# output: array([2.2, 1.4, 1.6])

添加不同维度的 numpy 数组:

cn = np.arange(1,3)
t = np.arange(1,100)
cn + t
# output: ValueError: operands could not be broadcast together with shapes (2,) (99,)

添加具有相同维度的 numpy 数组:

cn = np.arange(1,3)
t = np.arange(3,5)
cn + t
# output: array([4, 6])

关于python - python 中的总和,其中列表列表呈指数形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57978885/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com