update integration method to be inside the particle class #7

Open
julioro wants to merge 1 commit from julioro/2025-plovdiv-scientific-patterns:integrate_pos into main
Showing only changes of commit 19dcbeea5c - Show all commits

View file

@ -35,7 +35,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {
"pycharm": {
"name": "#%%\n"
@ -52,20 +52,24 @@
],
"source": [
"class Particle:\n",
" def __init__(self, mass, velocity):\n",
" def __init__(self, mass, velocity, position=0):\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",
" def update_position(self, dt):\n",
" self.position += self.velocity * dt\n",
"\n",
"particle = Particle(mass=2.1, velocity=0.8)\n",
"print(particle.momentum())"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
@ -77,8 +81,10 @@
}
],
"source": [
"position = 8.2\n",
"new_position = update_position(particle.velocity, position, dt=0.1)\n",
"\n",
"particle = Particle(mass=2.1, velocity=0.8, position = 8.2)\n",
"particle.update_position(dt=0.1)\n",
"new_position = particle.position\n",
"print(new_position)"
]
},
@ -104,7 +110,7 @@
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "env",
"language": "python",
"name": "python3"
},
@ -118,7 +124,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
"version": "3.13.6"
},
"toc": {
"nav_menu": {