问题
I would like to have first several coefficients of formal power series defined implicitly by a differential equation.
Example.
import sympy as sp
sp.init_printing() # math as latex
from IPython.display import display
z = sp.Symbol('z')
F = sp.Function('F')(z)
F_ = sp.Derivative(F, z)
equation = sp.Eq(F**2 + 1 - F_, 0)
display(equation)
solution = sp.dsolve(equation)
display(solution)
sp.series(sp.tan(z), n = 8)
Question. How to compute formal power series solution of ODE without explicitly solving it in terms of elementary functions?
Sidenote. Some differential equations (even linear) have solutions in divergent power series only, for example an equation
L = z^2 + z^2 L + z^4 L'. It is interesting to know whethersympysupports such equations along with usual ones.
Related.
The current question is a sequel of a more easy question.
Sympy: how to solve algebraic equation in formal power series?
回答1:
UPDATE: an answer to a similar question has been posted here. I use this second answer instead of the one presented below. The solution using standard functions from sympy works very slowly.
This seems to be (partially) possible in sympy-1.1.1. Be sure to update to the corresponding version first. I refer to official documentation part on ordinary differential equations
http://docs.sympy.org/latest/modules/solvers/ode.html
We can use the method dsolve with additional hints asking to represent the solution as formal power series. This is only possible for certain types of equations. For the above equation, we ask for possible hint types.
>>> sp.classify_ode(equation)
('separable',
'1st_exact',
'1st_power_series',
'lie_group',
'separable_Integral',
'1st_exact_Integral')
Continuing the example in the question, we specify the hint '1st_power_series' and initial conditions F(0) = 0:
solution = sp.dsolve(equation, hint='1st_power_series', ics={F.subs(z,0):0})
display(solution)
Issue 1. If we want more terms, even say
n = 10, the function works for a very long time (normally, I expect 20 coefficients within several seconds). Combinatorially, one can write a very fast recurrence for linear ODE. I don't know whether it is implemented insympy.Issue 2. This technique doesn't seem to apply to Ricatti equations like the one mentioned in the original question
L = z^2 + z^2 L + z^4 L'.
If someone knows a better solution, (s)he is warmly welcome!
来源:https://stackoverflow.com/questions/46427586/how-to-solve-a-differential-equation-in-formal-power-series