程序发出近似正弦和余弦值的问题 [英] Issue with program to approximate sin and cosine values

查看:95
本文介绍了程序发出近似正弦和余弦值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,该程序采用一个角度(度)为单位,并根据用户选择的多个给定术语来近似估算sin和cos值.万一你不知道 如何查找罪恶和cos.如此说来,这就是我当前的代码:

I'm trying to write a program that takes an angle in degrees, and approximates the sin and cos value based on a number of given terms that the user chooses. In case you don't know how to find sin and cos. So, with that being said, here is my current code:

import math
def main():
    print()
    print("Program to approximate sin and cos.")
    print("You will be asked to enter an angle and \na number of terms.")
    print("Written by ME")
    print()

    sinx = 0
    cosx = 0

    x = int(input("Enter an angle (in degrees): "))
    terms = int(input("Enter the number of terms to use: "))
    print()

    for i in range(1, terms+1):
        sinx = sinx + getSin(i, x)
        cosx = cosx + getCos(i, x)

    print(cosx, sinx)

def getSin(i, x):
    if i == 1:
        return x
    else:
        num, denom = calcSinFact(i, x)
        sin = num/denom
        return sin

def getCos(i, x):
    if i == 1:
        return 1
    else:
        num, denom = calcCosFact(i, x)
        cos = num/denom
        return cos

def calcSinFact(i, x):
    if i % 2 == 1:
        sign = -1
    if i % 2 == 0:
        sign = +1
    denom = math.factorial(i*2-1)
    num = sign * (x**(i*2-1))
    return num, denom

def calcCosFact(i, x):
    if i % 2 == 1:
        sign = -1
    if i % 2 == 0:
        sign = +1
    denom = math.factorial(i*2)
    num = sign * (x**(i*2))
    return num, denom

它运行,但是如果我使用上图所示的示例,则得到cos = -162527117141.85715和sin = -881660636823.117.所以很明显有什么问题.在上图中,答案应为cos = 0.50000000433433和sin = 0.866025445100.我假设这是我在第一个循环中将值相加的方式,但我可能是错的.感谢您的帮助!

It runs but if i use the example shown in the picture above, i get cos = -162527117141.85715 and sin = -881660636823.117. So clearly something is off. In the picture above the answers should be cos = 0.50000000433433 and sin = 0.866025445100. I'm assuming it's the way I'm adding together the values in the first loop but i could be wrong. Any help is appreciated!

推荐答案

第1个问题是您正在使用的公式

Issue no 1 is that the formulas you are using

(请参阅维基百科)期望 x 是不是<弧度>弧度.绕一圈是360度或2 * pi,因此您可以通过乘以pi/180来将度数转换为弧度,如下面的python代码所示,以正确地获得90度的罪恶.

(see wikipedia) expect x to be in radians not degrees. Going once round a circle is 360 degrees or 2*pi, so you can convert from degrees to radians by multipling by pi/180, as shown below in python code to incorrectly and then correctly get the sin of 90 degrees.

>>> math.sin(90)
0.8939966636005579
>>> math.sin(90*math.pi/180)
1.0

问题2是其余的代码.正如评论中指出的那样,存在一些错误,找到它们的最佳方法是使用一些策略性的print语句.但是,您可以使用更少的代码行来编写程序,而且简单的程序如果出现问题,往往具有更少的错误并且更容易调试.

Issue no 2 is the rest of the code. As pointed out in the comments, there are some bugs, and the best way to find them would be to use some strategic print statements. However, you could write your program with far fewer lines of code, and simpler programs tend to have fewer bugs and be easier to debug if they do have problems.

由于这是一项任务,因此我不会为您做这件事,但是一个相关的示例是sinh(x)的系列.

As this is an assignment, I won't do it for you, but a related example is the series for sinh(x).

(再次来自维基百科)

您可以使用Python 列表理解.可以对列表进行printsum med以获得结果,如下面的程序

You can produce the terms in "one shot", using a Python list comprehension. The list can be printed and summed to get the result, as in the program below

x = 90 * math.pi / 180 # 90 degrees
n = 5
terms = [x**(2*i+1)/math.factorial(2*i+1) for i in range(n)]
print terms
sinh = sum(terms)
print sinh, math.sinh(x)

该程序的输出为

[1.5707963267948966, 0.6459640975062462, 0.07969262624616703, 0.004681754135318687, 0.00016044118478735975]
2.30129524587 2.30129890231

我直接从求和的数学公式中生成了Python列表理解代码,该代码很方便地用左侧的"Sigma"符号表示.您可以通过类似的方式产生罪恶和cos.您需要的一个缺少的成分是系列中每个点的符号.数学公式告诉您您需要(-1) n .与Python等效的是(-1)**n,可以将其插入列表理解代码中的相应位置.

I produced the Python list comprehension code directly from the mathematical formula for the summation, which is conveniently given in "Sigma" notation on the left hand side. You can produce sin and cos in a similar way. The one missing ingredient you need is the signs at each point in the series. The mathematical formulas tell you you need (-1)n. The Python equivalent is (-1)**n, which can be slotted into the appropriate place in the list comprehension code.

这篇关于程序发出近似正弦和余弦值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