急求C语言/c++编程,用幂级数求圆周率近似值.

这里已知pi=16arctan1/5-4arctan1/239,要求运用arctanx幂级数展开式建立数值计算方法通过上式计算pi,并且误差小于10^-5.下面是我编写的一个程序,但运行不出结果,请高手纠正错误.高手们也可自己编写能运行的程序发来,不胜感激.arctanx幂级数展开式我就不写在这了.
#include<stdio.h>
#include<math.h>
double Arctan(double a,float x)
{
int i,s=1;
double d=0,d1=0,d2;
double y=0;
do
{
for(i=0;i<=30000;i++)
{ d2=d1;
y=pow(x,2*i-1);
d+=s*y/(double)(2*i-1);
s*=-1;
i++;
d1=s;
}
}while(fabs(d1-d2)>=1e-5);
return a*d1;
}
void main(){
double y1=0;
double y2=0;
double y3=0;
y1=Arctan(16,1/5);
printf("%lf",y1);
y2=Arctan(4,1/239);
y3=y1-y2;
printf("%lf",y3);}

#include<stdio.h>
#include<math.h>
double Arctan(double a,float x)
{
int i,s=1;
double d=0,d1=0,d2;
double y=0;
do
{
for(i=0;i<=30000;i++)
{ d2=d1;
y=pow(x,2*i-1);
d+=s*y/(double)(2*i-1);
s*=-1;
i++;
d1=s;
}
}while(fabs(d1-d2)>=1e-5);
return a*d1;
}
void main(){
double y1=0;
double y2=0;
double y3=0;
y1=Arctan(16,1/5);
printf("%lf",y1);
y2=Arctan(4,1/239);
y3=y1-y2;
printf("%lf",y3);}

第1个回答  推荐于2018-04-12
//下面的程序经测试过是正确的。
#include<stdio.h>
#include<math.h>
double Arctan(double a,float x)
{
int i=0,s=1;
double d=0,d1=0,d2;
double y=0;
do
{
d2=d1;
y=pow(x,2*i+1);
d+=s*y/(double)(2*i+1);
s*=-1;
i++;
d1=d;
}
while(fabs(d1-d2)>=1e-5);
return a*d1;
}
void main()
{
double y1=0;
double y2=0;
double y3=0;
y1=Arctan(16,1.0/5);
printf("%lf\n",y1);
y2=Arctan(4,1.0/239);
y3=y1-y2;
printf("%lf\n",y3);
}本回答被网友采纳
第2个回答  2012-12-16
#include<stdio.h>#include<math.h>
double Arctan(double a,float x)
{ //
int xishu = 1;
double arctanxold=0.0, arctanxnew = 0.0;
int n=1;
// arctanx = x - x^3/3 + x^5/5 + (-1)^(n-1)*[x^(2n-1)/(2n-1)]
do
{
arctanxold = arctanxnew;
arctanxnew += powf(x, (2*n-1))/(2*n-1) * xishu; // 这里要使用powf做小数的幂运算
xishu *= -1;
n++;
}while(fabs(arctanxnew-arctanxold)>=1e-5);

return a*arctanxnew;
}

void main()
{
double y1=0;
double y2=0;
double y3=0;
y1=Arctan(16,1.0/5); // 这里要使用一个小数作为系数来做, 否则 1/5=0
printf("%lf\n",y1);
y2=Arctan(4,1.0/239);
y3=y1-y2;
printf("%lf\n",y3);
}来自:求助得到的回答
第2个回答  2012-12-16
粗略滴看了下,不知道是否是d1=s这个有些问题。。可能是d1=d??
第3个回答  2012-12-16
错误比较多。
计算反正切的函数有错;
y1=Arctan(16,1/5);右边的1/5明显结果为0好吧。

#include<stdio.h>
#include<math.h>
double Arctan(double a,float x)
{
int i = 1,s=1;
double d=0,d1=0,d2;
double y=0;
do //do...while里面就不要再用for了。

{
d2=d1;
y=pow(x,2*i-1);

d+=s*y/(double)(2*i-1);
s*=-1;
i++;
d1=d; //这里你也写错了。

}while(fabs(d1-d2)>=1e-5);
return a*d1;
}
void main(){
double y1=0;
double y2=0;
double y3=0;
y1=Arctan(16,1.0/5); //打印y1没有意义;另外注意1/5的结果为0

y2=Arctan(4,1.0/239);
y3=y1-y2;
printf("%lf",y3);}
第4个回答  2012-12-16
有C和C++的课程视 频,是否可以帮到你呢?
希望对你有用!

记得采纳噢追问

可以啊,只要是有关的就行?请提供链接或直接发至邮箱[email protected]

追答

内容很多,你可以直接通过百度搜索“猎豹网校”就可以了。

相似回答