Accounting for a proportionality constant to use dsolve in Python

95 Views Asked by At

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

There are 1 best solutions below

0
On

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))