【工程数学】若干种计算圆周率的算法

准确的PI=3.1415926535897931................

/*
*函数功能:用反正切方法计算圆周率
*函数原形:double PiByArctan(double acc);
*参数:double acc;该计算结果的精度
*返回值:所求圆周率
*时间复杂度:O(n)
*备注:无
*日期:2014/12/1
*原创:是
*作者:EbowTang
*Email:tangyibiao520@163.com
*/
#include "stdafx.h"
#include "iostream"
#include <iomanip>
#include <time.h
using namespace std;
double PiByArctan(double acc);
int _tmain(int argc, _TCHAR* argv[])
{
	double accrucy=0.0001;
	double res=0.0;
	clock_t start, finish; 
	double Totaltime; 
	while (true)
	{
		cout<<"请输入精度:"<<endl;
		cin>>accrucy;
		start=clock();
		res=PiByArctan(accrucy);
		cout<<"计算结果为:"<<setprecision(9) <<res<<"   ";
		finish=clock();
		Totaltime=finish-start;
		cout<<"程序所花时间为:"<<Totaltime<<"毫秒"<<endl;
	}
	system("pause");
	return 0;
}
double PiByArctan(double acc)
{
	double tempacc=1.0;
	int n=1;
	int	f=-1;//正负系数
	double sum=0.0;
	int count=0;//用于统计迭代次数
	while (tempacc>acc)
	{
		f=-f;
		tempacc=1.0/n;
		sum=sum+f*tempacc;
		n=n+2;
		count++;
	}
	sum=4*sum;
	cout<<"反正切函数迭代次数:"<<count<<"  ";
	return sum;
}


 

/*
*函数功能:用蒙卡罗特方法计算圆周率
*函数原形:double Menkalote( int Runs);
*参数:int Runs;
*返回值:所求圆周率
*时间复杂度:O(n^2)
*备注:无
*日期:2014/12/2
*原创:是
*作者:EbowTang
*Email:tangyibiao520@163.com
*/
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <time.h>
using namespace std;
double Menkalote( int );
int _tmain(int argc, _TCHAR* argv[])
{
	int Runs=0;
	double pi=0.0;
	int count=0;
	clock_t start, finish; 
	double totaltime=0.0;
	while (true)
	{
		cout<<"请输入“打靶”点数(只能测试10次):"<<endl;
		cin>>Runs;
		start = clock(); 
		pi=Menkalote(Runs);
		cout<<"PI="<<setprecision(9)<<pi<<"   ";
		finish = clock(); 
		count++;
		if (count>10)
			break;
		totaltime=finish-start;
		cout<<"程序所花时间为:"<<totaltime<<"毫秒"<<endl;
	}
	system("pause");
	return 0;
}
double Menkalote(int Runs)
{
	srand(unsigned int(time(NULL)));//初始化随机函数种子
	const int MAX_TIMES = Runs;
	int inside = 0;//记录1/4圆的点个数
	for(int i = 0; i < MAX_TIMES; ++i) {	
		double x = static_cast<double>(rand()) / RAND_MAX;
		double y = static_cast<double>(rand()) / RAND_MAX;
		if(x * x + y * y <= 1.0)
			++inside;
	}	
	double pi = 4.0 * inside / MAX_TIMES;
	return pi;
}


// ConsoleAppPiByIntegral.cpp : 定义控制台应用程序的入口点。
//
/*
*函数功能:用数值积分法计算圆周率
*函数原形:double PiByIntegral(double a,double b,int n);
*参数:double a,double b,int n,前两个是积分区间,后一个积分区间的分隔数
*返回值:圆周率
*时间复杂度:O(n)
*备注:无
*日期:2014/12/3
*原创:是
*作者:EbowTang
*Email:tangyibiao520@163.com
*/
#include "stdafx.h"
#include "iostream"
#include <iomanip>
#include <time.h>

using namespace std;

double Func(double x);
double PiByIntegral(double a,double b,int n);
int _tmain(int argc, _TCHAR* argv[])
{
	double x=0.0;
	double y=1.0;//积分区间
	int n=10;
	int count=0;
	clock_t start, finish; 
	double totaltime=0.0;
	while (true)
	{
		cout<<"请输入积分区间分隔数:"<<endl;
		cin>>n;
		start=clock();
		cout<<"PI="<<setprecision(9)<<PiByIntegral(x,y,n)<<"  ";
		finish=clock();
		totaltime=finish-start;
		cout<<"程序所花时间为:"<<totaltime<<"毫秒"<<endl;
		count++;
		if (count>10)
		break;
	}
	system("pause");
	return 0;
}

double Func(double x)
{
	return 1.0/(x*x+1);
}

double PiByIntegral(double a,double b,int n)
{
	double h=(b-a)/n;
	double t=0.0;
	double xk=0.0;
	for (int k = 0; k < n; k++)
	{
		xk=a+k*h;
		t+=Func(xk);
	}
	t=h*(Func(a)+Func(b))/2+t*h;
	t=4.0*t;
	return t;
}
<pre name="code" class="cpp">/<span style="font-family: Arial, Helvetica, sans-serif;">*也可以写成这种表达式,反正换汤不换药的本质!</span>
<span style="font-family: Arial, Helvetica, sans-serif;">double PiByIntegral(double a,double b,int n)  </span>
{  
<span>	</span>double h=(b-a)/n;  
<span>	</span>double t=0.0;  
<span>	</span>double xk=0.0;  
<span>	</span>double sum=0.0;
<span>	</span>for (int k = 0; k <= n; k++)  
<span>	</span>{  
<span>		</span>xk=a+k*h;  
<span>		</span>t+=Func(xk);  
<span>	</span>}     
<span>	</span>sum=h*t-h*(Func(a)+Func(b))/2;
<span>	</span>return 4*sum;  
}
*/
 
 


最后再数据拟合看看时间和有效正确精度的走势(拟合效果不好):

1,反正切法拟合的结果


2,蒙卡罗特法的拟合结果


3,数值积分法的拟合结果


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值