2.2. Ordinary differential equations
Let the source function \(F:[t_0, T] \times ℝ \to ℝ\) be given, and the initial data \(u_0:ℝ \to ℝ\) be given. Consider the initial value problem:
with the initial condition \(u(t_0) = u_0(t_0)\).
To solve the problem numerically, the subpackage specular.ode provides the following numerical schemes:
- the specular Euler scheme (Type 1 ~ 6)
- the specular trigonometric scheme
- the specular Heun scheme
- the explicit Euler scheme
- the implicit Euler scheme
- the Crank-Nicolson scheme
2.2.1. Specular Euler scheme
All functions return an instance of the ODEResult class that encapsulates the numerical results.
import specular
def F(t, u):
return -2*u
specular.Euler_scheme(of_Type='1', F=F, t_0=0.0, u_0=1.0, T=2.5, h=0.1)
Running the specular Euler scheme of Type 1: 100%|██████████| 24/24 [00:00<?, ?it/s]
<specular.ode.result.ODEResult at 0x1765982d8d0>
To access the numerical results, call .history().
It returns a tuple containing the time grid and the numerical solution.
import specular
def F(t, u):
return -2*u
specular.Euler_scheme(of_Type=1, F=F, t_0=0.0, u_0=1.0, T=2.5, h=0.1).history()
Running the specular Euler scheme of Type 1: 100%|██████████| 24/24 [00:00<?, ?it/s]
(array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1, 1.2,
1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4, 2.5]),
array([1. , 0.8 , 0.62169432, 0.48101574, 0.37172557,
0.2870388 , 0.22149069, 0.17081087, 0.13166787, 0.1014624 ,
0.07816953, 0.06021577, 0.04638162, 0.0357239 , 0.02751427,
0.02119088, 0.01632056, 0.0125695 , 0.00968054, 0.00745555,
0.00574195, 0.00442221, 0.00340579, 0.00262299, 0.00202011,
0.0015558 ]))
To visualize the numerical results, call .visualization().
import specular
import numpy as np
def F(t, u):
return -2*u
def exact_sol(t):
return np.exp(-2*t)
def u_0(t_0):
return exact_sol(t_0)
specular.Euler_scheme(of_Type='1', F=F, t_0=0.0, u_0=u_0, T=2.5, h=0.1).visualization(exact_sol=exact_sol, save_path="specular-Euler-scheme-of-Type-1")
Running the specular Euler scheme of Type 1: 100%|██████████| 24/24 [00:00<?, ?it/s]
Figure saved: figures\specular-Euler-scheme-of-Type-1

To obtain the table of the numerical results, call .table().
import specular
import numpy as np
def F(t, u):
return -2*u
def exact_sol(t):
return np.exp(-2*t)
def u_0(t_0):
return exact_sol(t_0)
specular.Euler_scheme(of_Type=4, F=F, t_0=0.0, u_0=u_0, T=2.5, h=0.1).table(exact_sol=exact_sol, save_path="specular-Euler-scheme-of-type-4")
Running the specular Euler scheme of Type 4: 100%|██████████| 25/25 [00:00<?, ?it/s]
Table saved: tables\specular-Euler-scheme-of-type-4.csv
.visualization() and .table() are chainable.
import specular
import numpy as np
def F(t, u):
return -2*u
def exact_sol(t):
return np.exp(-2*t)
def u_0(t_0):
return exact_sol(t_0)
specular.Euler_scheme(of_Type=4, F=F, t_0=0.0, u_0=u_0, T=2.5, h=0.1).visualization(exact_sol=exact_sol).table(exact_sol=exact_sol)
Running the specular Euler scheme of Type 4: 100%|██████████| 25/25 [00:00<?, ?it/s]
To compute the total error of the numerical results, call .total_error().
The exact solution is required.
The norm can be max, l1, or l2.
def F(t, u):
return -2*u
def exact_sol(t):
return np.exp(-2*t)
def u_0(t_0):
return exact_sol(t_0)
specular.Euler_scheme(of_Type=5, F=F, t_0=0.0, u_0=u_0, T=10.0, h=0.1).total_error(exact_sol=exact_sol, norm='max')
Running the specular Euler scheme of Type 5: 100%|██████████| 100/100 [00:00<00:00, 300882.64it/s]
0.0011409613137273178
2.2.2. Specular trigonometric scheme
import specular
def F(t, u):
return -2*u
def exact_sol(t):
return np.exp(-2*t)
def u_0(t_0):
return exact_sol(t_0)
t_0 = 0.0
h = 0.1
u_1 = exact_sol(t_0 + h)
specular.trigonometric_scheme(F=F, t_0=t_0, u_0=u_0, u_1=u_1, T=2.5, h=h).visualization(exact_sol=exact_sol, save_path="specular-trigonometric")
Running specular trigonometric scheme: 100%|██████████| 24/24 [00:00<?, ?it/s]
Figure saved: figures\specular-trigonometric

