Search code examples
sympysymbolic-mathodedsolve

sympy dsolve returns incorrect answer


I'm using sympy.dsolve to solve a simple ODE for a decay chain. The answer I get for different decay rates (e.g. lambda_1 > lambda_2) is wrong. After substituting C1=0, I get a simple exponential

-N_0*lambda_1*exp(-lambda_1*t)/(lambda_1 - lambda_2)

instead of the correct answer which has:

(exp(-lambda_1*t)-exp(-lambda_2*t)).

What am I doing wrong? Here is my code

sp.var('lambda_1,lambda_2 t')
sp.var('N_0')
daughter = sp.Function('N2',Positive=True)(t)

stage1 = N_0*sp.exp(-lambda_1*t)
eq = sp.Eq(daughter.diff(t),stage1*lambda_1 - daughter*lambda_2)

sp.dsolve(eq,daughter)

Solution

  • Your differential equation is (using shorter variable identifiers)

    y' = A*N0*exp(-A*t) - B*y
    

    Apply the integrating factor exp(B*t) to get the equivalent

    (exp(B*t)*y(t))' = A*N0*exp((B-A)*t)
    

    Integrate to get

    exp(B*t)*y(t) = A*N0*exp((B-A)*t)/(B-A) + C
    
    y(t) = A*N0*exp(-A*t)/(B-A) + C*exp(-B*t)
    

    which is exactly what the solver computed.