forked from MaterSim/ComputationalPhysics300
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lec 12 hw take 2.py
98 lines (82 loc) · 1.86 KB
/
lec 12 hw take 2.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 7 23:50:01 2018
@author: towar
"""
# MC integral
# libraries
import matplotlib.pyplot as plt
from random import random
import numpy as np
from scipy.integrate import quad
# input and constants
N= input(prompt = "enter a number of iterations: ")
N = int(N)
integrals = input(prompt = "enter which integral you would like to evaluate (1 or 2): ")
integrals = int(integrals)
a, b = 0, 1
x = np.linspace(a,b,N)
# functions
def f(x):
if integrals == 1:
y = 1/np.sqrt(x)/(1 + np.exp(x))
if integrals == 2:
y = 1/x/(1 + np.exp(x))
return y
def w(x):
if integrals == 1:
y = 1/np.sqrt(x)
if integrals == 2:
y = 1/x
return y
def p(x):
if integrals == 1:
y = x*x
if integrals == 2:
y = x
return y
# improved monte carlo algo
def IMC(N):
constant = quad(w,a,b)[0]
I = 0
for i in range(N):
x = random()
y = p(x)
I += f(y)/w(y)
return I/N*constant
# OG monte carlo
def MC(N):
I = 0
for i in range(N):
x = random()
I += f(x)
return I/N
# General guesses of MC algos and comparison
res_IMC = []
res_MC = []
for i in range(N):
res_IMC.append(IMC(N))
res_MC.append(MC(N))
# plot of guesses
plt.figure(figsize = (15,15))
plt.plot(res_IMC, label='IMC')
plt.plot(res_MC, label='MC')
plt.legend()
plt.show()
# plot of of curves
plt.figure(figsize = (15,15))
plt.plot(x, f(x), label='f(x)')
plt.plot(x, w(x), label='w(x)')
plt.show()
# plot of weighting
res = []
for i in range(N):
x = random()
res.append(x*x)
plt.figure(figsize = (15,15))
plt.hist(res, bins=100)
plt.show()
# output of answers
print('improved: ', IMC(N))
print('original: ', MC(N))
print('from scipy: ', quad(f, a, b)[0])