-1

I have the differential equation: dP/dt = kx(t) where k is a proportionality constant. I am trying to use dsolve to find a general solution, but I don't know how to account for that k in the code. Below is my code, which technically works, but doesn't account for k. If you can let me know how to edit it, I would appreciate it greatly.

import sympy as sp
t = sp.symbols('t')
x = sp.Function('x')

deq = sp.Eq(sp.diff(x(t),t), x(t))
xsoln = sp.dsolve(deq, x(t))
sp.pprint(xsoln)
  • 1
    See https://stackoverflow.com/questions/65451278/sympy-dsolve-couldnt-solve-for-initial-conditions for a question on this equation passing straight over your hurdle. – Lutz Lehmann Sep 17 '21 at 06:24
  • Or https://stackoverflow.com/questions/44099570/using-sympy-dsolve-for-simple-harmonic-motion for a similarly simple example with a constant parameter. – Lutz Lehmann Sep 17 '21 at 06:35

1 Answers1

0

Just make k another symbol. SymPy assumes all symbols are independent to one another. In other words, k is automatically treated as a constant with respect to t.

>>> t, k = symbols('t, k')
>>> x = Function('x')
>>> deq = Eq(diff(x(t), t), k*x(t))
>>> dsolve(deq, x(t))
Eq(x(t), C1*exp(k*t))
asmeurer
  • 86,894
  • 26
  • 169
  • 240