[英]Summation of a function in Python
I'm new to Python programming.我是 Python 编程的新手。 Coming from a MATLAB background.来自 MATLAB 背景。 I'm looking something similar to symsum function from MATLAB in Python.我正在寻找类似于 Python 中来自 MATLAB 的 symsum 函数的东西。
I have my array,我有我的阵列,
a = np.linspace([0,3.14])
I want to sum我想总结
sin(2*i*a) where i ranges from 1 to 20
and then plot the results between a and y然后在 a 和 y 之间绘制结果
I tried the following我尝试了以下
y = nsum(lambda i: np.sin(2*i*a), [0,20])
I'm stuck at this point.我被困在这一点上。
Edit.编辑。 The MATLAB equivalent would be MATLAB 等价物将是
a = linspace(0,pi) syms iy=double(symsum(sin(2*i*a),i,0,20)
Looks like symsum
is part of a symbolics package (in MATLAB and Octave).看起来symsum
是符号包的一部分(在 MATLAB 和 Octave 中)。 sympy
is the Python symbolics package. sympy
是 Python 符号包。 Its integration with numpy
is looser.它与numpy
集成更松散。
=== ===
Here's a guess as to what you are trying to do:以下是对您要尝试执行的操作的猜测:
A range of a
values: a
值的范围:
In [180]: a = np.linspace(0, np.pi, 100)
Outer product with (0,1,2,3,4) (uses broadcasting)带 (0,1,2,3,4) 的外积(使用广播)
In [181]: x = np.arange(5)[:,None]*a
Sum the sin
values, and plot:对sin
值求和,并绘制:
In [182]: y = np.sin(2*x).sum(axis=0)
In [184]: plt.plot(a,y)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.