LORENZ
Overview
The LORENZ function numerically solves the classic Lorenz system of ordinary differential equations, a model for chaotic dynamics in atmospheric science and nonlinear systems. The Lorenz system describes the evolution of three variables (X, Y, Z) governed by three parameters: Prandtl number (sigma), Rayleigh number (rho), and a geometric factor (beta). The equations are:
The function uses SciPy’s ODE solver (scipy.integrate.solve_ivp ) to integrate the system from t_start to t_end. Only the classic three-variable Lorenz system is exposed; extensions and external forcing are not supported. The integration method can be selected from several common solvers, but only the most used options are exposed. This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=LORENZ(x_initial, y_initial, z_initial, sigma, rho, beta, t_start, t_end, [timesteps], [solve_ivp_method])x_initial(float, required): Initial value for X.y_initial(float, required): Initial value for Y.z_initial(float, required): Initial value for Z.sigma(float, required): Prandtl number.rho(float, required): Rayleigh number.beta(float, required): Geometric factor.t_start(float, required): Start time for integration.t_end(float, required): End time for integration.timesteps(int, optional, default=10): Number of timesteps to solve for.solve_ivp_method(string (enum), optional, default=‘RK45’): Integration method. Valid options:RK45,RK23,DOP853,Radau,BDF,LSODA.
The function returns a 2D array (table) with columns: t, X, Y, Z, representing time and the three variables at each step. If the input is invalid or integration fails, a string error message is returned.
Examples
Example 1: Classic Lorenz, Default Method
Inputs:
| x_initial | y_initial | z_initial | sigma | rho | beta | t_start | t_end | timesteps |
|---|---|---|---|---|---|---|---|---|
| 1.0 | 1.0 | 1.0 | 10.0 | 28.0 | 2.667 | 0.0 | 2.0 | 10 |
Excel formula:
=LORENZ(1.0, 1.0, 1.0, 10.0, 28.0, 2.667, 0.0, 2.0, 10)Expected output:
| t | X | Y | Z |
|---|---|---|---|
| 0.0 | 1.0 | 1.0 | 1.0 |
| 0.222 | 8.316 | 17.13 | 6.414 |
| 0.444 | 8.48 | -7.117 | 39.99 |
| 0.667 | -6.458 | -8.345 | 25.27 |
| 0.889 | -9.589 | -10.23 | 27.69 |
| 1.111 | -7.985 | -6.744 | 27.98 |
| 1.333 | -7.666 | -8.676 | 24.54 |
| 1.556 | -9.831 | -9.726 | 28.8 |
| 1.778 | -7.42 | -6.509 | 26.95 |
| 2.0 | -8.168 | -9.518 | 24.67 |
Example 2: Classic Lorenz, RK23 Method
Inputs:
| x_initial | y_initial | z_initial | sigma | rho | beta | t_start | t_end | timesteps | solve_ivp_method |
|---|---|---|---|---|---|---|---|---|---|
| 1.0 | 1.0 | 1.0 | 10.0 | 28.0 | 2.667 | 0.0 | 2.0 | 10 | RK23 |
Excel formula:
=LORENZ(1.0, 1.0, 1.0, 10.0, 28.0, 2.667, 0.0, 2.0, 10, "RK23")Expected output:
| t | X | Y | Z |
|---|---|---|---|
| 0.0 | 1.0 | 1.0 | 1.0 |
| 0.222 | 8.308 | 17.11 | 6.397 |
| 0.444 | 8.413 | -7.002 | 39.78 |
| 0.667 | -6.407 | -8.32 | 25.05 |
| 0.889 | -9.69 | -10.31 | 27.81 |
| 1.111 | -7.897 | -6.644 | 27.91 |
| 1.333 | -7.694 | -8.759 | 24.48 |
| 1.556 | -9.823 | -9.626 | 28.91 |
| 1.778 | -7.373 | -6.55 | 26.78 |
| 2.0 | -8.29 | -9.636 | 24.85 |
Example 3: Short Time Span
Inputs:
| x_initial | y_initial | z_initial | sigma | rho | beta | t_start | t_end | timesteps |
|---|---|---|---|---|---|---|---|---|
| 0.0 | 1.0 | 2.0 | 10.0 | 28.0 | 2.667 | 0.0 | 0.1 | 10 |
Excel formula:
=LORENZ(0.0, 1.0, 2.0, 10.0, 28.0, 2.667, 0.0, 0.1, 10)Expected output:
| t | X | Y | Z |
|---|---|---|---|
| 0.0 | 0.0 | 1.0 | 2.0 |
| 0.011 | 0.105 | 1.004 | 1.942 |
| 0.022 | 0.201 | 1.038 | 1.887 |
| 0.033 | 0.292 | 1.097 | 1.835 |
| 0.044 | 0.381 | 1.183 | 1.786 |
| 0.056 | 0.471 | 1.293 | 1.739 |
| 0.067 | 0.565 | 1.429 | 1.696 |
| 0.078 | 0.664 | 1.592 | 1.657 |
| 0.089 | 0.772 | 1.784 | 1.622 |
| 0.1 | 0.89 | 2.006 | 1.592 |
Example 4: Different Parameters
Inputs:
| x_initial | y_initial | z_initial | sigma | rho | beta | t_start | t_end | timesteps |
|---|---|---|---|---|---|---|---|---|
| 2.0 | 3.0 | 4.0 | 14.0 | 35.0 | 3.0 | 0.0 | 1.0 | 10 |
Excel formula:
=LORENZ(2.0, 3.0, 4.0, 14.0, 35.0, 3.0, 0.0, 1.0, 10)Expected output:
| t | X | Y | Z |
|---|---|---|---|
| 0.0 | 2.0 | 3.0 | 4.0 |
| 0.111 | 8.113 | 16.09 | 6.988 |
| 0.222 | 24.17 | 26.37 | 50.63 |
| 0.333 | 3.532 | -10.19 | 45.24 |
| 0.444 | -6.605 | -8.593 | 35.25 |
| 0.556 | -8.298 | -9.281 | 31.51 |
| 0.667 | -10.36 | -11.88 | 31.98 |
| 0.778 | -11.82 | -11.75 | 36.03 |
| 0.889 | -10.16 | -8.519 | 36.45 |
| 1.0 | -8.303 | -7.881 | 32.76 |
Python Code
import micropip
await micropip.install(['scipy', 'numpy'])
from scipy.integrate import solve_ivp
import numpy as np
def lorenz(x_initial, y_initial, z_initial, sigma, rho, beta, t_start, t_end, timesteps=10, solve_ivp_method='RK45'):
"""
Numerically solves the Lorenz system of ordinary differential equations for chaotic dynamics.
Args:
x_initial: Initial x value.
y_initial: Initial y value.
z_initial: Initial z value.
sigma: Prandtl number.
rho: Rayleigh number.
beta: Geometric factor.
t_start: Start time of integration.
t_end: End time of integration.
timesteps: Number of timesteps to solve for. Default is 10.
solve_ivp_method: Integration method ('RK45', 'RK23', 'DOP853', 'Radau', 'BDF', 'LSODA'). Default is 'RK45'.
Returns:
2D list with header row: t, X, Y, Z. Each row contains time and variable values, or an error message (str) if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
# Validate input types and ranges
try:
x0 = float(x_initial)
y0 = float(y_initial)
z0 = float(z_initial)
sig = float(sigma)
rh = float(rho)
bet = float(beta)
t0 = float(t_start)
t1 = float(t_end)
ntp = int(timesteps)
except Exception:
return "Invalid input: All initial values, parameters, and timesteps must be numbers."
if t1 <= t0:
return "Invalid input: t_end must be greater than t_start."
if ntp <= 0:
return "Invalid input: timesteps must be a positive integer."
if solve_ivp_method not in ['RK45', 'RK23', 'DOP853', 'Radau', 'BDF', 'LSODA']:
return "Invalid input: solve_ivp_method must be one of 'RK45', 'RK23', 'DOP853', 'Radau', 'BDF', 'LSODA'."
# Create time array for evaluation
t_eval = np.linspace(t0, t1, ntp)
# Lorenz system definition
def lorenz_ode(t, xyz):
x, y, z = xyz
dxdt = sig * (y - x)
dydt = x * (rh - z) - y
dzdt = x * y - bet * z
return [dxdt, dydt, dzdt]
# Integrate ODE
try:
sol = solve_ivp(
lorenz_ode,
[t0, t1],
[x0, y0, z0],
method=solve_ivp_method,
dense_output=False,
t_eval=t_eval
)
except Exception as e:
return f"Integration error: {e}"
if not sol.success:
return f"Integration failed: {sol.message}"
# Format output: header row then data rows
result = [['t', 'X', 'Y', 'Z']]
for i in range(len(sol.t)):
t_val = float(sol.t[i])
x_val = float(sol.y[0][i])
y_val = float(sol.y[1][i])
z_val = float(sol.y[2][i])
# Disallow nan/inf
if any([
not isinstance(t_val, float) or not isinstance(x_val, float) or not isinstance(y_val, float) or not isinstance(z_val, float),
t_val != t_val or x_val != x_val or y_val != y_val or z_val != z_val, # NaN check
abs(t_val) == float('inf') or abs(x_val) == float('inf') or abs(y_val) == float('inf') or abs(z_val) == float('inf')
]):
return "Invalid output: nan or inf detected."
result.append([t_val, x_val, y_val, z_val])
return result