Skip to content
Advertisement

Sympy gives a residual value when trying to solve the logistics equation

If I try solving the logistics differential equation in Sympy I get a residual value (10^(-13)) which prevents sympy from getting the correct values for the initial coditions. If I run this code:

import numpy as np

import sympy as sp

M = 10000
a = 0.03
x = sp.symbols("x")
# x, a, M = sp.symbols("x a M")
f = sp.Function('f')
fl = sp.Derivative(f(x),x)
sol = sp.dsolve(fl - a*(1 - f(x)/M)*f(x), f(x));sol

I get:

Eq(f(x), (9.09494701772928e-13*exp(0.03*C1 - 0.03*x) - 10000.0)/(exp(0.03*C1 - 0.03*x) - 1))

How can one get rid of these residuals in the solution?

Advertisement

Answer

Either don’t use Float (use a = Rational(3, 100)) or if you know you want those 1e-13 magnitude numbers to be 0 then you can replace them with 0:

>>> eq
Eq(f(x), (9.09494701772928e-13*exp(0.03*C1 - 0.03*x) - 10000.0
... )/(exp(0.03*C1 - 0.03*x) - 1))
>>> eq.replace(lambda x: x.is_Float and abs(x) < 1e-12, lambda x: 0)
Eq(f(x), -10000.0/(exp(0.03*C1 - 0.03*x) - 1))
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement