add update_position as a class method #9

Open
mihaelami wants to merge 2 commits from mihaelami/2025-plovdiv-scientific-patterns:exercise1 into main

View file

@ -35,7 +35,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 5,
"metadata": {
"pycharm": {
"name": "#%%\n"
@ -52,20 +52,24 @@
],
"source": [
"class Particle:\n",
" def __init__(self, mass, velocity):\n",
" def __init__(self, mass, velocity, position):\n",
" self.mass = mass\n",
" self.velocity = velocity\n",
" self.position = position\n",
"\n",
" def momentum(self):\n",
" return self.mass * self.velocity\n",
"\n",
"particle = Particle(mass=2.1, velocity=0.8)\n",
" def update_position(self, dt):\n",
" return self.position + self.velocity * dt\n",
"\n",
"particle = Particle(mass=2.1, velocity=0.8, position = 8.2)\n",
"print(particle.momentum())"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@ -77,8 +81,8 @@
}
],
"source": [
"position = 8.2\n",
"new_position = update_position(particle.velocity, position, dt=0.1)\n",
"\n",
"new_position = particle.update_position(position, dt=0.1)\n",
"print(new_position)"
]
},
@ -118,7 +122,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
"version": "3.13.6"
},
"toc": {
"nav_menu": {
@ -142,5 +146,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}