【发布时间】:2022-10-05 06:31:44
【问题描述】:
在下面的测试中,我想为每个频率步骤在 a 和 b 之间进行积分。我需要提取数组中的数据,以便显示每个频率步骤的积分结果。最终我想为一个复杂的积分重现它,我将在其中绘制实部和虚部。因为我没有相同的变量尺寸,所以我得到一个广播错误。我不确定如何仅使用 numpy 来编写这篇文章。
f = np.sin(x)*freq # for each freq calculate the integrale and store the result
ValueError: operands could not be broadcast together with shapes (11,) (5,)
import numpy as np
a = 0
b = 10
n = 11
h = (b - a) / (n - 1)
x = np.linspace(a, b, n)
freq = np.linspace(0.001, 100, 5)
f = np.exp(x*freq) # for each freq calculate the integrale and store the result of whatever function of 2 variables
#integration
for f in freq:
I_simp = ((h/3) * (f[x[0],freq[0]] + 2*sum(f[x[:n-2:2],freq[f]]) \
+ 4*sum(f[x[1:n-1:2],freq[f]]) + f[x[n-1],freq[-1]]))
print(I_simp) #print the array , in case of complex i will then extract real and imag
【问题讨论】:
标签: python integration numeric complextype