如何在Python中获得这些结果?

时间:2017-12-16 14:57:09

标签: python

我正在努力解决我遇到的问题,但我无法得到正确答案。

"""
Given is the following heading of a function to calculate the cosines.

               def cosines (x, number_of_terms):
               # x is a float; number_of_terms is an int

This method calculates the cosines of x with the series

    x - x^3/3! + x^5/5! - x^7/7! + x^9/9! - ...


x^n is the notation for "x to the power n" and n! is the notation for "the factorial of n".
The number of terms that should be calculated is given by the parameter number_of_terms.
Implement this method.

Do this without using any functions from the module math. Assume: number_of_terms >= 1.
"""

所以,我做的是这个:

def cosines(x, number_of_terms):
    sign = 1
    result = 0.0
    counter = x
    denominator = 1.0

    for i in range (number_of_terms, number_of_terms+1):
        fracture = counter / denominator
        term = sign * fracture
        result += term
        counter *= x 
        denominator *= i
        sign = - sign
    return result

first = cosines(90.0, 1)
second = cosines(90.0, 4)
third = cosines(90.0, 8)

print "should be 30.000000: %f" % first
print "should be -4546255.714286: %f" % second
print "should be -7167780281079.437000: %f" % third

这是我得到的结果:

---BEGIN TEST---
should be 30.000000: 90.000000
should be -4546255.714286: 90.000000
should be -7167780281079.437000: 90.000000
----END TEST----

我做错了什么?

2 个答案:

答案 0 :(得分:0)

我更正了您的代码,并将该功能的名称更改为“正弦”'因为这就是数学所反映的:

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)   

def sines(x_in_degrees, number_of_terms):
    # Convert to radians
    #import math
    #x = math.radians(x_in_degrees)
    x = x_in_degrees * 3.141592653589793/180.0

    sign = 1
    result = 0.0
    p = 1

    for i in range(1, number_of_terms+1):
        num = x**p 
        denom = factorial(p)
        temp = sign * num / denom
        #temp = sign * math.pow(x, p) / math.factorial(p)

        result = result + temp 
        sign = - sign
        p = p + 2
    return result

first = sines(90.0, 1)
second = sines(90.0, 4)
third = sines(90.0, 8)

print "Result with one term: %f" % first
print "Result with four terms: %f" % second
print "Result with eight terms: %f" % third

答案 1 :(得分:0)

(已编辑)正如评论中所指出的,您的代码需要

  1. 使用弧度而非度数的角度

  2. 为余弦

  3. 实施正确的幂级数

    解释"术语数量"作为您想要计算的幂级数中的相关项的数量,下面是计算45度或π/ 4弧度的余弦的示例。

    如果你想计算循环中的阶乘,那么使代码工作的关键是,只有偶数迭代才能计算一个项,以匹配余弦幂级数。

    def cosines(x, number_of_terms):
        sign = 1
        result = 1
        counter = x
        denominator = 1.0
        # Counting the 1 in the cosine power series as the first "term"
        terms_computed = 1
        # skip computing for iterations not relevant to the power series
        iterations = 1
        while terms_computed < number_of_terms:
            iterations += 1
            counter *= x 
            denominator *= iterations
            if iterations %2 == 0:
                terms_computed += 1
                sign = - sign
                fracture = counter / denominator
                term = sign * fracture
                result += term
    
    
        print 'input %d, number of relevant terms computed %d'%(number_of_terms,terms_computed)
        return result
    
    pi= 3.1415926
    angle = pi/4.
    print 'angle in radians ',angle
    first = cosines(angle, 1)
    second = cosines(angle, 2)
    third = cosines(angle, 4)
    fourth = cosines(angle, 8)
    fifth = cosines(angle, 10)
    
    print "first %f" % first
    print "second %f" % second
    print "third %f" % third
    print "fourth %f" % fourth
    print "fifth %f" % fifth
    

    结果:

    角度,弧度0.78539815

    输入1,计算1的相关术语数

    输入2,计算相关术语的数量2

    输入4,计算4的相关术语数

    输入8,计算相关项的数量8

    输入10,计算10的相关术语数

    首先1.000000
    第二个0.691575
    第三个0.707103
    第四次0.707107
    第五名0.707107