dev1
dev1

Reputation: 49

How can I get the constant from an ODE in python?

My task is to calculate the constant from an ODE. So, I don't want to plot the function, I just want to get the result of the differential equation, and after that I need to calculate the c based on this assumption: v0=120 when t0=0. I have started to implement with help of sympy module, and I get successfully the following result:[120.000000000000 -2.23606797749979/tanh(C1)] But, after that I have no idea how I could get c1. Is it possible?

import inline as inline
import sympy as sp
import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt
from sympy import *
from sympy import lambdify
t=Symbol('t')
v = map(Function, 'v')
v=Function('v')
k=2
g=10
i=dsolve(Eq( Derivative(v(t), t)+k*v(t)**2, g), v(t))
j=i.subs(v(t),120).subs(t,0).evalf()
print(i)
print(j)
h=j.args
k=np.array(h)

Upvotes: 1

Views: 365

Answers (1)

smichr
smichr

Reputation: 19093

If you want the value of C1 corresponding to the values of a given t and v(t) you could first solve for it and then substitute in the known values:

>>> C1 = [_ for _ in i.free_symbols if _.name == 'C1'][0]
>>> vals = solve(i, C1)
>>> [_.subs(v(t),120).subs(t,0).n(2) for _ in vals]
[-0.019 + 3.1*I, -0.019]

Upvotes: 2

Related Questions