Quick Start¶
My first program¶
If you have successfully installed desolver following the installation guide here you will be able to test it with the following script.
1import desolver as de
2import numpy as np
3
4@de.rhs_prettifier(
5 equ_repr="[vx, -k*x/m]",
6 md_repr=r"""
7$$
8\frac{dx}{dt} = \begin{bmatrix}
9 0 & 1 \\
10 -\frac{k}{m} & 0
11 \end{bmatrix} \cdot \begin{bmatrix}x \\ v_x\end{bmatrix}
12$$
13"""
14)
15def rhs(t, state, k, m, **kwargs):
16 return np.array([[0.0, 1.0], [-k/m, 0.0]])@state
17
18y_init = np.array([1., 0.])
19
20a = de.OdeSystem(rhs, y0=y_init, dense_output=True, t=(0, 2*np.pi), dt=0.01, rtol=1e-9, atol=1e-9, constants=dict(k=1.0, m=1.0))
21
22print(a)
23
24a.integrate()
25
26print(a)
27
28print("If the integration was successful and correct, a[0].y and a[-1].y should be near identical.")
29print("a[0].y = {}".format(a[0].y))
30print("a[-1].y = {}".format(a[-1].y))
31
32print("Maximum difference from initial state after one oscillation cycle: {}".format(np.max(np.abs(a[0].y-a[-1].y))))
Place it into a getting_started.py text file and run it with
python getting_started.py
This script shows the numerical integration of a Hooke’s Law spring (harmonic oscillator) for a single cycle.
We recommend the use of Jupyter or ipython to enjoy desolver the most.