2.2.3. Classical schemes
The three classical schemes are available: the explicit Euler, the implicit Euler, and the Crank-Nicolson schemes.
import specular
import numpy as np
import matplotlib.pyplot as plt
def F(t, u):
return -(t*u)/(1-t**2)
def exact_sol(t):
return np.sqrt(1 - t**2)
def u_0(t_0):
return exact_sol(t_0)
t_0 = 0.0
T = 0.9
h = 0.05
result_EE = specular.classical_scheme(F=F, t_0=t_0, u_0=u_0, T=T, h=h, form="explicit Euler").history()
result_IE = specular.classical_scheme(F=F, t_0=t_0, u_0=u_0, T=T, h=h, form="implicit Euler").history()
result_CN = specular.classical_scheme(F=F, t_0=t_0, u_0=u_0, T=T, h=h, form="Crank-Nicolson").history()
exact_values = np.array([exact_sol(t) for t in result_EE[0]])
plt.figure(figsize=(5.5, 2.5))
plt.plot(result_EE[0], exact_values, color='black', label='Exact solution')
plt.plot(result_EE[0], result_EE[1], marker='x', linestyle='None', markerfacecolor='none', markeredgecolor='red', label='Explicit Euler')
plt.plot(result_IE[0], result_IE[1], marker='x', linestyle='None', markerfacecolor='none', markeredgecolor='blue', label='Implicit Euler')
plt.plot(result_CN[0], result_CN[1], marker='x', linestyle='None', markerfacecolor='none', markeredgecolor='purple', label='Crank-Nicolson')
plt.xlabel(r"Time", fontsize=10)
plt.ylabel(r"Solution", fontsize=10)
plt.grid(True)
plt.legend(loc='center left', bbox_to_anchor=(1.02, 0.5), borderaxespad=0., fontsize=10)
plt.savefig('figures/classical-schemes.png', dpi=1000, bbox_inches='tight')
plt.show()
Running the explicit Euler scheme: 100%|██████████| 18/18 [00:00<?, ?it/s]
Running the implicit Euler scheme: 100%|██████████| 18/18 [00:00<?, ?it/s]
Running Crank-Nicolson scheme: 100%|██████████| 18/18 [00:00<00:00, 17988.44it/s]

