# The generalized model

The equation of the generalized model for 1 species can be written as follows:

$
\dot{X} = X\left[\left(r+b X\right)-\left(a + c b X\right)X\right],
$




```python
from sympy.abc import x, y
from sympy.solvers import solve
from sympy import *
```


```python
# We define the symbols for the equations and functiones
a,b,r,c = symbols('a b r c')
f = Function('f')
```


```python
# We define the function to solve

eq = r+(b-a)*x - c*b*x**2
f= x*eq

```

We calculate the fixed points


```python
s1,s2,s3=solve(f,x)
print('fixed points =',(s1,s2,s3))
```

    fixed points = (0, (-a + b - sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r))/(2*b*c), (-a + b + sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r))/(2*b*c))


We calculate the Jacobian of the function


```python
dfdx = f.diff(x)
print("f'(x) =", simplify(dfdx))
```

    f'(x) = -2*a*x - 3*b*c*x**2 + 2*b*x + r


We evaluate the jacobian in the fixed points


```python
L1=dfdx.subs(x,s1)
L1
```




    r




```python
L2=dfdx.subs(x,s2)
L2
```




    r + (-a + b)*(-a + b - sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r))/(2*b*c) - (-a + b - sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r))**2/(4*b*c) + (-a + b - sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r))*sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r)/(2*b*c)




```python
L3=simplify(dfdx.subs(x,s3))
L3
```




    -(a**2 - 2*a*b - a*sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r) + b**2 + 4*b*c*r + b*sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r))/(2*b*c)




```python
# We can evaluate the eigenvalues with the numerical values of $r,c,b,a$
L3.subs(r,0.1).subs(b,-0.01).subs(a,0.001).subs(c,0.1)
```




    -0.1395 - 0.0918681119866954*I




```python
# Another way to calculate the Jacobian Matrix and the eigenvalues.
equilibria = solve(f,x)
eqMat = Matrix([f])
Mat = Matrix([x])
jacMat =eqMat.jacobian(Mat)
print('Jacobian %s' % jacMat)
print('---------------------')

# iterate through list of equilibria
for item in equilibria:
    eqmat = jacMat.subs([ (x, item)])
    print('The eigenvalues for the fixed point (%s) is %s:' 
          %(item, eqmat.eigenvals()))
    
    print('-------------------------------------------')

```

    Jacobian Matrix([[-b*c*x**2 + r + x*(-a + b) + x*(-a - 2*b*c*x + b)]])
    ---------------------
    The eigenvalues for the fixed point (0) is {r: 1}:
    -------------------------------------------
    The eigenvalues for the fixed point ((-a + b - sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r))/(2*b*c)) is {(-a**2 + 2*a*b - a*sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r) - b**2 - 4*b*c*r + b*sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r))/(2*b*c): 1}:
    -------------------------------------------
    The eigenvalues for the fixed point ((-a + b + sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r))/(2*b*c)) is {(-a**2 + 2*a*b + a*sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r) - b**2 - 4*b*c*r - b*sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r))/(2*b*c): 1}:
    -------------------------------------------



```python
eqmat = jacMat.subs([ (x, s2)])
eqmat.refine(Q.positive(b)).refine(Q.negative(r))
```




    Matrix([[r + (-a + b)*(-a + b - sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r))/(2*b*c) - (-a + b - sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r))**2/(4*b*c) + (-a + b - sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r))*sqrt(a**2 - 2*a*b + b**2 + 4*b*c*r)/(2*b*c)]])




```python

```

9f1da457b88f8ff87f17d02b722c7bed5b9af526
ipynb
Jupyter Notebook
Chapter2_1species/Generalized_eq_1_species_Sympy.ipynb
JJ-Lab/Lucianos_Thesis
7ed50b0d5d12903066f8caec7df8cdf38490a060
MIT
Chapter2_1species/Generalized_eq_1_species_Sympy.ipynb
JJ-Lab/Lucianos_Thesis
7ed50b0d5d12903066f8caec7df8cdf38490a060
MIT
Chapter2_1species/Generalized_eq_1_species_Sympy.ipynb
JJ-Lab/Lucianos_Thesis
7ed50b0d5d12903066f8caec7df8cdf38490a060
MIT
Qwen/Qwen-72B
1. YES
2. YES