我在第11行(其中定义了b)的代码中得到以下错误:
-不支持的操作数类型:'int‘和'list’
#Needed libraries
import numpy as np
import matplotlib.pyplot as mpl
#Defining given complex refractive indices
N1=complex(1.5,-7.6)
N2=complex(1.0,0.0)
#Defining the function that gives physically reasonable answer for effective medium
def B(x):
a=-2
b=(N1**2)*(2*(1-x)-x)+(N2**2)*(2*x-(1-x))
c=(N1**2)*(N2**2)
Nsq = (-b + np.sqrt(b**2-4*a*c))/(2*a)
return np.sqrt(Nsq)
#Plotting the function
G=B(n)
mpl.plot(n,G)
mpl.show()有谁能帮帮我吗?我不确定问题出在哪里,而且我对python的科学用法也没有太多经验。谢谢!
发布于 2016-09-03 17:56:53
将您的代码修复为:
B并组成数组代码:
#Needed libraries
import numpy as np
import matplotlib.pyplot as mpl
#Defining given complex refractive indices
N1=complex(1.5,-7.6)
N2=complex(1.0,0.0)
#Defining the function that gives physically reasonable answer for effective medium
def B(x):
a=-2
b=(N1**2)*(2*(1-x)-x)+(N2**2)*(2*x-(1-x))
c=(N1**2)*(N2**2)
Nsq = (-b + np.sqrt(b**2-4*a*c))/(2*a)
return np.sqrt(Nsq)
nb_values = 10 # you can set it higher
n = np.linspace(0,1,nb_values+1)
# returns array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
#calling the function for each x in n, build an array from results
G=[B(x) for x in n]
#Plotting the function
mpl.plot(n,G)
mpl.show()https://stackoverflow.com/questions/39305217
复制相似问题