3 KiB
3 KiB
Exercise: Add the function update_position
to the Particle
class¶
- Make the function
update_position
into a method of the classParticle
- Where do the position
position
of the particle belong? Modify the class constructor if necessary - Once it is done, create a particle with mass 2.1 with velocity 0.8 at position 8.2 . Update the position with dt=0.1 and print out the new location.
In [1]:
def update_position(velocity, position, dt):
return position + velocity * dt
In [2]:
class Particle:
def __init__(self, mass, velocity):
self.mass = mass
self.velocity = velocity
def momentum(self):
return self.mass * self.velocity
particle = Particle(mass=2.1, velocity=0.8)
print(particle.momentum())
In [3]:
position = 8.2
new_position = update_position(particle.velocity, position, dt=0.1)
print(new_position)
In [ ]:
In [ ]: