반응형
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
import math
#task 2e
x = np.linspace(-0.0001,0.1,50)
#constants
e0=8.85*10 ** (-12)
r0=3 * 10 ** (-3)
Q=2 * 10** (-9)
Q2=10 * 10*(-9)
r2=5*10**(-3)
v=(Q/((4*math.pi*e0)*(math.sqrt((x**2+r0**2)))))
v2=v+(Q2/((4*math.pi*e0)*(math.sqrt(((x-2)**2+r2**2)))))
plt.plot(x, v)
plt.plot(x, v2)
plt.xlabel("Meter")
plt.ylabel("V1/2(x)")
이 코드를 실행하면 다음 TypeError가 발생합니다.
TypeError : length-1 배열 만 Python 스칼라로 변환 할 수 있습니다. 21 일 v = (Q / ((4 * math.pi * e0) (math.sqrt ((x * 2 + r0 ** 2)))))
해결 방법
>>> import numpy as np
>>> import math
>>> a = np.arange(5)
>>> np.sqrt(a)
array([ 0. , 1. , 1.41421356, 1.73205081, 2. ])
#error
>>> math.sqrt(a)
Traceback (most recent call last):
File "<ipython-input-78-c7d50051514f>", line 1, in <module>
math.sqrt(a)
TypeError: only length-1 arrays can be converted to Python scalars
>>>
참조 페이지 https://stackoverflow.com/questions/22158922
반응형
'파이썬' 카테고리의 다른 글
파이썬 jinja2에서 for 루프를 어떻게 끊을 수 있습니까? (0) | 2020.12.21 |
---|---|
파이썬 urlopen 오류 [Errno 11001] getaddrinfo 실패 (0) | 2020.12.21 |
파이썬 Python의 'in'연산자를 재정의 하시겠습니까? (0) | 2020.12.20 |
파이썬 Inserting a value into all possible locations in a list (0) | 2020.12.20 |
파이썬 " 'float'개체를 암시 적으로 str로 변환 할 수 없습니다." (0) | 2020.12.20 |
댓글