首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:-不支持的操作数类型:绘图程序的'int‘和'list’

TypeError:-不支持的操作数类型:绘图程序的'int‘和'list’
EN

Stack Overflow用户
提问于 2016-09-03 17:10:22
回答 1查看 520关注 0票数 0

我在第11行(其中定义了b)的代码中得到以下错误:

-不支持的操作数类型:'int‘和'list’

代码语言:javascript
复制
#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的科学用法也没有太多经验。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2016-09-03 17:56:53

将您的代码修复为:

  • 定义介于0和1之间的浮点数范围
  • 对每个值调用B并组成数组

代码:

代码语言:javascript
复制
#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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39305217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档