简体   繁体   中英

Differentiation in python using Sympy

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM