如何使用正弦系列找到正弦值 [英] How can I find sine value with using sine series

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

问题描述

我想用正弦系列找到x的罪。 xx ^ 3/3!+ ......

i希望使用pow功能找到这个



添加了代码块 - OriginalGriff [/ edit]



我的尝试:



  #include   <   stdio.h  >  
#include < math.h >
int main()
{
int i,j,n,fact,ch,p;
float x,sum,angle;
执行
{
printf( \ n1。正弦系列\ n2。余弦系列\ n3。指数系列\ n4。退出\ n输入你的选择:);
scanf( %d,& ch);
switch (ch)
{
case 1
printf( \ n输入x的值(以度为单位)和n在正弦系列中。\ n);
scanf( %f,& x);
printf( e);
scanf( %d,& n);
printf( \ n%d,n);
angle = x;
printf( hi);
x = angle * 3 .14 / 180;
fact = 1 ;
sum = 0 ;
p = 0 ;
for (i = 1 ; i< = n; i + 2)
{
for (j = 2 ; j< = n; j ++)
{
fact = fact * j;
}
// printf(\ n%d,fact);
sum + =(pow( - 1 ,p)*(( float )pow (X,I)/事实));
p ++;
}
printf( \ nsin(%。2f)=%。2f \ N,角度之和);
break ;
case 2
printf( \ n输入x的值(以度为单位)和n在余弦系列中的值。\ n);
scanf( %f%d,& x,& n);
angle = x;
x = x * 3 .14 / 180;
fact = 1 ;
sum = 0 ;
for (i = 0 ,p = 0 ; i< = n; i + 2,p ++)
{
for (j = 1 ; j< = n; j ++)
{
fact * = j;
}
sum + =(pow( - 1 ,p)*(( float )POW(X,I)/事实));
}
printf( \ ncos(%。2f)=%。2f \ N,角度之和);
break ;
case 3
printf( \ n输入x的值(以度为单位)和n以指数级数表示。\ n);
scanf( %f%d,& x,& n);
angle = x;
x = x * 3 .14 / 180;
fact = 1 ;
sum = 0 ;
for (i = 0 ; i< n; i ++)
{
for (j = 1 ; j< = n; j ++)
{
fact * = j;
}
sum + =(( float )pow(x,i)/ fact);
}
printf( \\\
e ^(%。2f)=%。2f \\ \
,角度之和);
break ;
case 4
exit( 0 ); break ;
默认:printf( \ n输入无效\\ n);
}
} while 1 );
return 0 ;
}

解决方案

我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



因此,它将取决于您。

在函数的第一行放置一个断点,然后通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


是时候学习一门新的(非常非常有用的)技能了:调试!



亲自尝试一下,你可能会发现它并不像你想象的那么困难。


引用:

我想用正弦系列找到x的罪。 xx ^ 3/3!+ ......

i希望使用pow功能找到它



然后这样做,开始工作。



我们不做你的HomeWork。

HomeWork不会测试你乞求别人做你的工作的技巧,它会被设定为您可以考虑并帮助您的老师检查您对所学课程的理解,以及您在应用这些课程时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

所以,试一试,重读你的课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。



作为程序员,您的工作是创建算法解决特定问题,你不能依赖别人永远为你做,所以有一段时间你必须学会​​如何。而且越快越好。

当你要求解决方案时,就像试图通过培训其他人来学习开车一样。

创建算法基本上是找到数学并进行必要的调整以适应你的实际问题。


有几个错误。例如

Quote:

for(i = 1; i< = n; i + 2)



