正弦系列说明 [英] sine series Explanation

查看:103
本文介绍了正弦系列说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sin(x)的值可以使用公式:

sin(x)= x -x ^ 3/3!+ x ^ 5/5!-x ^ 7/7 !+ x ^ 9/9!···



任何人解释while循环如何在此程序中运行..



如果我给sin90的结果是1.57这是否正确...









The value of sin(x) can be using the formula:
sin(x) = x −x^3/3!+x^5/5!−x^7/7!+x^9/9!· · ·

Any one Explain How the while loop is working in this program..

and if i give sin90 the result is 1.57 is this correct or not...




#include<stdio.h> 
#include<math.h> 
void main() 
{ 
  int i = 2, n, s = 1, x, pwr = 1, dr; 
  float nr = 1, x1, sum; 
  clrscr(); 
  printf("\n\n\t ENTER THE ANGLE...: "); 
  scanf("%d", &x); 
  x1 = 3.142 * (x / 180.0); 
  sum = x1; 
  printf("\n\t ENTER THE NUMBER OF TERMS...: "); 
  scanf("%d", &n); 
  while(i <= n) 
  { 
    pwr = pwr + 2; 
    dr = dr * pwr * (pwr - 1); 
    sum = sum + (nr / dr) * s; 
    s = s * (-1); 
    nr = nr * x1 * x1; 
    i+= 2; 
  } 
  printf("\n\t THE SUM OF THE SINE SERIES IS..: %0.3f",sum); 
  getch(); 
  }

推荐答案

显然90度的正弦值是1而不是1.57。



[在CPallini的评论之后]

这是一个有效的工作:

Apparently sine of 90 degree is one and not 1.57.

[After CPallini's comment]
Here is a working one:
int i = 0, iterations, sign = -1, currentN = 1, factorial = 1;
  float power, radian, sum, degree;

  Console.Write("\nAngle(D) ? ");
  degree = float.Parse(Console.ReadLine());
  radian = 3.14159265f * (degree / 180.0f);
  sum = radian;
  power = radian;
  Console.Write("\nNumber of iterations ? ");
  iterations = int.Parse(Console.ReadLine());

  while (i++ < iterations)
  {
      currentN = currentN + 2;
      factorial = factorial * currentN * (currentN - 1);
      power = power * radian * radian;
      sum = sum + (power / factorial) * sign;
      sign = sign * (-1);
  }

  Console.WriteLine("Sine of {0}(D) is {1}", degree,sum);
  Console.ReadLine();



给它15次迭代会产生一个好的结果。

这是在C#但只是转换 WriteLine printf ReadLine scanf getch 并且效果很好。


Giving it a 15 iteration will produce a good result.
It's in C# but just convert WriteLine to printf and ReadLine to scanf and getch and it works well.


您发布的代码错误

例如 dr 未初始化,你也可能发现它错误地实现了 sin 公式。



以下代码更好一点:

The code you posted is wrong.
For instance dr is not initialized, also you may find it implements incorrectly the expansion of the sin formula.

The following code is a bit better:
#include<stdio.h> 
#include<math.h> 
void main() 
{ 
  int i = 2, n, s = 1, x, pwr = 1, dr=1; 
  float nr, x1, sum; 

  clrscr(); 
  printf("\n\n\t ENTER THE ANGLE...: "); 
  scanf("%d", &x); 
  x1 = 3.142 * (x / 180.0); 
  sum = 0; 
  nr = x1;
  printf("\n\t ENTER THE NUMBER OF TERMS...: "); 
  scanf("%d", &n); 
  while(i <= n) 
  {
    sum = sum + (nr / dr) * s; 
    pwr = pwr + 2; 
    dr = dr * pwr * (pwr - 1); 
    s = s * (-1); 
    nr = nr * x1 * x1; 
    i += 2; 
  } 
  printf("\n\t THE SUM OF THE SINE SERIES IS..: %0.3f",sum); 
  getch(); 
  }


没有人可以回答是否正确,因为你没有告诉我们预期的结果。



要了解它是如何工作的,请使用调试器。没有什么比这更好的了。还有什么?..写程序。你明白了吗?



-SA
Nobody can answer if it is correct or not, because you did not tell us what is expected.

To see how it works, use the debugger. There is nothing better than that. What else?.. Write programs. Do you see the point?

—SA


这篇关于正弦系列说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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