Skip to content
DeepForgeby svx
All problems
HardCalculusca-048

Harmonic Oscillator Euler Step

Problem

Take one explicit Euler step for the harmonic oscillator x'' = -w^2 * x, written as the first-order system x' = v, v' = -w^2 * x.

Given position x, velocity v, frequency w, and step dt, return [x + dt*v, v - dt*w^2*x].

Hint

Update the velocity with the old position, then update the position with the old velocity.

Test cases

case 1
in: [1,0,1,0.1]
expected: [1,-0.1]
case 2
in: [0,2,2,0.25]
expected: [0.5,2]
case 3
in: [2,-1,0.5,0.5]
expected: [1.5,-1.25]
case 4
in: [1,1,0,0.5]
expected: [1.5,1]
Starter codePython
def harmonic_euler_step(x, v, w, dt):
    # Return [x_new, v_new]
    # Your code here
    pass

4 test cases · run your code against them in the editor