Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions Poisson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import numpy as np
import scipy.special
import matplotlib.pyplot as plt

def poisson(aver, N):
n = np.arange(0, N)
p = (np.power(aver, n)*np.exp(-aver))/scipy.special.factorial(n)
if aver >= 0:
return p
else:
return 0

def poisson_1(aver, N):
p = (np.power(aver, N) * np.exp(-aver)) / scipy.special.factorial(N)
if aver >= 0:
return p
else:
return 0

def initial_moment(n, k):
N = np.size(n)
arr = np.arange(N)
x = np.power(arr, k)
y = n
m = np.dot(y, x)
if isinstance(k, int) == False or not(np.size(np.array(n)) > 1):
return 0
else:
return m

def average(n):
m = initial_moment(n, 1)
return m

def dispersion(n):
m = initial_moment(n, 2) - initial_moment(n, 1) ** 2
return m

def test(aver, N):
a = poisson(aver, N)
b = average(a)
c = dispersion(a)
plt.plot(a)
plt.scatter(b, poisson_1(b, b))
plt.scatter(c, poisson_1(c, c))
plt.show()

#test
a = poisson(3, 100)
b = average(a)
c = dispersion(a)
print('a = ', 3)
print('b = ', b)
print('c = ', c)
test(3, 100)
57 changes: 57 additions & 0 deletions Poisson_decimal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import numpy as np
import matplotlib.pyplot as plt
from decimal import Decimal


def exp(x):
return x.exp()

@np.vectorize
def fact(n):
f = Decimal('1')
while n > Decimal('1'):
f = f * n
n = n - Decimal('1')
return f

def poisson(aver, N):
n = np.arange(N+1)
p = ((aver**n)*exp(-aver))/fact(n)
if aver >= 0:
return p
else:
return 0

def initial_moment(n, k):
arr = np.arange(np.size(n))
m = n*arr*k
if isinstance(k, int) == False or not(np.size(np.array(n)) > 1):
return 0
else:
return m.sum()

def average(n):
m = initial_moment(n, 1)
return m

def dispersion(n):
m = initial_moment(n, 2) - initial_moment(n, 1)**2
return m

def test(aver, N):
a = poisson(aver, N)
b = average(a)
c = dispersion(a)
plt.plot(a)
b1 = poisson(b, b)
plt.scatter(b, b1[np.size(b1)-1])
plt.show()

#test
a = poisson(Decimal('3'), Decimal('100'))
b = average(a)
c = dispersion(a)
print('a = ', Decimal('3'))
print('b = ', b)
print('c = ', c)
test(Decimal('3'), Decimal('100'))
103 changes: 103 additions & 0 deletions Solar_system.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation


class Star:
def __init__(self, mass: float):
self.mass = mass
self.vec_P = np.array([[0.0, 0.0],])


class CosmicBody:
G = 6.67430e-11
sec_in_day = 86400

def __init__(self, mass: float, vec_v: np.ndarray, vec_P: np.ndarray):
self.mass = mass
self.vec_v = vec_v
self.vec_P = vec_P

def gravitate(self, bodys: list, delta_t: float):
v = self.vec_v[-1]
v_delt = np.array([[0.0, 0.0]])

for affect_bod in bodys:
r_vec = self.vec_P[-1] - affect_bod.vec_P[-1]
r = sum(r_vec**2)**0.5

v_delt += (-self.G * self.mass *
affect_bod.mass *
r_vec / r**3) * self.sec_in_day * delta_t / self.mass

r_new = self.vec_P[-1] + (v + v_delt) * self.sec_in_day * delta_t
self.vec_P = np.append(self.vec_P, r_new, axis=0)
self.vec_v += v_delt

def destroy(self, bodys):
for affect_bod in bodys:
dist = sum(((self.vec_P[-1] - affect_bod.vec_P[-1])**2))**0.5
if dist < 1.75e9:
return True, affect_bod

Sun = Star(2.0e30)

Earth = CosmicBody(5.972e24,
np.array([[0.0, 29765.0]]),
np.array([[1.5e11, 0.0]]))

Mercur = CosmicBody(3.33022e23,
np.array([[0.0, 47360.0]]),
np.array([[6.9e10, 0.0]]))

all_bodyes = [Sun, Earth, Mercur]

t0 = 0.0
delta_t = 1.0
t = 1000

while t0 < t and len(all_bodyes) > 1:
for i, body in enumerate(all_bodyes[1:], start=1):
affect_bodyes = all_bodyes[:i] + all_bodyes[i+1:]
body.gravitate(affect_bodyes, delta_t)
t0 += delta_t

fig, ax = plt.subplots(figsize=(8, 8))

earth_point = ax.plot(Earth.vec_P[0][0], Earth.vec_P[0][1],
marker='o',
markersize=6,
markeredgecolor="black",
markerfacecolor="blue")[0]

merk_point = ax.plot(Mercur.vec_P[0][0], Mercur.vec_P[0][1],
marker='o',
markersize=4,
markeredgecolor="black",
markerfacecolor="gray")[0]

sun_point = ax.plot(Sun.vec_P[0][0], Sun.vec_P[0][0],
marker='o',
markersize=10,
markeredgecolor="black",
markerfacecolor="yellow")[0]




def animate(i):
merk_point.set_data(Mercur.vec_P[i][0], Mercur.vec_P[i][1])
earth_point.set_data(Earth.vec_P[i][0], Earth.vec_P[i][1])
ax.set_xlim(-2*1.5e11,2*1.5e11)
ax.set_ylim(-2*1.5e11,2*1.5e11)
return earth_point, merk_point


solar_system_animation = animation.FuncAnimation(fig,
func=animate,
frames = t,
interval=1)

plt.show()