2017-11-14 3 views
0
에 대해 지원되지 않는 피연산자 유형

파이썬 코드로 고민하고 있습니다. 이걸하고 싶다 equation.Python -> TypeError :^

이 내 코드 :

fs= 5000 
T=np.linspace(0,2.2,fs) 
n=np.arange(fs*2.2) 
u=[] 
for x in T: 
    if x < 0.2: 
     u.append(x * np.sin(34*np.pi*n/fs)) 
    if (x >= 0.2 and x < 0.8): 
     u.append(1/x * np.log10(x+1)) 
    if x >= 0.8 and x < 1.4: 
     u.append((x^2 + 1) * np.sin(12*np.pi*n/fs)) 
    if x >= 1.4: 
     u.append(np.sin(20*np.pi*n/fs + x/3)) 

그리고 파이썬 수익률 :

File "D:/Semestr V/Podstawy Transmisji Danych/labki-ZAD3.py", line 20, in <module> 
    u.append((x^2 + 1) * np.sin(12*np.pi*n/fs)) 

TypeError: unsupported operand type(s) for ^: 'numpy.float64' and 'int' 
+0

제목 자체는 의미가 없습니다. –

+0

문체 목적으로, if, elif, else 규칙을 논리에 사용하십시오. – APorter1031

+0

[Python TypeError :^: 'float'및 'int'에 대해 지원되지 않는 피연산자 유형의 가능한 복제본 (https://stackoverflow.com/questions/34258537/python-typeerror-unsupported-operand-types-for- float-and-int) – kazemakase

답변

3

전원 운영자가 **입니다 ^는 비트 XOR입니다. 실수 ^/** 넘어

1

는 좋은 방법은 빠른 벡터화 된 결과를 그 대상으로 np.piecewise을 사용하는 것입니다 : 모든 값에 대한 n

fs= 5000 
x=np.linspace(0,2.2,fs) 
n=3 

functions=[ 
lambda x : x * np.sin(34*np.pi*n/fs), 
lambda x : 1/x * np.log10(x+1), 
lambda x : (x**2 + 1) * np.sin(12*np.pi*n/fs), 
lambda x : np.sin(20*np.pi*n/fs + x/3)] 

conditions=[x < 0.2,(x >= 0.2) & (x < 0.8), (x >= 0.8) & (x < 1.4), x >= 1.4] 

res=np.piecewise(x,conditions,functions) 
plt.plot(x,res) 

enter image description here

단지 루프.