In [1]:
from PyGravity import *
In [3]:
# make new particle at position 1,1 with no velocity and mass 10
A = Particle('name',Vector(['1','1']), Vector(['0','0']), 10)
print A
print '\nmoving A\n'
A.move(1)
print A
In [4]:
#Make a particle with non-zero velocity and move it
B = Particle('B',Vector(['1','1']), Vector(['1','2']), 10)
print B
print '\nmoving b for 1 second\n'
B.move(1)
print B
print '\n moving B for 5 seconds\n'
B.move(5)
print B
In [6]:
#Particle that starts stationary, but is then accelerated
C = Particle('C',Vector(['1','1']), Vector(['0','0']), 10)
print C
print '\naccelerating by 1,1 for 1 second\n'
C.accelerate(Vector(['1','1']), 1)
print C
print '\nmoving a couple times\n'
C.move(3)
print C
print '\nnow comming back to a stop\n'
C.accelerate(Vector(['-1','-1']), 1)
print C
print '\n trying to move again\n'
for i in range(3):
C.move(1)
print C
In [8]:
#accelerate from rest for 5 seconds
E = Particle('E', Vector(['0','0']), Vector(['0','0']), Vector(['4']))
print E
print '\n accelerating \n'
for i in range(5):
E.accelerate(Vector(['1','2']),1)
E.move(1)
print 'time: ', i
print E, '\n'
print '\n coming back to a stop\n'
for i in range(5):
E.accelerate(Vector(['-1','-2']),1)
E.move(1)
print 'time: ', i
print E, '\n'
In [9]:
#plotting the above example
import numpy as np
import matplotlib.pyplot as plt
import mpld3
mpld3.enable_notebook()
#4 sets, 2 blue for accelerating, 2 red for decelerating
x = []
y = []
x2 = []
y2 = []
#Space ship
E = Particle(Vector(['1','1']), Vector(['0','0']), 10)
#forward engine
max_forward_engine = Vector(['1','2'])
#reverse engine half as powerful
max_reverse_engine = max_forward_engine*(-.5)
#speeding up
for i in range(10):
x.append(E.P[0])
y.append(E.P[1])
E.accelerate(max_forward_engine,1)
E.move(1)
#slowing down
for i in range(20):
x2.append(E.P[0])
y2.append(E.P[1])
E.accelerate(max_reverse_engine,1)
E.move(1)
plt.scatter(x, y, s=8, c='b', alpha=0.5)
plt.scatter(x2, y2, s=8, c='r', alpha=0.5)
plt.show()
In [2]:
%matplotlib inline
# plotting 3d
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Time')
#Space ship
E = Particle('space-ship',Vector(['1','1']), Vector(['0','0']), 10)
#forward engine
max_forward_engine = Vector(['1','2'])
#reverse engine half as powerful
max_reverse_engine = max_forward_engine*(-.5)
#speeding up
for i in range(10):
ax.scatter(float(E.P[0]), float(E.P[1]), i, c='r', marker='o')
E.accelerate(max_forward_engine,1)
E.move(1)
#slowing down
for i in range(10,30):
ax.scatter(float(E.P[0]), float(E.P[1]), i, c='b', marker='o')
E.accelerate(max_reverse_engine,1)
E.move(1)
plt.show()
In [ ]: