简体   繁体   English

C 程序使用 taylor put sin(1) 得到 0.841

[英]C program using taylor put sin(1) and get 0.841

Im stuck maybe someone here can help me plzzz:) I did wrong program that calc from radian to degri I need to build a program like you calc Sin(x) in radian mode in calculator I put radian x like 1 in radian and it need to give me in radian mode the calc of sin(1) Like if i put sin(1) it need to give me 0.8414 and i cant use sin() and all this only standart and need to use taylor to calc.我卡住了,也许有人可以帮助我plzzz :)我做错了从弧度计算到度数的程序我需要构建一个像你这样的程序在计算器中以弧度模式计算Sin(x)我把弧度x像1一样放在弧度中,它需要以弧度模式给我计算 sin(1) 就像我输入 sin(1) 一样,它需要给我 0.8414,我不能使用 sin(),而这一切只是标准,需要使用 taylor 来计算。 help plz:):') my wrong code:帮助请:):') 我的错误代码:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
double my_sin(double x);
/*my_sin func calc sin(x) from the main*/
double my_sin(double x){
double min_num = 0.000001;
double radian = x*(3.14/180);
double sol = radian;
int i;

int sign = 1;
int n;
int aseret = 1;

//Calc the solution if i <= 0.000001 it stop calc.
for(i=3;i <= min_num; i = i+2){
    n = n * radian * radian;        
    sign = -(sign);                 //change the sign every round.
    aseret = aseret * i * (i-1);    //!3 = 3*2*1.
    sol = sol + (pow(x,i)/aseret) *sign;    // calc the solution.

}
return sol;
}
/*main func*/
int main()
{
double x = 0;
double result;
printf("please enter the number to check the SIN of it:");
scanf("%lf",&x);
result = my_sin(x);
printf("SIN(%f) = %f\n",x,result);
return 0;
}

Your main problem is that you are comparing the index i with the minimum value of the term.您的主要问题是您将索引i与术语的最小值进行比较。 One consequence is that the loop stops immediately.结果之一是循环立即停止。 It is the term x^i/i(i-1) which must be compared with the minimum value.必须将x^i/i(i-1)与最小值进行比较。 As the series is alternating, the error at the end is lower than this minimum value.由于序列是交替的,所以最后的误差低于这个最小值。

It is also useles to perform the conversion to radian, as it seems you are entering a radian value.执行到弧度的转换也很有用,因为您似乎正在输入弧度值。

Moreover, it is useless and inefficient to use pow() function to calculate x^i .此外,使用pow() function 计算x^i是无用且低效的。 Better to alculate this term iteratively.最好迭代地计算这个术语。

Output: Output:

please enter the number to check the SIN of it: 1
SIN(1.000000) = 0.841471
error = -1.59828e-010

Code:代码:

#include <stdio.h>
#include <math.h>

double my_sin(double x){
    double min_num = 0.000001;

    int sign = 1;
    double term = x;
    double sol = term;
    double x2 = x*x;
    int i = 1;

    //Calc the solution if term <= 0.000001 it stop calc.
    do {
        i += 2;
        term *= x2 / (i * (i-1));        
        sign = -sign;                 //change the sign every round.
        sol += term * sign;    
    } while (term > min_num);
    return sol;
}

int main() {
    double x;
    printf("please enter the number to check the SIN of it: ");
    scanf("%lf", &x);
    double result = my_sin(x);
    printf("SIN(%f) = %f\n", x, result);
    double result_exact = sin(x);
    double delta = result - result_exact;
    printf ("error = %g\n", delta);
    return 0;
}
  • You convert x to radians when it is already in radians.当 x 已经是弧度时,您将它转换为弧度。
  • Your loop condition is wrong, the loop terminates immediately and compares an integer with a double.您的循环条件错误,循环立即终止并将 integer 与双精度数进行比较。
  • The variable n is uninitialised and the result of the expression it is used in is unused in any case.变量 n 未初始化,并且在任何情况下都未使用它所使用的表达式的结果。
  • Unnecessary headers included.包括不必要的标题。

Change as per <<<<<< comments:根据<<<<<<评论更改:

#include <stdio.h>
// #include <string.h>      // <<<<<<<<<<<<<< REDUNDANT
// #include <ctype.h>       // <<<<<<<<<<<<<< REDUNDANT
#include <math.h>

#define LIMIT 10        // <<<<<<<<<<<<<< ADD

double my_sin( double x );


/*my_sin func calc sin(x) from the main*/
double my_sin( double x )
{
    // double min_num = 0.000001;       // <<<<<<<<<<<<<< REMOVE   
    // double radian = x*(3.14/180);    // <<<<<<<<<<<<<< REMOVE
    double sol = x ;                    // <<<<<<<<<<<<<< CHANGE
    // int i;                           // <<<<<<<<<<<<<< REMOVE

    int sign = 1;
    // int n = 0 ;                      // <<<<<<<<<<<<<< REMOVE - NOT USED
    int aseret = 1;

    //Calc the solution if i <= LIMIT it stop calc.    // <<<<<<<<<<<<<< CHANGE
    for( int i = 3; i <= LIMIT; i += 2 )               // <<<<<<<<<<<<<< CHANGE
    {
        // n = n * x * x;                              // <<<<<<<<<<<<<< REMOVE - RESULT NOT USED
        sign = -(sign);                 //change the sign every round.
        aseret = aseret * i * (i - 1);    //!3 = 3*2*1.
        sol = sol + (pow( x, i ) / aseret) * sign;    // calc the solution.

    }
    return sol;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM