Skip to content
DeepForgeby svx
All problems
EasyCalculusca-109

Euler-Lagrange Discrete Step

Problem

The discrete Euler-Lagrange equation for L = 0.5*m*((x_k - x_{k-1})/dt)^2 - 0.5*k*x_k^2 gives the update:

x_{k+1} = 2*x_k - x_{k-1} - (dt^2 * k / m) * x_k

Given the previous two positions and the parameters, return the next position.

Hint

This is a discrete version of Newton's second law written as a two-step recurrence.

Test cases

case 1
in: [0,1,0.1,1,1]
expected: 1.99
case 2
in: [1,1,0.1,1,1]
expected: 0.99
case 3
in: [0,1,0,1,1]
expected: 2
case 4
in: [2,0,0.5,2,4]
expected: -2
Starter codePython
def discrete_euler_lagrange(x_prev, x_curr, dt, m, k):
    # Your code here
    pass

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