```python
import sympy as sm
import numpy as np
import matplotlib.pyplot as plt
from pyodesys.symbolic import SymbolicSys
sm.init_printing()
%matplotlib inline
```


```python
def mk_odesys(power=1):
    def rhs(t, y, p):
        k = t**power
        return [-k*y[0], k*y[0]]
    
    def analytic(tout, init_y):
        y0ref = init_y[0]*np.exp(-tout**(power+1)/(power+1))
        return np.array([y0ref, init_y[0] - y0ref + init_y[1]]).T
    
    odesys = SymbolicSys.from_callback(rhs, 2, 0, linear_invariants=[[1, 1]])
    return odesys, analytic
```


```python
odesys1, analytic1 = mk_odesys(1)
odesys1.dep, odesys1.exprs, odesys1.get_jac()
```


```python
def integrate_and_plot(odesys, analytic, axes=None, tout=3, init_y=(1,0)):
    result = odesys.integrate(tout, init_y, integrator='cvode')
    if axes is None:
        _fig, axes = plt.subplots(1, 3, figsize=(14, 4))
    result.plot(ax=axes[0])
    result.plot(ax=axes[1], y=result.yout - analytic(result.xout, result.yout[0, :]))
    result.plot_invariant_violations(ax=axes[2])
    print({k: v for k, v in result.info.items() if not k.startswith('internal')})
```


```python
integrate_and_plot(odesys1, analytic1)
```


```python
asys1 = odesys1.as_autonomous()
asys1.dep, asys1.exprs, asys1.get_jac()
```


```python
integrate_and_plot(asys1, analytic1)
```


```python
def compare(power, **kwargs):
    odesys, analytic = mk_odesys(power)
    autsys = odesys.as_autonomous()
    fig, axes = plt.subplots(2, 3, figsize=(14, 8))
    integrate_and_plot(odesys, analytic, axes=axes[0], **kwargs)
    integrate_and_plot(autsys, analytic, axes=axes[1], **kwargs)
```


```python
compare(2)
```


```python
compare(3, init_y=(3,1))
```


```python
compare(10, tout=1.5)
```

68f13b38c04a9132e6c9c3ed21247522d01b1c77
ipynb
Jupyter Notebook
examples/_linear_invariants_as_autonomous.ipynb
slayoo/pyodesys
8e1afb195dadf6c6f8e765873bc9dd0fae067c39
BSD-2-Clause
2015-09-29T16:51:03.000Z
2022-02-02T13:26:50.000Z
examples/_linear_invariants_as_autonomous.ipynb
slayoo/pyodesys
8e1afb195dadf6c6f8e765873bc9dd0fae067c39
BSD-2-Clause
2015-09-29T14:40:45.000Z
2021-09-18T19:29:50.000Z
examples/_linear_invariants_as_autonomous.ipynb
slayoo/pyodesys
8e1afb195dadf6c6f8e765873bc9dd0fae067c39
BSD-2-Clause
2016-03-18T14:00:39.000Z
2021-09-17T13:54:29.000Z
Qwen/Qwen-72B
1. YES
2. YES