(1)(2).cpp

来自「数值计算各算法的程序,包括:牛顿迭代法,超松驰迭代法,微分法等.对于初学数值分析」· C++ 代码 · 共 51 行

CPP
51
字号
#include<iostream.h>
float fun1(float N)
{
	float Sn=0,i;
	for(i=2;i<=N;i++)
	{Sn=Sn+1/(i*i-1);}
	return Sn;
}
float fun2(float N)
{
	float Sn=0,i;
	for(i=N;i>=2;i--)
	{Sn=Sn+1/(i*i-1);}
	return Sn;
}
float fun3(float N)
{
	float Sn;
	Sn=1.0/2.0*(3.0/2.0-1/N-1/(N+1));
	return Sn;
}
void main()
{
	float N;
	cout<<"Please input N:";
	cin>>N;
	cout<<"按第一种算法得Sn为:"<<fun1(N)<<endl;
	cout<<"按第二种算法得Sn为:"<<fun2(N)<<endl;
	cout<<"精确值为:"<<fun3(N)<<endl;
}

/*程序运行结果为:
Please input N:100
按第一种算法得Sn为:0.740049
按第二种算法得Sn为:0.74005
精确值为:0.740049

Please input N:10000
按第一种算法得Sn为:0.749852
按第二种算法得Sn为:0.7499
精确值为:0.7499

Please input N:1000000
按第一种算法得Sn为:0.749852
按第二种算法得Sn为:0.749999
精确值为:0.749999
Press any key to continue*/



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?