主页 > 微评测 >C语言编程求函数y=sinx在(0,1)区间的定积分?

C语言编程求函数y=sinx在(0,1)区间的定积分?

作者: 时间:2020-07-04 528° 微评测
C语言编程求函数y=sinx在(0,1)区间的定积分?
#include
#include

#define N 10000000

void main()
{
double sum=0;
int i;
for(i=0; i {
sum += sin((double)(i)/N)/N;
}
printf("%lf\n%lf",sum,1-cos(1));
}

N后面的0有了,不过这个数刚好能精确到小数点后6位。
当然个程序的算法是太垃圾了,效率非常的低下……#include
#include

int main(void)

{
double y=0, i;

for (i = 0; i < 1; i = i + 0.0001)//根据你度需求,在这长。
{
y = sin(i) + y;
}
printf("%f", y * 0.0001);

return 1;
}
c语言编程:编程并计算sin(x)的值

#include
#include

doublesin(double);
doublenResult(double,double);

intmain()
{
doublex=0;
scanf("%lf",&x);
printf("sin(%lf)=%lf ",x,sin(x));
return0;
}

doublesin(doublex)
{
//sin(x)=x-x^3/3!+x^5/5!-x^7/7!+…+(-1)(n^2n+1)/(2n+1)!+……
inti=0;
doubleresult=0,n=0;
while(fabs(n=nResult(x,2*++i-1))>0e-5)//绝对值大于10^-5就循环
result+=(i%2==1)?n:-n;
returnresult;
}

doublenResult(doublex,doublen)
{
//(n^2n+1)/(2n+1)!也n/1*n/2*n/3*n/4*.....n/(2n+1)
returnn==1?x:nResult(x,n-1)*x/n;
}

用递归给写了一个 sin(π)=0 sin(π/2)=1楼主看得上就采纳吧


c语言编程求sinx的近似值(泰勒展开)?
您好,是这样:泰勒展开是这个:sinx=x-x^3/3!+x^5/5!-..
下面给出20项的程序。

#include"math.h"
#include"stdio.h"
void main()
{
double x=0,y=0,z=1,s=1,mynum=0;
int i=1 ,j=0, k=1;
scanf("x=%f",&x);

for(i=1;i<20;i++)

z=1;k=1;
for(j=1;j<=2*i-1;j++)
{
z=x*z;//算j次方
k=k*j;//算阶乘}
s=-j*pow(-1,i);//pow(a,b)是a的b次方
z=z*s/k;

mymun=mynum+z;
}
printf("sinx=x-x^3/3!+x^5/5!-..");
printf("sinx=%f",mynum);
getch();
}
c语言编程,利用泰勒级数求解sinx,输入x值,求解sinx值,保留小数点后6位。
#include
#include
double fpow_div_fact(double num,unsigned int index);
int main(int argc, char const *argv[])
{
double x;
double x_save ;
scanf("%lf",&x);
unsigned int loop = 0;
double cur_taylor = 0;
double taylor_series = 0;//泰勒
x_save = x;
x *=3.1415926 / 180;
do
{
cur_taylor = fpow_div_fact(x, 2*loop + 1);
taylor_series += ((loop%2 == 0)?1:-1)*cur_taylor;
loop++;
}while(fabs(cur_taylor) >= 1e-5);
printf("sin(%.2f) = %.6f\n",x_save,taylor_series);
return 0;
}

/*分解幂运算和阶乘运算,证幂和阶乘运会溢出*/
double fpow_div_fact(double num,unsigned int index)
{
double temp = 1.0;
unsigned int index_temp = index;//阶乘的
int i = 1;

for ( i = 1; i <= index; ++i)
{
temp *= num / index_temp;
index_temp--;
}
return temp;
}

//#include"stdafx.h"//Ifthevc++6.0,withthisline.
#include"stdio.h"
intmain(void){
inti,t;
doublesum,x,tmp,xx;
printf("Inputx(real-360+360°)... ");
scanf("%lf",&x);
xx=x,x*=3.1415926535897932/180;printf("%f ",xx);
for(tmp=sum=x,i=2;tmp>1.0E-5;i++){
t=(i<<1)-1;
sum+=i&1?tmp*=(x*x/t/(t-1)):-(tmp*=(x*x/t/(t-1)));
}
printf("sin(%g°)=%.6f ",xx,sum);
return0;
}本回答被网友采纳

上一篇:
下一篇:

中国食品科技网|广州友邦资讯科技招聘|深圳科技资讯|网站地图