COS x和的SiNx在C语言程序设计的泰勒级数展开,而无需使用math.h中,仅INT里面的main() [英] Taylor Series Expansion of cos x and sin x in C Programming without using math.h and only inside int main()

查看:367
本文介绍了COS x和的SiNx在C语言程序设计的泰勒级数展开,而无需使用math.h中,仅INT里面的main()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我们的学校项目,我们需要创建一个计算罪x的泰勒展开级数的逼近程序 COS X ,只使用&LT; stdio.h中&GT; 没有用户定义的比<其它功能code> INT的main(),从 180 到 180 在的增量+5 。以下是我的code:

I'm working on a project for our school and we are required to create a program that computes the approximation of the Taylor Expansion Series of sin x and cos x, only using <stdio.h> and without user-defined functions other than int main(), of all angles from -180 to 180 in increments of +5. the following is my code:

#include <stdio.h>
#define PI   3.141592653589
#define NUMBER_OF_TERMS    10

int
main()
{
    int cosctr, sinctr;
    double ctr, radi;
    double cosaccu, costerm, sinaccu, sinterm;

    for (ctr = -180; ctr < 185; ctr = ctr + 5) {
        radi = ctr * PI/180.0;
        cosctr = 1;
        cosaccu = 1;
        costerm = 1;
        sinctr = 2;
        sinaccu = radi;
        sinterm = radi;
        while (cosctr <= 2*NUMBER_OF_TERMS) {
            costerm = costerm*(-1)*(radi*radi)/(cosctr*(cosctr + 1));
            cosaccu = cosaccu + costerm;
            cosctr+=2;
        } do {
            sinterm = sinterm*(-1)*(radi*radi)/(sinctr*(sinctr + 1));
            sinaccu = sinaccu + sinterm;
            sinctr+=2;
        } while (sinctr <= 2*NUMBER_OF_TERMS);
        printf("%.2lf      %.12lf      %.12lf      %.12lf\n", ctr, radi, cosaccu, sinaccu);
    } return 0;
}

在code以上是精确的15项扩展逼近。但是,如果我更改 NUMBER_OF_TERMS ,例如,5或10,近似是有缺陷的。结果
有什么建议?

The code above is accurate for a 15 terms expansion approximation. however, if I change NUMBER_OF_TERMS to, for example, 5 or 10, the approximation is flawed.
Any suggestions?

让我澄清一下:我需要获得5项,10项和15项的近似值。我不能使用超过&LT以外的任何其他图书馆; stdio.h中&GT; 。我不能使用任何其他功能之外INT主要()(我为我之前解释含糊道歉)。结果
请使用附带的修正 code回答。

Let me clarify: I need to obtain an approximation of 5 terms, 10 terms, and 15 terms. I cannot use any other library other than <stdio.h>. I cannot use any other functions outside of int main() (I apologize for the vagueness of my explanation before).
Please answer with the included corrected code.

推荐答案

我想你的code;它工作正常,我在这做什么它看起来像它的设计目的。这是你的code的输出余弦之间的5和10条款和相同的近似通过数学计算的比较。他们同意到小于10 ^ -12 ,即你的输出precision:

I tried your code; it works fine for me, in that it does what it looks like it's designed to do. Here's a comparison between your code's output for the cosine at 5 and 10 terms and the same approximation as calculated by Mathematica. They agree up to <10^-12, i.e. your outputted precision.:

在这里输入的形象描述

我与你code看到的唯一的问题是,你与你的设计循环的方式,实际上是考虑到 NUMBER_OF_TERMS + 1 条款:你算的第一项处于扩张(即常数项的余弦值,为正弦波线性项)。你开始与这个第一项,然后你的循环又增加了 NUMMBER_OF_TERMS 条款。如果不是在设计上,你实际上是近似的功能与你期望更高的precision。

The only problem I see with your code is that, with the way you designed your loops, you're actually taking into account NUMBER_OF_TERMS + 1 terms if you count the first terms in the expansion (i.e. the constant term for the cosine, the linear term for the sine.) You start with this first term, and then your loop adds another NUMMBER_OF_TERMS terms. If that is not by design, you're actually approximating the functions with higher precision that you are expecting.

这篇关于COS x和的SiNx在C语言程序设计的泰勒级数展开,而无需使用math.h中,仅INT里面的main()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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