oneshotlm
Compare all models on this prompt →

Double pendulum · deepseek-r1-0528

Simulate a chaotic double pendulum on a canvas using the correct equations of motion, drawing the arms and a colorful fading trail of the tip.

⛨ sandboxed Open in new tab ↗
transcript · 4 messages
user

Simulate a chaotic double pendulum on a canvas using the correct equations of motion, drawing the arms and a colorful fading trail of the tip.

assistant
thinking
We are going to create a single HTML file (index.html) that:
1. Contains a <canvas> element for drawing the double pendulum and its trail.
2. Uses inline JavaScript to simulate the double pendulum physics and draw the animation.
3. Uses inline CSS for basic styling (e.g., making the canvas full window).

The double pendulum simulation requires solving the equations of motion for the two angles. We'll use numerical integration (Euler or Runge-Kutta) to update the state.

We'll define:
- Constants: gravity, lengths of the two arms, masses (optional since equations often simplify), and time step.
- State variables: angles (theta1, theta2) and angular velocities (omega1, omega2).

Equations of motion for a double pendulum (for the derivation, see physics resources):

We'll use the equations from: 
https://www.myphysicslab.com/pendulum/double-pendulum-en.html

Let:
m1, m2 = masses (we can set to 1 if they don't matter, but keep general)
l1, l2 = lengths
g = gravity

We'll denote:
theta1, theta2: angles from vertical (positive counterclockwise)
omega1 = d(theta1)/dt, omega2 = d(theta2)/dt

Then we have the accelerations (alpha1 and alpha2) given by:

num1 = -g*(2*m1+m2)*sin(theta1) - m2*g*sin(theta1-2*theta2) - 2*sin(theta1-theta2)*m2*(omega2*omega2*l2 + omega1*omega1*l1*cos(theta1-theta2))
den1 = l1*(2*m1+m2 - m2*cos(2*theta1-2*theta2))

num2 = 2*sin(theta1-theta2) * (omega1*omega1*l1*(m1+m2) + g*(m1+m2)*cos(theta1) + omega2*omega2*l2*m2*cos(theta1-theta2))
den2 = l2*(2*m1+m2 - m2*cos(2*theta1-2*theta2))

Then:
alpha1 = num1 / den1
alpha2 = num2 / den2

But note: this is a compact form. Alternatively, we can express the accelerations in a more standard form (via Lagrangian mechanics) and then solve the system:

We have:

a1 = (m1+m2)*l1
a2 = m2*l2*cos(theta1 - theta2)
a3 = m2*l1*cos(theta1 - theta2)
a4 = m2*l2
a5 = -m2*l2*omega2*omega2*sin(theta1 - theta2) - (m1+m2)*g*sin(theta1)
a6 = m2*l1*omega1*omega1*sin(theta1 - theta2) - m2*g*sin(theta2)

Then the system:

a1 * alpha1 + a2 * alpha2 = a5
a3 * alpha1 + a4 * alpha2 = a6

Then we can solve for alpha1 and alpha2 by inverting the matrix:

[ a1, a2; a3, a4 ] * [alpha1; alpha2] = [a5; a6]

We'll go with the second method because it is more straightforward to code.

We'll then update using Euler method (or better, Runge-Kutta 4). But for simplicity we'll use Euler first, and if needed switch to RK4.

Additionally, we want:
- A trail of the tip of the second pendulum that fades away (we'll store positions in an array and draw with decreasing opacity).

Implementation plan:

1. Set up the canvas and context.
2. Define the pendulum parameters and state.
3. Update the state using the equations of motion in a loop (using requestAnimationFrame).
4. Draw the pendulum arms and the trail.

We'll make the trail as a set of points with a maximum length. We'll draw the trail using a path with decreasing alpha.

Let's write the code:

We'll create the index.html with:
- A canvas that fills the window.
- A script that:
   * sets up the canvas
   * defines the simulation parameters and state
   * an update function that computes the derivatives and updates the state
   * a draw function that draws the pendulum and the trail

We'll use the Euler method for now.

Note: The Euler method is unstable for chaotic systems, but we are taking small steps and it's acceptable for a basic simulation. We can later switch to a better method if needed.

Let me write the equations more precisely:

Let:
  l1, l2: lengths
  m1, m2: masses
  theta1, theta2: angles (from vertical, positive counterclockwise)
  omega1, omega2: angular velocities

We define the matrix:

  a = l1 * (m1 + m2)
  b = m2 * l2 * cos(theta1 - theta2)
  c = l1 * m2 * cos(theta1 - theta2)
  d = l2 * m2

And the right hand side:

  e = -m2 * l2 * omega2^2 * sin(theta1 - theta2) - (m1 + m2) * g * sin(theta1)
  f = m2 * l1 * omega1^2 * sin(theta1 - theta2) - m2 * g * sin(theta2)

Then:

  [ a  b ] [ alpha1 ] = [ e ]
  [ c  d ] [ alpha2 ]   [ f ]

We can solve:

  det = a*d - b*c
  alpha1 = (d*e - b*f) / det
  alpha2 = (a*f - c*e) / det

Then we update:

  omega1 += alpha1 * dt
  omega2 += alpha2 * dt
  theta1 += omega1 * dt
  theta2 += omega2 * dt

For the trail, we push the current (x2, y2) to an array and keep a fixed number of points (say 500). We'll draw the trail by drawing a polyline with decreasing opacity.

We'll set initial conditions to a slightly perturbed vertical (so it falls and moves).

We also need to convert the angles to Cartesian coordinates for drawing:

  x1 = px + l1 * sin(theta1)
  y1 = py + l1 * cos(theta1)
  x2 = x1 + l2 * sin(theta2)
  y2 = y2 + l2 * cos(theta2)

Where (px, py) is the pivot point (center of canvas top).

Now, let's write the code.

I'll create a self-contained HTML file simulating a chaotic double pendulum with the trail effect. Here's the plan: 1. Set up a full-screen canvas 2. Implement double pendulum physics using Lagrangian mechanics 3. Use numerical integration (Runge-Kutta 4) for accuracy 4. Create a colorful fading trail using an array of past positions 5. Draw pendulum arms and trail with vibrant colors Here's the complete implementation:

write /tmp/ws/index.html (6655 bytes)
toolResult

Successfully wrote 6655 bytes to /tmp/ws/index.html

assistant
thinking
We will create a single HTML file that simulates a chaotic double pendulum.
 The file will contain:
   - A canvas for drawing the pendulum and its trail.
   - CSS for full-screen and background.
   - JavaScript for the physics simulation and drawing.

 Physics:
   The double pendulum is a chaotic system with two rods and two masses.
   We will use the Lagrangian mechanics to derive the equations of motion.

 Equations of motion (for two angles θ1 and θ2):
   We will use a state vector [θ1, ω1, θ2, ω2] (angles and angular velocities).

   The derivatives (angular accelerations) can be computed as:
     dθ1/dt = ω1
     dθ2/dt = ω2

     dω1/dt = ( -g*(2*m1+m2)*sin(θ1) - m2*g*sin(θ1-2*θ2) 
                - 2*sin(θ1-θ2)*m2*(ω2^2*L2 + ω1^2*L1*cos(θ1-θ2)) )
                / ( L1*(2*m1 + m2 - m2*cos(2*θ1-2*θ2)) )

     dω2/dt = ( 2*sin(θ1-θ2)*( ω1^2*L1*(m1+m2) + g*(m1+m2)*cos(θ1) 
                + ω2^2*L2*m2*cos(θ1-θ2) ) )
                / ( L2*(2*m1 + m2 - m2*cos(2*θ1-2*θ2)) )

 However, note: There are different formulations. We'll use a standard one.

 Alternatively, we can derive by computing the Lagrangian and solving the system.

 To avoid complex derivation, we use a known form from literature:

   Let:
     Δθ = θ1 - θ2

   Then:

     num1 = -G * (2*m1 + m2) * sin(θ1)
            - m2 * G * sin(θ1 - 2*θ2)
            - 2 * sin(Δθ) * m2 * (ω2*ω2 * L2 + ω1*ω1 * L1 * cos(Δθ))
     den1 = L1 * (2*m1 + m2 - m2*cos(2*Δθ))

     α1 = num1 / den1

     num2 = 2 * sin(Δθ) * 
            ( ω1*ω1*L1*(m1+m2) + G*(m1+m2)*cos(θ1) + ω2*ω2*L2*m2*cos(Δθ) )
     den2 = L2 * (2*m1 + m2 - m2*cos(2*Δθ))

     α2 = num2 / den2

 But note: The above formulation might be one of many. We'll use a common one.

 We integrate using Runge-Kutta 4th order (RK4) for stability.

 Drawing:
   - We draw the pendulum arms and masses.
   - We also draw a trail for the tip of the second pendulum with a fading effect.

 Implementation:

   We'll set up a canvas that fills the window.

   We'll define:
     pivot: (pivotX, pivotY) at the top center of the canvas.
     arm1Length, arm2Length: lengths of the two rods.
     mass1, mass2: masses (which we represent as circle radii).

   We'll maintain a state vector: [θ1, ω1, θ2, ω2].

   Each frame:
     1. Update the state using RK4 with a fixed time step.
     2. Calculate the positions of the two masses.
     3. Draw:
          - A semi-transparent background overlay to create a fading trail effect.
          - The trail (array of past positions of the second mass) with a gradient color (for example, from red to blue based on age).
          - The two arms as lines.
          - The two masses as circles.
     4. Add the current position of the second mass to the trail.

   We'll use a fixed time step for physics to be independent of frame rate.

 Let's code accordingly.

 We'll write the entire HTML, CSS, and JS in a single file.

 Note: The equations of motion we are using are from:
   https://www.myphysicslab.com/pendulum/double-pendulum-en.html

 But adjusted for our state representation.

 The equations:

   θ1'' = [ -g (2 m1 + m2) sin θ1 - m2 g sin(θ1 - 2 θ2) 
             - 2 sin(θ1 - θ2) m2 (θ2'^2 L2 + θ1'^2 L1 cos(θ1 - θ2)) ]
           / [ L1 (2 m1 + m2 - m2 cos(2 θ1 - 2 θ2)) ]

   θ2'' = [ 2 sin(θ1 - θ2) ( θ1'^2 L1 (m1 + m2) + g (m1 + m2) cos θ1 
             + θ2'^2 L2 m2 cos(θ1 - θ2) ) ]
           / [ L2 (2 m1 + m2 - m2 cos(2 θ1 - 2 θ2)) ]

 In code:

   Let dtheta = θ1 - θ2.

   Then:
     θ1'' = ( term1a - term1b - term1c ) / denominator1
        term1a = G * (2 * mass1 + mass2) * sin(θ1)
        term1b = mass2 * G * sin(θ1 - 2*θ2)  // Note: we have a negative sign in front, so it's negative in the numerator
        term1c = 2 * sin(dtheta) * mass2 * (ω2² * L2 + ω1² * L1 * cos(dtheta))

        denominator1 = L1 * (2*mass1 + mass2 - mass2*cos(2*dtheta))

   Similarly for θ2''

 But wait: our state vector is [θ1, ω1, θ2, ω2]. We are computing θ1'' (i.e. dω1/dt) and θ2'' (dω2/dt).

 However, note the above formula for θ1'' has the leading negative? The source has:

     θ1'' = [ -g (2 m1 + m2) sin θ1 ... ]

   So:

      dω1/dt = [ -G*(2*m1+m2)*sin(θ1) - m2*G*sin(θ1-2*θ2) - 2*sin(dtheta)*m2*(ω2*ω2*L2 + ω1*ω1*L1*cos(dtheta)) ] 
               / [ L1 * (2*m1+m2 - m2*cos(2*dtheta)) ]

   And for θ2'':

      dω2/dt = [ 2*sin(dtheta) * ( ω1*ω1*L1*(m1+m2) + G*(m1+m2)*cos(θ1) + ω2*ω2*L2*m2*cos(dtheta) ) ]
               / [ L2 * (2*m1+m2 - m2*cos(2*dtheta)) ]

   But note: in the source, the numerator for θ2'' has a leading 2 and the denominator has L2, and the same denominator structure.

 We'll code accordingly.

 Steps:

   1. Set up the canvas and context.
   2. Define parameters: lengths, masses, gravity, initial angles.
   3. State vector: [theta1, omega1, theta2, omega2]
   4. RK4 integration function.
   5. Derivatives function: returns [dθ1, dω1, dθ2, dω2] = [omega1, alpha1, omega2, alpha2]
   6. Animation loop: update state, draw, and requestAnimationFrame.

   For the trail: we store an array of {x, y, timestamp?} for the second mass. We draw the trail with gradient color and fading.

   We'll use a fixed trail length (e.g., 500 points). Each frame we push the new point and shift the oldest if over.

   Color: we can use HSL and vary the hue from 0 to 360 and then fade to transparent based on age.

   We draw the trail by connecting the points with lines, each with increasing transparency and changing color.

 Let's write the HTML file.

 Note: We use a black background.

 We set the canvas to full window.

 The pivot point is at (canvas.width/2, 200) so that it's near the top.

 We'll write the entire HTML in one string and use the write tool.

 Note: We'll use a small time step for RK4 to be stable. We can use the fixed time step of 1/60 seconds.

 Since we're using requestAnimationFrame, the actual time step might vary, but we'll fix the physics step to 16ms (about 60 FPS).

 We'll calculate the positions and then draw.

 Let's code accordingly.

I've created a self-contained HTML file that simulates a chaotic double pendulum with a colorful fading trail. The implementation includes: 1. Accurate physics simulation: - Uses Lagrangian mechanics for pendulum equations - Implements Runge-Kutta 4 (RK4) numerical integration - Handles both arms and masses with realistic physics 2. Visual elements: - Smooth white pendulum arms - Blue and red masses for visual distinction - Colorful fading trail for the end pendulum (hue shifts from red to purple) - Pivot point highlight 3. Performance optimization: - Fixed physics time step independent of rendering - Trail limit prevents memory bloat - Efficient trail drawing with single path per segment To view the simulation, simply open the generated `index.html` file in any modern browser. The pendulum will start in a near-horizontal position and demonstrate chaotic motion as it evolves. Would you like me to adjust any parameters like arm lengths, masses, or trail effects?

TAKE IT TO CODE

A single self-contained HTML file — copy it and open in any browser, or reuse the prompt with your own pi-agent run.

RUN

modeldeepseek-r1-0528
vendorDeepSeek
harnesspi-agent
generated2026-08-01

EVALUATION

4/5 working

Double pendulum swings with a colorful fading trail, animates continuously and reacts consistently to all inputs.

Vision grade · anthropic/claude-sonnet-5

MEASUREMENTS

Raw readings from the capture, not a verdict — the grade above is the only judgement. Percentages are how much of the screen changed; the measure under-reports sparse motion on a dark background.

idle motion0.7%
on click2.4%
on drag2.7%
on wheel2.4%
on enter + space1.7%
on arrow keys2.0%
on w/a/s/d3.0%
frame spread13.8 / 255
console errors0
js errors none

METRICS

runtime261.1s
tokens in5.0k
tokens out5.7k
cached0
cost / run$0.0147