이 페이지는 코딩도장 데이터의 읽기 전용 정적 보관본입니다.

삼각함수 계산

  1. 임의의 수 x를 입력으로 받고, sin x + cos x를 출력하는 프로그램을 만드시오.
  2. 단, sin x와 cos x를 구하는 함수를 직접 만들어야 합니다.
  3. sin x = x - x^3 / 3! + x^5 / 5! - x^7 / 7! + ...입니다.
  4. cos x = 1 - x^2 / 2! + x^4 / 4! - x^6 / 6! + ...입니다.
  5. 삼각함수 연산은 호도법을 이용합니다.
  6. p.s. 정밀한 계산을 원할 경우 오버플로 문제를 해결하는 법도 찾아야 하겠네요. (팩토리얼 연산 이용하기 때문) 예시 입력: 0 예시 출력: 1
테일러 급수

2021/04/30 14:28

이준우

4개의 풀이가 있습니다.

import math

def calculate_trigonometric_function(x: float) -> float:

    """This function refers to Taylor's theorm.
    A remainder for cos or sin at x follows Cauchy's remainder.
    The function has 2 steps. Step1 gets a degree of
    Taylor polynomial function at x. Step 2 calculates sin and cos at x
    and returns two values
    """

    # Step1: get a degree of Taylor polynomial function at x
    degree = 0
    error = 5 * 1e-6
    while True:
        # Upper limit of Cauchy's remainder for sin or cos at x
        upper_limit = x**degree / math.factorial(degree)
        if upper_limit < error:
            break
        else:
            degree += 1

    # Step2: Calculate sin, cos at x
    sin = 0
    cos = 0
    for i in range(degree):
        sin += (-1)**i * x**((2 * i) + 1) / math.factorial(2 * i + 1)
        cos += (-1)**i * x**(2 * i) / math.factorial(2 * i)

    return sin + cos

2021/05/02 03:54

돈 벌면 뭐하노

nfact = {1:1, 2:2}
accuracy = 10

def fact(n):
    global nfact
    if n in nfact:
        return nfact[n]
    else:
        ans = n*fact(n-1)
        nfact[n] = ans
        return ans

def sin(n):
    ans = 0
    for i in range(1, accuracy, 2):
        ans += (n ** i) / fact(i) * (-1) ** (i // 2)
    return ans

def cos(n):
    ans = 1
    for i in range(2, accuracy, 2):
        ans += (n ** i) / fact(i) * (-1) ** (i//2)
    return ans


x = float(input('input number: '))
print('result: ', sin(x) + cos(x))

2021/05/26 15:25

songci

import math

def calSin(x):
    range = 0.1**10
    x = x * math.pi / 180
    sin = x
    i = 1
    while x > range :
        x = x**(2*i + 1) / math.factorial(2*i + 1)
        sin += (-1)**(2*i + 1) * x
        i += 1
    return sin

def calCos(x):
    range = 0.1**8
    x = x * math.pi / 180
    cos = 1
    i = 1
    while x > range :
        x = x**(2*i) / math.factorial(2*i)
        cos += ((-1)**i) * x
        i += 1
    return cos


q = int(input('육십분법의 각을 입력하세요(예: 0 ~ 360) : '))
print('\nsin{0} + cos{0} = {1}'.format(q, calSin(q)+calCos(q)))

2023/07/02 23:33

insperChoi

import math

def factorial(n):
    ret = 1
    for i in range(1, n+1):
        ret *= i
    return ret

x = float(input())
sinx = x; cosx = 1
for n in range(1,11):
  sinx+=math.pow(x,2)/factorial(2*n+1)*math.pow(-1,n)
  cosx+=math.pow(x,2)/factorial(2*n)*math.pow(-1,n)

print(sinx+cosx)  

2023/07/13 12:39

스탠리

목록으로