테일러 함수 명령문 오류
조회수 1241회
import math
class tail: def facto(a):
fac = 1
if a == 0:
fac == 1
else:
while a != 1:
fac = (fac * a)
a -= 1
return fac
def asd(i):
if (i % 2) == 0:
a = 1
else:
a = -1
return a
def sin(x,i):
a = 0
res = 0
while a != i :
res = res + tail.asd(a)*(x**(2*(a+1)-1)/tail.facto(2*(a+1)-1))
a += 1
return res
def cos(x,i):
a = 0
res = 0
while a != i :
res = res + tail.asd(a)*(x**(2*(a+1)-2)/tail.facto(2*(a+1)-2))
a += 1
return res
def cal(dif):
start = 1
while abs(abs(math.sin(math.pi * 0.1)) - abs(tail.sin(math.pi * 0.1, start))) > dif:
start += 1
print("sin 의 경우"+str(start)+"번쨰 부터 오차가 " + str(dif) + " 이하로 떨어짐")
start = 1
while abs(abs(math.cos(math.pi * 0.1)) - abs(tail.cos(math.pi * 0.1, start))) > dif:
start += 1
print("cos 의 경우"+str(start)+"번째 부터 오차가 " + str(dif) + " 이하로 떨어짐")
##### main
dif = 0.0001
tail.cal(dif)
오류는 아래와 같은 오류가 발생합니다.
Traceback (most recent call last): File "/solution.py", line 87, in tail.cal(dif) TypeError: unbound method cal() must be called with tail instance as first argument (got float instance instead)
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
제 환경 ( Pycharm, Python interpreter 3.5.3 )에서는 잘 돌아가는데요..
sin 의 경우2번쨰 부터 오차가 0.0001 이하로 떨어짐 cos 의 경우3번째 부터 오차가 0.0001 이하로 떨어짐
이렇게 출력됩니다.
- 테일러 다항식 일반항을 구하면 ((-1)**n)*(x**(2*n))/factorial(2*n)인데 asd(a)*(x**(2*(a+1)-2)/factorial(2*(a+1)-2))로 가공한 이유를 알 수 있을까요?? 알 수 없는 사용자 2019.9.2 09:15
댓글 입력