From b304ed88426a78f52af260f2387346ffeb7681d5 Mon Sep 17 00:00:00 2001 From: AyarKulakovski <116837044+AyarKulakovski@users.noreply.github.com> Date: Fri, 16 Dec 2022 15:36:00 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9A=D1=83=D0=BB=D0=B0=D0=BA=D0=BE=D0=B2?= =?UTF-8?q?=D1=81=D0=BA=D0=B8=D0=B9=20=D0=90=D1=8F=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Poisson.py | 55 ++++++++++++++++++++++++++++++++++++++++++++ Poisson_decimal.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 Poisson.py create mode 100644 Poisson_decimal.py diff --git a/Poisson.py b/Poisson.py new file mode 100644 index 0000000..79f1148 --- /dev/null +++ b/Poisson.py @@ -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) \ No newline at end of file diff --git a/Poisson_decimal.py b/Poisson_decimal.py new file mode 100644 index 0000000..756ad85 --- /dev/null +++ b/Poisson_decimal.py @@ -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')) \ No newline at end of file From 44e03eecf26cb11b9910137451d9b46d78a4872d Mon Sep 17 00:00:00 2001 From: AyarKulakovski <116837044+AyarKulakovski@users.noreply.github.com> Date: Thu, 22 Dec 2022 20:16:09 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9A=D1=83=D0=BB=D0=B0=D0=BA=D0=BE=D0=B2?= =?UTF-8?q?=D1=81=D0=BA=D0=B8=D0=B9=20=D0=90=D1=8F=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Solar_system.py | 103 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 Solar_system.py diff --git a/Solar_system.py b/Solar_system.py new file mode 100644 index 0000000..e5d5be0 --- /dev/null +++ b/Solar_system.py @@ -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() + +