我不明白为什么输出不是列表...我加错了吗?
from numpy import *
b=0.1;g=0.5;l=632.8;p=2;I1=[I];I=0
for a in arange(-0.2,0.2,0.001):
I+=b**2*(sin(pi/l*b*sin(a)))**2/(pi/l*b*sin(a))**2*(sin(p*pi /l*g*sin(a)))**2/(sin(pi/l*g*sin(a)))**2
I1.append(I)
print (I)
output: 15.999998678557855
答案 0 :(得分:0)
您要打印I1
并在{for循环内 内附加I
b=0.1;g=0.5;l=632.8;p=2;I=0;I1=[I]
for a in arange(-0.2,0.2,0.001):
I+=b**2*(sin(pi/l*b*sin(a)))**2/(pi/l*b*sin(a))**2*(sin(p*pi /l*g*sin(a)))**2/(sin(pi/l*g*sin(a)))**2
I1.append(I)
print(I1)
此代码目前很丑陋,应按以下步骤进行清理... 进行编辑
from numpy import arange
from math import sin, pi
def sin_square(x):
return (sin(x)**2)/(x**2)
def sin_square2(x, p):
return (sin(p*x)**2)/(sin(x)**2)
def term(a, b=0.1, g=0.5, l=632.8, p=2):
"""Function description goes here
More detailed description here including context and
descriptions of arguments b, g, l, p
"""
return (b**2) * sin_square(pi/l*b*sin(a)) * sin_square2(pi/l*g*sin(a), p=p)
I = 0
I1 = [I]
for a in arange(-0.2, 0.2, 0.001):
I += term(a)
I1.append(I)
print(I1)
# output
# [0, 0.03999999014218294, 0.07999998038139602, 0.1199999707171788, ...
我建议使b, g, l, p
更具描述性,并将term
重命名为特定于上下文的内容
答案 1 :(得分:0)
您的代码中有多个错误,缺少导入等。请参见代码中的注释以获取修正:
from numpy import arange
from math import sin,pi
b = 0.1
g = 0.5
l = 632.8
p = 2
I = 0 # you need to specify I
I1 = [I] # before you can add it
for a in arange(-0.2,0.2,0.001):
I += b**2 * (sin(pi/l*b*sin(a)))**2 / (pi/l*b*sin(a))**2 * (sin(p*pi /l*g*sin(a)))**2 / (sin(pi/l*g*sin(a)))**2
I1.append(I) # by indenting this you move it _inside_ the loop
print (I)
print (I1)
输出:
15.999998678557855
[0, 0.03999999014218294, 0.07999998038139602, 0.1199999707171788, ....] # shortened