From 00db3aea6fce5afa13245ed9194cff7c28d7fd84 Mon Sep 17 00:00:00 2001 From: Ceres Date: Sun, 12 Oct 2025 15:49:48 +0100 Subject: [PATCH] Update program and add gitignore --- .gitignore | 1 + Exercise 1/exercise1.py | 110 +++++++++++++++++++++------------------- 2 files changed, 60 insertions(+), 51 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6f9a44 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/settings.json diff --git a/Exercise 1/exercise1.py b/Exercise 1/exercise1.py index 7b2e894..be3f3ba 100644 --- a/Exercise 1/exercise1.py +++ b/Exercise 1/exercise1.py @@ -1,68 +1,76 @@ +from math import * import numpy as np from scipy.integrate import solve_ivp import matplotlib.pyplot as plt +def part1(radFactor, orbits): + #Part one Diffeq + def f_part1(t, state, Me, Mm, G): + xm, ym, vx, vy = state -#Part one Diffeq -def f_part1(t, state, Me, Mm, G): - xm, ym, vx, vy = state + dvxdt = -(Me*G*xm)/((xm**2+ym**2)**(3/2)) + dxmdt = vx + dvydt = -(Me*G*ym)/((xm**2+ym**2)**(3/2)) + dymdt = vy - dvxdt = -(Me*G*xm)/((xm**2+ym**2)**(3/2)) - dxmdt = vx - dvydt = -(Me*G*ym)/((xm**2+ym**2)**(3/2)) - dymdt = vy - - return (dxmdt, dymdt, dvxdt, dvydt) + return (dxmdt, dymdt, dvxdt, dvydt) -# Initial Conditions -t_min = 0 -t_max = 86400*20 -numpoints = 200 -t = np.linspace(t_min, t_max, numpoints) + # Initial Conditions + Me = 5.97*(10**24) + Mm = 7.35*(10**22) + G = 6.67*(10**-11) -xm0 = 384400000 -ym0 = 0 -vx0 = 0 -vy0 = 1017.79 + t_min = 0 + t_max = 2360620*int(orbits) + numpoints = 2000*int(orbits) + t = np.linspace(t_min, t_max, numpoints) -Me = 5.97*(10**24) -Mm = 7.35*(10**22) -G = 6.67*(10**-11) + r = 384400000 + v = sqrt((G*Me)/r)*float(radFactor) + + xm0 = r + ym0 = 0 + vx0 = 0 + vy0 = v + + rtol = 1e-5 + atol = 1e-8 -#Solver -results = solve_ivp(f_part1, (t_min,t_max), (xm0, ym0, vx0, vy0), args=(Me, Mm, G), t_eval=t) + #Solver + results = solve_ivp(f_part1, (t_min,t_max), (xm0, ym0, vx0, vy0), args=(Me, Mm, G), t_eval=t, atol=atol, rtol=rtol) -#Graph plotting -ax=plt.axes() # This creates some axes, so that we -ax.set_aspect(1) # can set the aspect ratio to 1 i.e. - # x and y axes are scaled equally. -ax.set_xlabel("x coordinate (m)") # Must label axes (with -ax.set_ylabel("y coordinate (m)") # units) and give -ax.set_title("Orbit of moon around earth") # plot title. -# -ax.plot(results.y[0],results.y[1]) # Make the plot -ax.legend(['Moon']) # and add a key. -plt.show() + #Graph plotting + ax=plt.axes() # This creates some axes, so that we + ax.set_aspect(1) # can set the aspect ratio to 1 i.e. + # x and y axes are scaled equally. + ax.set_xlabel("x coordinate (m)") # Must label axes (with + ax.set_ylabel("y coordinate (m)") # units) and give + ax.set_title("Orbit of moon around earth") # plot title. + # + ax.plot(results.y[0],results.y[1]) # Make the plot + ax.plot(0,0) + ax.legend(['Moon']) # and add a key. + plt.show() -# MyInput = '0' -# while MyInput != 'q': -# MyInput = input('Enter a choice, "1", "2" or "q" to quit: ') -# print('You entered the choice: ',MyInput) -# if MyInput == '1': -# print('You have chosen part (1): simulation of a lunar orbit') -# # -# # put your code for part (1) here -# # -# elif MyInput == '2': -# print('You have chosen part (2): earth-moon-probe system') -# # -# # put your code for part (2) here -# # -# elif MyInput != 'q': -# print('This is not a valid choice') -# print('You have chosen to finish - goodbye.') \ No newline at end of file +MyInput = '0' +while MyInput != 'q': + MyInput = input('Enter a choice, "1", "2" or "q" to quit: ') + print('You entered the choice: ',MyInput) + if MyInput == '1': + print('You have chosen part (1): simulation of a lunar orbit') + radFactor = input(f'Enter the velocity scalar. A value of 1 will result in a circular orbit, and anything else will give an elliptical orbit: ') + orbits = input(f'Enter the approximate number of orbits desired: ') + part1(radFactor, orbits) + elif MyInput == '2': + print('You have chosen part (2): earth-moon-probe system') + # + # put your code for part (2) here + # + elif MyInput != 'q': + print('This is not a valid choice') +print('You have chosen to finish - goodbye.') \ No newline at end of file