错了,for子句中的最后一个语句没有效果,它应该是 i + = 2 。此外,必须在factorial循环之前立即初始化 fact 变量(顺便说一句,最好使用 float 用于保存阶乘结果的变量,因为它很快变大。

您可以这样修复代码:

  #include   <   stdio。 h  >  
#include < stdlib.h >
#include < math.h >
int main()
{
int i,j,n,ch,p;
float 事实;
float x,sum,angle;
执行
{
printf( \ n1。正弦系列\ n2。余弦系列\ n3。指数系列\ n4。退出\ n输入你的选择:);
scanf( %d,& ch);
switch (ch)
{
case 1
printf( \ n输入x的值(以度为单位)和n在正弦系列中。\ n);
scanf( %f,& x);
scanf( %d,& n);
angle = x;
x = x * M_PI / 180;
sum = 0 0 ;
p = 0 ;
for (i = 1 ; i< = n; i + = 2
{
fact = 1 ;
for (j = 2 ; j< = i; j ++)
{
fact = fact * j;
}
sum + =(pow( - 1 ,p)*(( float )POW(X,I)/事实));
p ++;
}
printf( \ nsin(%。2f)=%。2f \ N,角度之和);
break ;
// ...





但是我会在函数中计算 sin 计算,我还会逐步计算这些项,例如

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

double comp_sin( double x, int n)
{
double sign = 1 0 ;
double fact = 1 0 ;
double sum = 0 0 ;
double x2 = x * x;

int k;

for (k = 0 ; k< n; ++ k)
{
sum + =(sign * x)/ fact;
// 更新因素
sign = sign> 0 0 ? - 1 0 1 。< span class =code-digit> 0
; // 切换符号
fact * =( 2 0 * k + 2 0 )*( 2 0 * k + 3 0 ); // 计算下一个阶乘因子
x * = x2; // 计算下一个权力因素
}
return sum;
}


int main()
{
int n;
double 角度;

printf( 请输入degres的角度和系列的术语数量\
);
scanf( %lf,& angle);
scanf( %d,& n);

printf( sin(%g)=%g \ n,angle,comp_sin(angle * M_PI / 180,n));

return 0 ;
}


i want to find sin of x using sine series . x-x^3/3!+......
i want to find this using pow function

[edit]Code block added - OriginalGriff[/edit]

What I have tried:

#include<stdio.h>
#include<math.h>
int main()
{
  int i,j,n,fact,ch,p;
  float x,sum,angle;
   do
    {
      printf("\n1. Sine series\n2. Cosine series\n3. Exponential series\n4. Exit\nEnter your choice: ");
      scanf("%d",&ch);
      switch(ch)
	{
	case 1:
	  printf("\nEnter the value of x(in degree)and n in sine series.\n");
	  scanf("%f",&x);
	  printf("e");
	  scanf("%d",&n);
	  printf("\n%d",n);
       	  angle=x;
	  printf("hi");
	  x=angle*3.14/180;
	  fact=1;
	  sum=0;
	  p=0;
	  for(i=1;i<=n;i+2)
	    {
	      for(j=2;j<=n;j++)
		{
		  fact=fact*j;
		}
	      //printf("\n%d",fact);
	      sum+=(pow(-1,p)*((float)pow(x,i)/fact));
	      p++;
	    }
	  printf("\nsin(%.2f)=%.2f\n",angle,sum);
	  break;
	case 2:
	  printf("\nEnter the value of x(in degree)and n in cosine series .\n");
	  scanf("%f%d",&x,&n);
	  angle=x;
	  x=x*3.14/180;
	  fact=1;
	  sum=0;
	  for(i=0,p=0;i<=n;i+2,p++)
	    {
	      for(j=1;j<=n;j++)
		{
		  fact*=j;
		}
	      sum+=(pow(-1,p)*((float)pow(x,i)/fact));
	    }
	  printf("\ncos(%.2f)=%.2f\n",angle,sum);
	  break;
	case 3:
	  printf("\nEnter the value of x(in degree)and n in exponential series.\n");
	  scanf("%f%d",&x,&n);
	  angle=x;
	  x=x*3.14/180;
	  fact=1;
	  sum=0;
	  for(i=0;i<n;i++)
	    {
	      for(j=1;j<=n;j++)
		{
		  fact*=j;
		}
	      sum+=((float)pow(x,i)/fact);
	    }
	  printf("\ne^(%.2f)=%.2f\n",angle,sum);
	  break;
	case 4:
	  exit(0);break;
	  default:printf("\n Invalid input\n");
	}
       }while(1);
    return 0;
}

解决方案

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Time for you to learn a new (and very, very useful) skill: debugging!

Try it yourself, you may find it is not as difficult as you think.


Quote:

i want to find sin of x using sine series . x-x^3/3!+......
i want to find this using pow function


Then do it, start working.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.


There are several errors. For instance

Quote:

for(i=1;i<=n;i+2)


is wrong, the last statement in the for clause ha no effect, it should be i+=2. Moreover, the fact variable must be initialized immediately before the factorial loop (by the way, it is better using a float variable for holding the factorial result, because it becomes large soon).
You might fix your code this way:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
  int i,j,n,ch,p;
  float fact;
  float x,sum,angle;
  do
  {
    printf("\n1. Sine series\n2. Cosine series\n3. Exponential series\n4. Exit\nEnter your choice: ");
    scanf("%d",&ch);
    switch(ch)
    {
    case 1:
      printf("\nEnter the value of x(in degree)and n in sine series.\n");
      scanf("%f",&x);
      scanf("%d",&n);
      angle = x;
      x=x*M_PI/180;
      sum=0.0;
      p=0;
      for(i=1;i<=n;i+=2)
      {
        fact=1;
        for(j=2;j<=i;j++)
        {
          fact=fact*j;
        }
        sum+=(pow(-1,p)*((float)pow(x,i)/fact));
        p++;
      }
      printf("\nsin(%.2f)=%.2f\n",angle,sum);
    break;
    //...



However I would factor out the sin computation in a function, and I would also compute the terms incrementally, e.g.

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

double comp_sin( double x, int n)
{
  double sign = 1.0;
  double fact = 1.0;
  double sum=0.0;
  double x2 = x * x;

  int k;

  for (k=0; k<n; ++k)
  {
    sum += (sign * x) / fact;
    // update the factors
    sign = sign > 0.0 ? -1.0 : 1.0; // toggle the sign
    fact *= (2.0 * k + 2.0) * (2.0 * k + 3.0); // compute the next 'factorial' factor
    x *= x2; // compute the next 'power' factor
  }
  return sum;
}


int main()
{
  int n;
  double angle;

  printf("please enter the angle in degres and the number of terms of the series\n");
  scanf("%lf", &angle);
  scanf("%d", &n);

  printf("sin(%g) = %g\n", angle, comp_sin(angle*M_PI/180, n ));

  return 0;
}


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

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