2.2.4. API Reference
specular.ode.solver
Let the source function \(F:[t_0, T] \times \mathbb{R} \to \mathbb{R}\) be given, and the initial data \(u_0:\mathbb{R} \to \mathbb{R}\) be given. Consider the initial value problem:
with the initial condition \(u(t_0) = u_0(t_0)\). To solve (IVP) numerically, this module provides implementations of the specular Euler schemes and the specular trigonometric scheme.
Euler_scheme(of_Type, F, t_0, u_0, T, h=1e-06, u_1=None, tol=1e-12, zero_tol=1e-08, max_iter=100)
Solves an initial value problem (IVP) using the specular Euler scheme of Type 1, 2, 3, 4, 5, and 6.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
of_Type
|
int | str
|
The type of the specular Euler scheme.
Options: |
required |
F
|
callable
|
The given source function |
required |
t_0
|
float
|
The starting time of the simulation. |
required |
u_0
|
callable
|
The given initial condition |
required |
T
|
float
|
The end time of the simulation. |
required |
h
|
float
|
Mesh size used in the finite difference approximation. Must be positive. |
1e-06
|
u_1
|
callable | float | None
|
The numerical solution at the time |
None
|
tol
|
float | optional
|
Tolerance for fixed-point iteration Used for Types 3, 4, 5, and 6. |
1e-12
|
zero_tol
|
float | floating
|
A small threshold used to determine if the denominator (alpha + beta) is close to zero for numerical stability. |
1e-08
|
max_iter
|
int | optional
|
Max iterations for fixed-point solver. |
100
|
Returns:
| Type | Description |
|---|---|
ODEResult
|
An object containing |
Source code in specular\ode\solver.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
Heun_scheme(F, t_0, u_0, T, h=1e-06, zero_tol=1e-08)
Solves an initial value problem using the specular Heun scheme.
This is the one-step, two-stage scheme obtained from one fixed-point iteration of the specular Euler scheme of Type 6.
Source code in specular\ode\solver.py
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | |
ellipse_scheme(F, t_0, u_0, T, a, b, h=1e-06, tol=1e-12, zero_tol=1e-08, max_iter=100)
Solves an initial value problem (IVP) using the specular ellipse scheme.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
F
|
callable
|
The given source function |
required |
t_0
|
float
|
The starting time of the simulation. |
required |
u_0
|
callable | float
|
The given initial condition |
required |
T
|
float
|
The end time of the simulation. |
required |
a
|
float
|
The semi-axis length in the |
required |
b
|
float
|
The semi-axis length in the |
required |
h
|
float
|
Mesh size used in the finite difference approximation. Must be positive. |
1e-06
|
tol
|
float
|
Tolerance for fixed-point iteration. |
1e-12
|
zero_tol
|
float | floating
|
A small threshold used to determine if the denominator (alpha + beta) is close to zero for numerical stability. |
1e-08
|
max_iter
|
int
|
Max iterations for fixed-point solver. |
100
|
Returns:
| Type | Description |
|---|---|
ODEResult
|
An object containing |
Source code in specular\ode\solver.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |
trigonometric_scheme(F, t_0, u_0, u_1, T, h=1e-06)
Solves an initial value problem (IVP) using the specular trigonometric scheme.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
F
|
callable
|
The given source function |
required |
t_0
|
float
|
The starting time of the simulation. |
required |
u_0
|
callable
|
The given initial condition |
required |
u_1
|
callable | float
|
The numerical solution at the time |
required |
T
|
float
|
The end time of the simulation. |
required |
h
|
float
|
Mesh size used in the finite difference approximation. Must be positive. |
1e-06
|
Returns:
| Type | Description |
|---|---|
ODEResult
|
An object containing |
Source code in specular\ode\solver.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | |
specular.ode.classical_solver
Classical fixed-step schemes for scalar first-order ODEs.
specular.ode.result
ODEResult
Source code in specular\ode\result.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
history()
Returns the time grid and the numerical solution as a tuple.
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, ndarray]
|
(time_grid, numerical_sol) |
Source code in specular\ode\result.py
17 18 19 20 21 22 23 24 25 26 | |
total_error(exact_sol, norm='max')
Calculates the error between the numerical solution and the exact solution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exact_sol
|
callable | list | ndarray
|
A function that returns the exact solution at a given time |
required |
norm
|
str | optional
|
The type of norm to use |
'max'
|
Returns:
| Type | Description |
|---|---|
float
|
The computed error value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in specular\ode\result.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |