使用 C++语言并采用蒙特卡洛方法求图 1 中阴影部分的面积,

作者&投稿:陈没米 (若有异议请与网页底部的电邮联系)
c++利用蒙特卡洛方法求解圆周率结果为什么是0~

算法思路如下:
假设一个变长为2的正方形,内接一个半径为1的圆,那么四边形的面积为4,圆形的面积是3.14,开始随机产生点,那么落在圆内的数目应该在N*3.14/4

随便写了下,也没怎么检查,大致算法就是这样。你可以参考参考。

#include
#include
#include
using namespace std;

const double PI = acos(-1);
const int MAXR = 999;
double interval[MAXR+10];
int n;
const double a = 0;
const double b = PI/2;

double func(double x)
{
return 4/(3*sqrt(1-sin(PI/12)*sin(PI/12)*sin(x)*sin(x)));
}

void genRandomInterval(double a,double b)
{
srand( (unsigned)time( NULL ) );
n=rand()%MAXR;
if(n%2==0)n++;
interval[0]=a;
interval[n+1]=b;
for(int i=1;i<=n;i++)
interval[i]=a+(b-a)*(i+0.5)/n;
}

double integral()
{
double ret=0;
genRandomInterval(a,b);
for(int i=1;i<=n;i++)
ret+=func(interval[i])*(interval[i+1]-interval[i-1]);
return ret;
}

int main()
{
int m;
cout<<"Please input the experiment time: ";
cin>>m;
cout<<"Processing "<<m<<" experiments and output the average value."<<endl;
double ans=0;
for(int i=0;i<m;i++)
ans+=integral();
ans/=m;
cout<<"The average value is "<<ans<<endl;
system("pause");
}

将图形变换一下,得下图.建立坐标系.利用蒙特卡洛方法求阴影面积如下:

阴影面积=落在阴影部分的坐标点个数÷ 生成的全部坐标点个数×正方形面积

运行结果:

代码:

#include <iostream>
#include <iomanip>
#define MAX_ITERS 10000000 //生成坐标点总个数

using namespace std;

struct Point
{
    double x, y;
};

double Rand(double L, double R) //随机数 
{  
    return L + (R - L) * rand() * 1.0 / RAND_MAX;  


Point getPoint() //生成坐标点
{
    Point t;
    t.x = Rand(-5.0, 5.0);
    t.y = Rand(-5.0, 5.0);
    return t;
}

double getResult()//运行
{
    int m = 0;
    int n = MAX_ITERS;
    srand(time(NULL));
    for(int i = 0; i < n; i++)
    {
        Point t = getPoint();
        double res = t.x*t.x+t.y*t.y;
        if(res > 25){ //不在圆内
         if(t.x>0 && t.y>0.5*t.x+2.5)//除去斜线以上右上角的点
         continue;
         else
             m++;
        } 
    }  
    return m*100.0/MAX_ITERS;
}

int main()
{
    for(int i = 0; i < 20; i++)
        cout << fixed << setprecision(6) << getResult() << endl;
    return 0;
}



颍东区17070029348: 使用 C++语言并采用蒙特卡洛方法求图 1 中阴影部分的面积, -
策肢坦立: 将图形变换一下,得下图.建立坐标系.利用蒙特卡洛方法求阴影面积如下:阴影面积=落在阴影部分的坐标点个数÷ 生成的全部坐标点个数*正方形面积运行结果:代码:#include <iostream>#include <iomanip>#define MAX_ITERS 10000000 //生...

颍东区17070029348: C++ 编程 利用蒙特卡罗模拟方法,得到sinα在积分区间0到pi的近似结果 -
策肢坦立: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 //****** //Answer for: //http://zhidao.baidu.com/question/544284803.html?fr=uc_push&push=help&entry=...

颍东区17070029348: 蒲丰投针问题的蒙特卡洛方法 -
策肢坦立: 在用传统方法难以解决的问题中,有很大一部分可以用概率模型进行描述.由于这类模型含有不确定的随机因素,分析起来通常比确定性的模型困难.有的模型难以作定量分析,得不到解析的结果,或者是虽有解析结果,但计算代价太大以至不能...

颍东区17070029348: 如何运用c++里的“ -- stdcall”? -
策肢坦立: 1、 __stdcall调用类型:该调用只是通过堆栈来push和pop参数.push参数时,顺序是从右到左. 2、“_stdcall”的作用 :在C/C++中函数默认Calling Conventions(调用约定)是:参数由右向左压入栈,由调用者清空栈. 3、在FORTRAN、...

颍东区17070029348: 怎样使用C++语言书写迭代法的Fibonacci数列 -
策肢坦立: f(0)=0;f(1)=1;f(2)=1 f(x)=f(x-1)+f(x-2) int f(int x) { if(x==0) return 0; else if(x==1||x==2) return 1; else return (f(x-1)+f(x-2)); } void main() { for(int i=0;i<100;i++) { cout<<f(i)<<endl; } }

颍东区17070029348: 用C++编程实现 替换加密法 -
策肢坦立: #include void main() { char Code[5]="LOVE"; char *p =new char [4]; int i = 0; while(Code[i] != '\0') { p[i] = Code[i]+3; cout< i++; } cout< } 输出为:ORYH Press any key to continue

颍东区17070029348: c/c++高手请进
策肢坦立: 补充:几种初等函数的复合情形更是难上加难,像exp(x)*sin(x)-ln(x^2) 蒙特卡罗法计算定积分就是用一块已知面积的区域括住所求区域,再这个区域进行投针实验(用C的随机函数产生点的坐标),记下落在所求区域的次数n,n/N=s/S(N为所有点的个数, S为已知面积,s为所求)

颍东区17070029348: 编写一个程序 要求使用c++语言编写在c++软件中可以看到结果!
策肢坦立: 是数据结构的练习吧,涉及到算法比较内容比较难!自己自己研究下,函数代码如下: *最短路径问题*/ #include"stdio.h" #define INFINITY 10000 #define MaxVertexNum 20 #define FALSE 0 #define TRUE 1 typedef char VertexType; typedef ...

颍东区17070029348: 求数字分组算法,请使用C或C++语言 -
策肢坦立: #include "stdafx.h"#include #include #include using namespace std ;#define MAX 100 int g_nArr[MAX] ; int N ; // the number of items ...int M ; // the maximum value ...bool make( int nArray , int nRemain = M ) { static int nCount = 0 ; // defined ...

颍东区17070029348: 计算机二级C++考什么 -
策肢坦立: 计算机二级C++考试大纲考试大全基本要求 1、掌握C++语言的基本语法规则. 2、熟练掌握有关类与对象的相关知识. 3、能够阅读和分析C++程序. 4、能够采用面向对象的编程思路和方法编写应用程序....

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网