• 蒙特卡洛算法


    简介

    随机取样法(别名),基于大量事件的统计结果来实现一些确定性问题的计算。
    其实很简单哦

    QU

    (y=x^2) (y=12-x) (y = 0) 围成的区域面积
    面积如下图所示

    感觉matlab对于这种程序真的很方面,如果用C++看来没有50+行是写不完了

    code

    clc,clear
    x = unifrnd(0,12,[1,10000000]); % doc unifrnd
    y = unifrnd(0,9,[1,10000000]);
    pinshu = sum(y < x.^2 & x <=3 ) + sum (y<12-x & x>=3);
    area_appr = 12 * 9 * pinshu / 10^7
    
    hold on;
    lx = [0:0.01:12];
    ly1 = lx.^2; %注意这里必须使用点乘。
    plot(lx, ly1);
    ly2 = 12 - lx;
    ly3 = 0;
    plot(lx,ly2);
    plot(lx,ly3);
    

    TIPS

    unifrnd 函数是什么意思呢?
    returns an array R of random numbers generated from the continuous uniform distributions with lower and upper endpoints specified by A and B, respectively. If A and B are arrays, R(i,j) is generated from the distribution specified by the corresponding elements of A and B. If either A or B is a scalar, it is expanded to the size of the other input.
    生成一个数组R连续均匀分布于最低值和最高值。
    R = unifrnd(A,B,m,n,...) or R = unifrnd(A,B,[m,n,...]) returns an m-by-n-by-... array. If A and B are scalars, all elements of R are generated from the same distribution. If either A or B is an array, they must be m-by-n-by-... .
    总而言之 生成了 (10^7) 个数据点,x和y
    sum 统计这些数据点 的分布个数。
    感觉这个算法简单又实在。过于优秀。
    美的总是简单又好用的。

    Result

    area_appr =

    49.5029

    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    Java批量文件打包下载
    Java Swing
    空白文章
    linux 用户创建、管理、权限分配
    在虚拟机下安装hadoop集成环境(centos7+hadoop-2.6.4+jdk-7u79)
    《转载》POI导出excel日期格式
    java导出生成word(类似简历导出)
    《sqoop实现hdfs中的数据导出至mysql数据库》
    c# winform 自动关闭messagebox 模拟回车
    Ubuntu下启动/重启/停止apache服务器
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13440276.html
Copyright © 2020-2023  润新知