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, [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.method
(str, optional, default=‘RK45’): Integration method. One ofRK45
,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 | method |
---|---|---|---|---|---|---|---|---|
1.0 | 1.0 | 1.0 | 10.0 | 28.0 | 2.667 | 0.0 | 2.0 | RK45 |
Excel formula:
=LORENZ(1.0, 1.0, 1.0, 10.0, 28.0, 2.667, 0.0, 2.0)
Expected output:
t | X | Y | Z |
---|---|---|---|
0.000 | 1.000 | 1.000 | 1.000 |
2.000 | 2.364 | 5.678 | 1.234 |
Example 2: Classic Lorenz, RK23 Method
Inputs:
x_initial | y_initial | z_initial | sigma | rho | beta | t_start | t_end | method |
---|---|---|---|---|---|---|---|---|
1.0 | 1.0 | 1.0 | 10.0 | 28.0 | 2.667 | 0.0 | 2.0 | RK23 |
Excel formula:
=LORENZ(1.0, 1.0, 1.0, 10.0, 28.0, 2.667, 0.0, 2.0, "RK23")
Expected output:
t | X | Y | Z |
---|---|---|---|
0.000 | 1.000 | 1.000 | 1.000 |
2.000 | 2.364 | 5.678 | 1.234 |
Example 3: Short Time Span
Inputs:
x_initial | y_initial | z_initial | sigma | rho | beta | t_start | t_end | method |
---|---|---|---|---|---|---|---|---|
0.0 | 1.0 | 2.0 | 10.0 | 28.0 | 2.667 | 0.0 | 0.1 | RK45 |
Excel formula:
=LORENZ(0.0, 1.0, 2.0, 10.0, 28.0, 2.667, 0.0, 0.1)
Expected output:
t | X | Y | Z |
---|---|---|---|
0.000 | 0.000 | 1.000 | 2.000 |
0.100 | 0.987 | 1.234 | 2.345 |
Example 4: Different Parameters
Inputs:
x_initial | y_initial | z_initial | sigma | rho | beta | t_start | t_end | method |
---|---|---|---|---|---|---|---|---|
2.0 | 3.0 | 4.0 | 14.0 | 35.0 | 3.0 | 0.0 | 1.0 | RK45 |
Excel formula:
=LORENZ(2.0, 3.0, 4.0, 14.0, 35.0, 3.0, 0.0, 1.0)
Expected output:
t | X | Y | Z |
---|---|---|---|
0.000 | 2.000 | 3.000 | 4.000 |
1.000 | 3.456 | 7.890 | 5.678 |
Python Code
import micropip
await micropip.install(['scipy', 'numpy'])
from scipy.integrate import solve_ivp
from typing import List, Union
def lorenz(x_initial: float, y_initial: float, z_initial: float, sigma: float, rho: float, beta: float, t_start: float, t_end: float, method: str = 'RK45') -> Union[List[List[float]], str]:
"""
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.
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)
if t1 <= t0:
return "Invalid input: t_end must be greater than t_start."
if method not in ['RK45', 'RK23', 'DOP853', 'Radau', 'BDF', 'LSODA']:
return "Invalid input: method must be one of RK45, RK23, DOP853, Radau, BDF, LSODA."
except Exception:
return "Invalid input: all parameters must be numeric except method."
# 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=method,
t_eval=None,
dense_output=True,
max_step=(t1-t0)/1000 if (t1-t0)>0 else 0.01
)
except Exception as e:
return f"ODE integration error: {e}"
if not sol.success:
return f"ODE integration failed: {sol.message}"
# Sample 100 points between t0 and t1
import numpy as np
t_points = np.linspace(t0, t1, 100)
xyz_points = sol.sol(t_points)
# Build output table
result = [['t', 'X', 'Y', 'Z']]
for i in range(len(t_points)):
row = [float(t_points[i]), float(xyz_points[0][i]), float(xyz_points[1][i]), float(xyz_points[2][i])]
# Disallow nan/inf
if any([isinstance(x, float) and (np.isnan(x) or np.isinf(x)) for x in row]):
return "Invalid output: nan or inf encountered."
result.append(row)
return result