From ee390003371bee6174850434ff34f1b78b279cf1 Mon Sep 17 00:00:00 2001 From: Didictateur Date: Tue, 17 Feb 2026 19:46:17 +0100 Subject: [PATCH] can compute external forces --- examples/pendule_simple.py | 4 +++- legrandchien.py | 24 ++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/examples/pendule_simple.py b/examples/pendule_simple.py index a669249..65e0d16 100644 --- a/examples/pendule_simple.py +++ b/examples/pendule_simple.py @@ -10,6 +10,7 @@ import numpy as np g = lgc.Const(-10) m = lgc.Const(1) r = lgc.Const(1) +c = lgc.Const(0.3) theta = lgc.Var() @@ -20,13 +21,14 @@ T = 0.5 * m * (x.diff()**2 + y.diff()**2) V = m * g * r * lgc.cos(theta) L = T - V +F = 0.5 * c * (x.diff()**2 + y.diff()**2) init = { theta : [-3 * 3.14/4, 0], # theta, theta.diff() } dt = 0.01 -data = L.solve(init, 10, dt, True) +data = L.solve(init, 10, dt, True, F) # Extraire les positions times = sorted(data.keys()) diff --git a/legrandchien.py b/legrandchien.py index 9992a05..5d02f3a 100644 --- a/legrandchien.py +++ b/legrandchien.py @@ -33,6 +33,22 @@ class Const: def simplify(self): pass + def diff(self): + return Equation( + Operation.CONST, + None, + None, + Const(0) + ) + + def partial(self, _): + return Equation( + Operation.CONST, + None, + None, + Const(0) + ) + def __str__(self): return str(self.value) @@ -529,7 +545,7 @@ class Equation: self.compile() return self.compiled_function(dico) - def solve(self, init, tmax=10, dt=0.01, progress_bar=True): + def solve(self, init, tmax=10, dt=0.01, progress_bar=True, F_ext = Const(0)): dico = {} for v_name, v_init in init.items(): for i in range(len(v_init)): @@ -541,7 +557,11 @@ class Equation: equations = [] # print([v.name for v in variables]) for var in variables: - equations.append(self.partial(var.diff().name).diff() - self.partial(var.name)) + equations.append( + self.partial(var.diff().name).diff()\ + - self.partial(var.name)\ + + F_ext.partial(var.diff().name) + ) unknown = self.getUnknown(dico) for eq in equations: