`

Calculate the integral using Monte Carlo method

阅读更多
#include "stdio.h"
#include "stdlib.h"
#include "math.h"

double rnd1(double *r)  //the definition body of rnd1
{ int m;
  double s, u, v, p;
  s=65536.0; u=2053.0; v=13849.0;
  m=(int)(*r/s); *r=*r-m*s;
  *r=u*(*r)+v; m=(int)(*r/s);
  *r=*r-m*s; p=*r/s;
  return(p);
}

double mtml(int n, double a[], double b[], double (*f)())  /*n为积分的维度,数组a保存着积分下限,
                                                                                                                        数组b保存着积分上限,f为函数指针,指向被积函数*/
{
    int m, i;
    double r, s, d, *x;
    x=malloc(n*sizeof(double));
    r=1.0; d=10000.0; s=0.0;
    for (m=0; m<=9999; m++)
    {
        for (i=0; i<=2-1; i++)
            x[i]=a[i]+(b[i]-a[i])*rnd1(&r);
        s=s+(*f)(n, x)/d;
    }
    for (i=0; i<=n-1; i++)
        s=s*(b[i]-a[i]);
    free(x);
    return(s);
}


main()
{
    static double a[3]={1.0, 1.0, 1.0};
    static double b[3]={2.0, 2.0, 2.0};
    double mtmlf(int, double []);
    printf("\n");
    printf("s=%e\n", mtml(3, a, b, mtmlf));
    printf("\n");
}

double mtmlf(int n, double x[])//mtmlf为被积函数,这里是定值1
{
    int i;
    double f;
    f=0.0;
    for(i=0; i<=n-1; i++)
      f=f+x[i]*x[i];
    return(f);
}
  • 大小: 2.8 KB
分享到:
评论

相关推荐

    bontednulerical.zip_formula_monte carlo method

    Using MATLAB software to master trapezoid formula, Simpson formula and monte carlo method to calculate numerical integration

    Monte Carlo & Statistics

    Project Description The app can calculate the VaR of assets based on analisys of covariance, historical simulation and ...monte carlo simulation based on historical data of the assets.

    Business Case Analysis with R

    Model the inherent uncertainties in the problem with Monte Carlo simulation using the R language Communicate the results graphically Draw appropriate insights from the results Develop creative ...

    Using_the_Genetic_Algorithm_to_calculate_the_maxim_genetic

    Using_the_Genetic_Algorithm_to_calculate_the_maxim_genetic-algorithm-in-action

    Calculate Band Structure Using VASP

    ### 使用VASP计算能带结构 #### 概述 本文主要介绍了如何利用VASP(Vienna Ab initio Simulation Package)软件来计算材料的能带结构,并以硅(Si)为例进行详细阐述。VASP是一种广泛使用的密度泛函理论(Density ...

    c语言实现求导(calculate derivative by using c programming language).pdf

    在给定文件中,作者介绍了如何使用C语言实现求导算法。这一过程涉及到了微积分领域中的基础概念,特别是微分学部分。文档提到了利用C语言编写的程序来近似计算函数在某一点的导数,即通过求取函数在这一点附近的差分...

    Calculate-the-day-of-the-year.zip_The Year

    本项目“Calculate-the-day-of-the-year.zip_The Year”提供了这样的学习资源,通过一个名为“Calculate the day of the year.txt”的文本文件,帮助我们理解如何实现这一功能。 首先,我们需要知道的是年份、月份...

    Impractical Python Projects (2018.11出版,EPUB格式)

    Plan your parents’ secure retirement with Monte Carlo simulation Save the sorceress Zatanna from a stabby death using palingrams Model the Milky Way and calculate our odds of detecting alien ...

    MATLAB code to calculate the 3D skeleton of a binary volume

    标题中的“MATLAB code to calculate the 3D skeleton of a binary volume”指的是使用MATLAB编程实现的计算三维二值体积的骨架算法。在图像处理和计算机视觉领域,骨架化是提取物体主干结构的一种方法,它能将物体...

    Calculate-the-normalized-maximum-value.py

    Calculate-the-normalized-maximum-value.py

    matlab函数微积分课程设计.doc.doc

    The program design and running debugging portion of the course involves implementing Matlab functions to calculate the definite integral of a one-variable function. This can be achieved using built-in...

    calculate.rar_calculate指令_calculate数学

    《calculate指令与calculate数学:深入理解数学公式计算程序》 在信息技术领域,高效处理和解析数学公式是不可或缺的一部分。本文将深入探讨名为“calculate”的计算程序,它具备处理加减乘除以及括号的能力,是...

    How to calculate the noise of Amplifier.pdf

    【如何计算运放噪声】 运算放大器(Op Amp)在信号处理系统中广泛使用,而其噪声性能对系统的整体性能至关重要。噪声可以来源于多个因素,包括输入电压噪声、电流噪声和电阻噪声等。理解并计算这些噪声对于优化设计...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: If the TCustomProp.Assign method calls when the undo/redo history writing was in progress, the error occured. - FIX: The TFlexPanel.LoadFlexControl method now virtual. - FIX: The TFlexPanel....

    随机算法以及素数生成实验报告附代码

    与Monte Carlo算法类似,Las Vegas算法也使用随机性,但不同的是,它在成功时总是能得出正确结果,只是成功所需的平均运行时间取决于随机性。在本实验中,Las Vegas算法被用来进一步确认由Monte Carlo算法得出的可能...

    java calculate the prime which is received from input

    一种很好求素数方法可能会有帮助对大家不信下载看看吧啊啊啊

    calculate.zip_calculate 在python_calculate在Python_calculator_pyth

    在这个名为 "calculate.zip" 的压缩包中,包含了一个名为 "calculate.py" 的源代码文件,这应该就是实现了一个基于 `tkinter` 的简单计算器应用。 在Python中,`tkinter` 提供了一系列的类和方法,用于构建窗口、...

    09067568.pdf

    Therefore, this improved bioinspired neural network algorithm has proven the ability to calculate the multi-robot 3D optimal paths. By introducing this multi-scale map method into the multirobot ...

Global site tag (gtag.js) - Google Analytics