Differentiation in python using Sympy

490 views Asked by At

How do i implement this kind of equation in python dC/dt = r + kI - dC where the left hand side are constants and the right hand side are varibles?

i am relatively new to python and as such can't really do much. from sympy.solvers import ode

r=float(input("enter r:"))

k=float(input("enter k:"))

I=float(input("enter I:"))

d=float(input("enter d:"))

C=float(input("enter C:"))

dC/dt=x

x=r + kI-dC

print(x)

what it just does equate the values of x and not any differential, would like help getting this to work.

if possible i would like to get answer specifying the using of sympy, but all answers are truly appreciated.

1

There are 1 answers

7
smichr On BEST ANSWER

You asigned values to all the variables that are on the rhs of x so when you show x you see the value that it took on with the variables that you defined. Rather than input values, why not try solve the ode symbolically if possible?

>>> from sympy import *
>>> var('r k I d C t')
(r, k, I, d, C, t)
>>> eq = Eq(C(t).diff(t), r + k*I + d*C(t))  # note d*C(t) not d*C
>>> ans = dsolve(eq); ans
C(t) == (-I*k - r + exp(d*(C1 + t)))/d

Now you can substitute in values for the variables to see the result:

>>> ans.subs({k: 0})
C(t) == (-r + exp(d*(C1 + t)))/d