Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sympy fails to solve a linear system of first order ODEs #8567

Closed
maeshtro opened this issue Dec 5, 2014 · 2 comments · Fixed by #19341
Closed

sympy fails to solve a linear system of first order ODEs #8567

maeshtro opened this issue Dec 5, 2014 · 2 comments · Fixed by #19341

Comments

@maeshtro
Copy link

maeshtro commented Dec 5, 2014

I tried to solve the following initial value problem

y1'   =    y1 + 2 y2,
y2'   = -2 y1 +   y2 + 2 exp(t),
y1(0) =  y2(0) = 1

with sympy using the code

import sympy as sy
t=sy.symbols('t')
y1=sy.Function('y1')   
y2=sy.Function('y2')
eqs=(sy.Eq(y1(t).diff(t),y1(t)+2*y2(t)), sy.Eq(y2(t).diff(t),-2*y1(t)+y2(t)+2*sy.exp(t))) 
s=sy.dsolve(eqs)        # General solution
y1g=s[0].args[1]
y2g=s[1].args[1]
# Find C1 and C2 so that the initial condition is satisfied
sol=sy.solve([y1g.subs(t,0)-1,y2g.subs(t,0)-1])
y1=y1g.subs(sol)
y2=y2g.subs(sol)
print([y1,y2])

The solution I get is

[2*(sin(2*t)/2 + cos(2*t)/2)*exp(t), (-sin(2*t) + cos(2*t))*exp(t)]

which is wrong. The correct solution is

[exp(t)*(1+sin(2*t)), exp(t)*cos(2*t)]

I am running python 3.3.2 with sympy 0.7.3 under fedora 19 x64.

@asmeurer
Copy link
Member

asmeurer commented Dec 7, 2014

Your version of SymPy is ancient. Please upgrade to 0.7.6.

Even so, I get the same answer in 0.7.6.

@oscarbenjamin
Copy link
Contributor

On current master I get NotImplementedError for this system:

NotImplementedError                       Traceback (most recent call last)
~/current/sympy/sympy/tmp/dsolvesys.py in <module>
      4 y2=sy.Function('y2')
      5 eqs=(sy.Eq(y1(t).diff(t),y1(t)+2*y2(t)), sy.Eq(y2(t).diff(t),-2*y1(t)+y2(t)+2*sy.exp(t)))
----> 6 s=sy.dsolve(eqs)        # General solution
      7 y1g=s[0].args[1]
      8 y2g=s[1].args[1]

~/current/sympy/sympy/sympy/solvers/ode.py in dsolve(eq, func, hint, simplify, ics, xi, eta, x0, n, **kwargs)
    618             "number of functions being equal to number of equations")
    619         if match['type_of_equation'] is None:
--> 620             raise NotImplementedError

This is what the equations look like:

In [4]: eqs                                                                                                                                    
Out[4]: 
⎛d                            d                                 t⎞
⎜──(y₁(t)) = y₁(t) + 2⋅y₂(t), ──(y₂(t)) = -2⋅y₁(t) + y₂(t) + 2⋅ℯ ⎟
⎝dt                           dt                                 ⎠

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants