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

View file

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