这次与上一次两次作业

2022 11 14日作业:

上一次作业(蒙特卡洛算法)

蒙特卡洛算法简介:

本质上来说蒙特卡洛方法是一类基于概率方法的统称,它并非一个特定的算法或者针对某一问题(如:概率)的解决方案,而是一种解决问题的思想,相关的方法有:蒙特卡罗算法,蒙特卡罗模拟,蒙特卡罗过程,蒙特卡罗搜索树---AlphaGO以及蒙特卡罗积分法。 蒙特卡洛积分是图形学里常用的数值方法。无论BRDF方程,还是PCF阴影滤波都会用到。所以图形学里这是一个需要搞明白的概念。
蒙特卡罗方法的基本思想是:随机抽样无限逼近。举个例子:我们要预测特朗普和拜登的支持率,可以通过随机抽样的方式统计计算,随着采样数的增加,结果越来越准确(实际上大于一定程度的抽象后就已经足够准确了(采样数取决于你对准确度的需求))。这就是所有蒙特卡罗方法的核心出发点。

利用蒙特卡洛算法求解的python过程

import math
import numpy as np

import random

upper_bound=15

lower_bound=10

m=100000
n=0
def func(x):
   return np.log(1+x)/(1+x**2)

for i in range(m):
    x=random.random()
    y=random.random()


def f_x_(x):

    outcome=3*(x**2)+4*math.cos(x)-4*x*math.sin(x)

    return outcome


def F_x_(x):

    outcome=x**3+4*x*math.cos(x)

    return outcome


DefiniteIntegral_By_MonteCarloMethod=0


sum=0

count=1

while count<=10000:

sum=sum+f_x_(random.uniform(lower_bound,upper_bound))

count=count+1

DefiniteIntegral_By_MonteCarloMethod=(upper_bound-lower_bound)*(sum/10000)

print("用蒙特卡洛方法计算的定积分:")

print(DefiniteIntegral_By_MonteCarloMethod)

print("")


DefiniteIntegral_By_PrimitiveFunction=F_x_(upper_bound)-F_x_(lower_bound)

print("直接用原函数计算的定积分:")

print(DefiniteIntegral_By_PrimitiveFunction)

print("")


math_deviation=abs(DefiniteIntegral_By_MonteCarloMethod-DefiniteIntegral_By_PrimitiveFunction)/DefiniteIntegral_By_PrimitiveFunction

print("偏差程度为:")


print('percent: {:.4%}'.format(math_deviation))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值