Open In App
Related Articles

Creative Programming In Processing | Set 2 (Lorenz Attractor)

Improve Article
Save Article
Like Article

Creative Programming In Processing | Set 1 (Random Walker)
The Lorenz system is a system of ordinary differential equations first studied by Edward Norton Lorenz, an American mathematician and meteorologist around 1963. It is notable for having chaotic solutions for certain parameter values and initial conditions. It was derived from a simplified model of convection in the earth’s atmosphere. It also arises naturally in models of lasers and dynamos. The Lorenz attractor is a set of chaotic solutions of the Lorenz system which, when plotted, resemble a butterfly or figure eight. The following image appeared in the Nature journal 31 August 2000, pp 949 as part of an article titled The Lorenz Attractor Exists, written by Ian Stewart.

The Lorenz attractor system is most commonly expressed as 3 coupled non-linear differential equations-

  • dx / dt = a (y - x)
  • dy / dt = x (b - z) - y
  • dz / dt = xy - c z

In the above set of equations, ‘a’ is sometimes known as the Prandtl number and ‘b’ the Rayleigh number. One commonly used set of constants is a = 10, b = 28, c = 8 / 3. Another is a = 28, b = 46.92, c = 4.

Sample implementation of the differential equations in java:-

int i = 0;
double x0, y0, z0, x1, y1, z1;
double h = 0.01, a = 10.0, b = 28.0, c = 8.0 / 3.0;
x0 = 0.1;
y0 = 0;
z0 = 0;
for (i = 0; i < N; i++) {
    x1 = x0 + h * a * (y0 - x0);
    y1 = y0 + h * (x0 * (b - z0) - y0);
    z1 = z0 + h * (x0 * y0 - c * z0);
    x0 = x1;
    y0 = y1;
    z0 = z1;
    // Printing the coordinates
    if (i > 100)
        System.out.println(i + " " + x0 + " " + y0 + " " + z0);

We will try to implement the above logic in Processing Java visually. Since we will be plotting the points in 3d, we need use a 3d renderer. We will be using the OPENGL renderer in the following implementation but a P3D renderer can also be used. We also need to use an external Processing library called PeasyCam which helps us to create an interactive camera object for 3d environment workflow. PeasyCam can be downloaded using the Processing IDE from tools ->Add Tool -> Libraries.

We will also be using the following functions to represent the Lorenz Attractor structure-

  • beginShape() – begins recording vertices for a shape.
  • endShape() – stops recording vertices for a shape.
  • vertex() – this function is used to specify the vertex coordinates for points, lines, triangles, quads, and polygons. It is used exclusively within the beginShape() and endShape() functions.

Implementation of Lorenz Attractor in Processing java:-

import peasy.*; // Importing peasy package
// Initialization
float x = 0.01, y = 0, z = 0;
float a = 10, b = 28, c = 8.0 / 3.0;
// ArrayList of PVector objects to store
// the position vectors of the points to be plotted.
ArrayList<PVector> points = new ArrayList<PVector>();
PeasyCam cam; // Declaring PeasyCam object
void setup()
    // Creating the output window
    // and setting up the OPENGL renderer
    size(800, 600, OPENGL);
    // Initializing the cam object
    cam = new PeasyCam(this, 500);
void draw()
    // Implementation of the differential equations
    float dt = 0.01;
    float dx = (a * (y - x)) * dt;
    float dy = (x * (b - z) - y) * dt;
    float dz = (x * y - c * z) * dt;
    x += dx;
    y += dy;
    z += dz;
    // Adding the position vectors to points ArrayList
    points.add(new PVector(x, y, z));
    translate(0, 0, -80);
    // Beginning plotting of points
    for (PVector v : points) {
        // Adding random color to the structure in each frame
        stroke(random(0, 255), random(0, 255), random(0, 255));
        vertex(v.x, v.y, v.z); // plotting the vertices
    endShape(); // Drawing ends


Source Materials

  • The Coding Train Coding Challenges by Daniel Shiffman.
  • The Lorenz Attractor in 3D by Paul Bourke.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 19 Sep, 2023
Like Article
Save Article
Similar Reads