作业不会,救命
根据Π=4-4/3+4/5-4/7......请打印一个表格表示:用公式中的1,2,3项。。。。依次计算出Π的近似值为3.14 3.141 3.1415 3.14159时分别使用了公式中的前几项
谢谢援助 什么意思,麻烦你说清楚 _2_ 发表于 2019-10-19 18:00
什么意思,麻烦你说清楚
利用这个式子分别计算3.14 3.141 3.1415 3.14159 时分别用了几个项,用表格打出来
这样的程序 SilverCrow 发表于 2019-10-19 18:05
利用这个式子分别计算3.14 3.141 3.1415 3.14159 时分别用了几个项,用表格打出来
这样的程序
忘看是C/C++板块了,这个我暂时不会 本帖最后由 jackz007 于 2019-10-19 18:42 编辑
楼主,pi 是无限不循环小数,所以,你所谓的 3.14、3.141、3.1415、3.14159 这几个数唯一的不同之处就是 pi 取了不同位数的小数而已,也就是说,只是把同一个数显示成不同的形式而已。与任何参数、算法真的是没有任何关系! jackz007 发表于 2019-10-19 18:40
楼主,pi 是无限不循环小数,所以,你所谓的 3.14、3.141、3.1415、3.14159 这几个数唯一的不同之 ...
这是作业,我也没办法 这个问题属于数值分析的内容,或者是离散数学。
这个题目的意思是给你了一个多项式,然后用这个多项式来去逼近Pi,在学科中,这个多项式是应该自己找寻的,题目直接给了出来。
几个问题是提出因为在计算机的多项式的计算都有精确度,根据我们需求的不同精确度计算机的在同一算法同一浮点运算的前提下,时间开销也是不同的,所以问题转化为若计算到3。14,3.141,3.1415,3.14159这个不同精确度下,需要计算机计算多少次(次数跟算法直接挂钩,我们知道循环次数和前提计算机的浮点运算速度,就能推断出时间开销,这个在科学运算中,十分很重要,因为一次循环往往就耗费大量时间,后续可以提升算法时间复杂度,提升计算机的浮点运算量,来尽量快的模拟结果)
下面是代码:
#include <iostream>
using namespace std;
int signal(int n);
float n_for_sum(int i);
int main()
{
bool check_for314=false;
bool check_for3141=false;
bool check_for31415=false;
bool check_for314159=false;
float sum=0;
int i=0;
while(1)
{
sum=sum+n_for_sum(i);
// printf("i = %d sum = %.6f\n",i,sum);
if(sum<3.144&& sum>3.135&&check_for314==false)
{
printf("before find 3.14 i-1= %d sum = %.6f\n",i-1,sum-n_for_sum(i-1));
printf("find approx 3.14i = %d sum = %.6f\n",i,sum);
printf("after find3.14i+1 = %d sum = %.6f\n",i+1,sum+n_for_sum(i+1));
check_for314=true;
printf("\n");
}
if(sum<3.1414&& sum>3.1405&&check_for3141==false)
{
printf("before find 3.141 i-1 = %d sum = %.6f\n",i-1,sum-n_for_sum(i-1));
printf("find approx 3.141 i = %d sum = %.6f\n",i,sum);
printf("after find3.141 i+1 = %d sum = %.6f\n",i+1,sum+n_for_sum(i+1));
check_for3141=true;
printf("\n");
}
if(sum<3.14154&& sum>3.14145&&check_for31415==false)
{
printf("before find 3.1415 i-1 = %d sum = %.6f\n",i-1,sum-n_for_sum(i-1));
printf("find approx 3.1415 i = %d sum = %.6f\n",i,sum);
printf("after find3.1415 i+1 = %d sum = %.6f\n",i+1,sum+n_for_sum(i+1));
check_for31415=true;
printf("\n");
}
if(sum<3.141594&& sum>3.141585)
{
printf("before find 3.14159 i-1 = %d sum = %.6f\n",i-1,sum-n_for_sum(i-1));
printf("find approx 3.14159 i = %d sum = %.6f\n",i,sum);
printf("after find3.14159 i+1 = %d sum = %.6f\n",i+1,sum+n_for_sum(i+1));
check_for314159=true;
printf("\n");
break;
}
i++;
}
return 0;
}
int signal(int n)
{
return (-1)*(n%2*2)+1;
}
float n_for_sum(int i)
{
return signal(i)*4.0/float(2*i+1);
}
说明下:每组的i是结果,我把前后两项也打出来进行比较,其实可以前后四项更科学,其实这个写法是局部最优下求全局最优的情况,具体证明过程就不写了。
结果如下:
before find 3.14 i-1= 150 sum = 3.121724
find approx 3.14i = 151 sum = 3.135013
after find3.14i+1 = 152 sum = 3.148128
before find 3.141 i-1 = 914 sum = 3.138315
find approx 3.141 i = 915 sum = 3.140502
after find3.141 i+1 = 916 sum = 3.142684
before find 3.1415 i-1 = 6696 sum = 3.141151
find approx 3.1415 i = 6697 sum = 3.141450
after find3.1415 i+1 = 6698 sum = 3.141749
before find 3.14159 i-1 = 91678 sum = 3.141563
find approx 3.14159 i = 91679 sum = 3.141585
after find3.14159 i+1 = 91680 sum = 3.141607
--------------------------------
Process exited after 0.1054 seconds with return value 0
请按任意键继续. . . 本帖最后由 matrixgadener 于 2019-10-20 02:04 编辑
这个是“数值分析”或“离散数学”中的问题
用到给你一个多项式,用多项式去逼近数值,因为在科学计算中,计算机对于不同的精确度计算的次数是不同的,在硬件计算机浮点运算固定情况下,尽量优化算法,降低时间复杂度。在科学计算中,往往循环一次就需要耗费大量时间。
问题转化为:通过计算机计算,分别达到3.14,3.141,3.1415,3.14159的精确度需要计算多少次?
#include <iostream>
using namespace std;
int signal(int n);
float n_for_sum(int i);
int main()
{
bool check_for314=false;
bool check_for3141=false;
bool check_for31415=false;
bool check_for314159=false;
float sum=0;
int i=0;
while(1)
{
sum=sum+n_for_sum(i);
// printf("i = %d sum = %.6f\n",i,sum);
if(sum<3.144&& sum>3.135&&check_for314==false)
{
printf("before find 3.14 i-1= %d sum = %.6f\n",i-1,sum-n_for_sum(i-1));
printf("find approx 3.14i = %d sum = %.6f\n",i,sum);
printf("after find3.14i+1 = %d sum = %.6f\n",i+1,sum+n_for_sum(i+1));
check_for314=true;
printf("\n");
}
if(sum<3.1414&& sum>3.1405&&check_for3141==false)
{
printf("before find 3.141 i-1 = %d sum = %.6f\n",i-1,sum-n_for_sum(i-1));
printf("find approx 3.141 i = %d sum = %.6f\n",i,sum);
printf("after find3.141 i+1 = %d sum = %.6f\n",i+1,sum+n_for_sum(i+1));
check_for3141=true;
printf("\n");
}
if(sum<3.14154&& sum>3.14145&&check_for31415==false)
{
printf("before find 3.1415 i-1 = %d sum = %.6f\n",i-1,sum-n_for_sum(i-1));
printf("find approx 3.1415 i = %d sum = %.6f\n",i,sum);
printf("after find3.1415 i+1 = %d sum = %.6f\n",i+1,sum+n_for_sum(i+1));
check_for31415=true;
printf("\n");
}
if(sum<3.141594&& sum>3.141585)
{
printf("before find 3.14159 i-1 = %d sum = %.6f\n",i-1,sum-n_for_sum(i-1));
printf("find approx 3.14159 i = %d sum = %.6f\n",i,sum);
printf("after find3.14159 i+1 = %d sum = %.6f\n",i+1,sum+n_for_sum(i+1));
check_for314159=true;
printf("\n");
break;
}
i++;
}
return 0;
}
int signal(int n)
{
return (-1)*(n%2*2)+1;
}
float n_for_sum(int i)
{
return signal(i)*4.0/float(2*i+1);
}
输出结果:
before find 3.14 i-1= 150 sum = 3.121724
find approx 3.14i = 151 sum = 3.135013
after find3.14i+1 = 152 sum = 3.148128
before find 3.141 i-1 = 914 sum = 3.138315
find approx 3.141 i = 915 sum = 3.140502
after find3.141 i+1 = 916 sum = 3.142684
before find 3.1415 i-1 = 6696 sum = 3.141151
find approx 3.1415 i = 6697 sum = 3.141450
after find3.1415 i+1 = 6698 sum = 3.141749
before find 3.14159 i-1 = 91678 sum = 3.141563
find approx 3.14159 i = 91679 sum = 3.141585
after find3.14159 i+1 = 91680 sum = 3.141607
--------------------------------
Process exited after 0.07908 seconds with return value 0
请按任意键继续. . .
说明:每组中的 i 是答案,但是将前后两项也打了出来,其实这里原理在于局部最优情况下的全局最优解,具体理论推导这里不累述了。 本帖最后由 bin554385863 于 2019-10-20 03:06 编辑
{:10_256:}
func(double pi)
{
int a = 1;
double result = 0, m = 0, n = 0;
while (result != pi)
{
m = pow(-1, a - 1) * (2 * a - 1);
n = 1 / m;
result += 4 * n;
std::cout<<"4/"<<m<<"";
a++;
}
}
页:
[1]