本文介绍了用无穷级数评估sinx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用无限系列评估sinx



我尝试过:



evaluation of sinx using infinite series

What I have tried:

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

int main()
{
    int terms;
    int count=0;
    int sum = 0;int x;int n;
    printf("enter number of terms");
    scanf("%d",&terms);
    printf("enter value of x");
    scanf("%d",&x);
    while(count <= terms){

        n = 2*count+1;

        sum = sum + ((pow(-1,count))*(pow(x,n)/(fact(n))));
        count++;
    }
    printf("%d",sum);
    }
    int fact(int f){
    int number = 1;
    int product = 1;
    while(number<=f){

         product = number*product;
         number++;

    }
    return product;
}

推荐答案


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

double fact(unsigned n)
{
  double result =1.0;
  unsigned int i;
  for (i=2; i<=n; ++i )
    result *= i;
  return result;
}

int main()
{
  unsigned terms;
  unsigned count=0;
  double result = 0.0;
  double angle;

  printf("enter number of terms ");
  scanf("%d",&terms);
  printf("enter value of x ");
  scanf("%lf",&angle);
  while(count <= terms)
  {
      double sign = count & 1 ? -1.0 : 1.0;
      unsigned n = 2*count+1;

      result += sign*(pow(angle,(double)n)/fact(n));
      count++;
  }
  double br = sin(angle);
  printf("approximation %g, builtin function result %g, difference  %5.2f%%", result, br, fabs(result-br)/br*100.0);
}


这篇关于用无穷级数评估sinx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-16 10:36