-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex7.3.py
More file actions
36 lines (29 loc) · 844 Bytes
/
ex7.3.py
File metadata and controls
36 lines (29 loc) · 844 Bytes
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 22 17:11:58 2020
@author: astro
"""
import math
import random
import matplotlib.pyplot as plt
#%% Integral
N = [1e3, 5*1e3, 1e4, 5*1e4, 1e5, 5*1e5, 1e6, 5*1e6]
I = []
Ip = 0.
for v in range(int(len(N))):
for i in range(int(N[v])):
x = random.random()*2.#draws a random uniform number between the limits
y = float((math.sin(1./(x*(2.-x))))**2.) #equation for the given function
Ip+=y #repeats it for N times and sums up all results
I.append((2./N[v])*Ip)# I = Ip*(b - a)/N
Ip = 0.
print(I)
M=((I[0]+I[1]+I[2]+I[3]+I[4]+I[5]+I[6]+I[7])/8)
print('Averange integral value: ', M)
#%% Plot
plt.plot(N, I)
plt.xlabel('N', fontsize=20)
plt.ylabel('I', fontsize=20)
plt.xscale('log')
plt.show()