In [1]:
using Plots

ODE to JOY¶

$$ \begin{align} \frac{d^2}{dt^2} x = -\omega^2 x \end{align} $$

basic strategies:¶

Euler's method

RK4

Implicit method

In [2]:
let

    ww = 1.0

    ff = (t, x) -> [x[2], -ww^2*x[1]]

    
    dt = 0.25
    tv = range(0.0, 44.0, step = dt)

    N = length(tv)

    xv = [zeros(2) for tt in tv]

    data = zeros(N, 3)

    # initial condition
    xv[1] = [1.0, 4.25]
    data[1, :] = [tv[1], xv[1]...]
    
    for i1 in 2:N

        tt = tv[i1-1]
        xx = xv[i1-1]
    
        # RK4 on the fly
        f1 = ff(tt, xx)
        f2 = ff(tt+dt/2, xx+dt*f1/2)
        f3 = ff(tt+dt/2, xx+dt*f2/2)
        f4 = ff(tt+dt, xx+dt*f3)

        xv[i1] = xx + dt * (f1+2*f2+2*f3+f4)/6

        data[i1, 1:3]= [tt, xv[i1]...]
        
    end
        
    p1 = scatter(
        tv, data[:, 2],
        label="numerical", alpha = 0.4, color=:red,
        xlabel="t", ylabel="x(t)"
    )
    
    # ref. result
    plot!(tv, t -> data[1,2] * cos(ww * t) + data[1,3]/ww * sin(ww*t), 
        label = "natural frequency", color="gray", line=(dash=:dash))
            
end
    

    
    
Out[2]:
No description has been provided for this image
In [3]:
let

    # implicit method
    ww = 1.0

    ff = (t, x) -> [x[2], -ww^2*x[1]]

    
    dt = 0.25
    tv = range(0.0, 25.0, step = dt)

    N = length(tv)

    xv = [zeros(2) for tt in tv]

    data = zeros(N, 3)

    # initial condition
    xv[1] = [1.0, 4.25]
    data[1, :] = [tv[1], xv[1]...]

    tol = 1E-5
    
    for i1 in 2:N

        tt = tv[i1-1]
        xx = xv[i1-1]

        _f = y -> (xx + dt * ff(tt+dt/2, (xx+y)/2))

        yy = xx

        while true

            yy = _f(yy)
            if sum(abs2.(yy-_f(yy))) < tol break end

        end

        xv[i1] = yy

        data[i1, 1:3]= [tt, xv[i1]...]
        
    end

    
    p1 = scatter(
        tv, data[:, 2],
        label="numerical", alpha = 0.4, color=:red,
        xlabel="t", ylabel="x(t)"
    )
    
    # ref. result
    plot!(tv, t -> data[1,2] * cos(ww * t) + data[1,3]/ww * sin(ww*t), 
        label = "natural frequency", color="gray", line=(dash=:dash))
    
    # p2 = plot(data[:, 2], data[:,3])
    
        
end
    

    
    
Out[3]:
No description has been provided for this image

Morse Potential¶

The Morse potential models a bond that can stretch and break. For unit mass,

$$ V(x)=\frac12(1-e^{-\omega x})^2, $$

where $x$ is displacement from equilibrium and $\omega>0$ is the small-amplitude frequency.

Near $x=0$,

$$ V(x)\approx\frac12\omega^2x^2, $$

so the motion resembles a simple harmonic oscillator. The full equation of motion is

$$ \ddot x=-V'(x) =-\omega e^{-\omega x}(1-e^{-\omega x}). $$

As $x\to\infty$, the potential approaches $1/2$. Starting at equilibrium, a kick with $|v_0|<1$ produces bounded oscillations; at $|v_0|\ge1$, the particle can escape. The SHO remains bounded for any finite kick.

In [4]:
let

    ww = 1.0

    # Morse potential: V(x) = 0.5*(1-exp(-ww*x))^2

    ff = (t, x) -> [x[2], -(1-exp(-ww*x[1]))*ww*exp(-ww*x[1])]

    dt = 0.1
    tv = range(0.0, 20.0, step = dt)

    N = length(tv)

    xv = [zeros(2) for tt in tv]
    ref = [zeros(2) for tt in tv]

    data = zeros(N, 3)

    # initial condition
    xv[1] = [0.0, 1.0]
    
    data[1, :] = [tv[1], xv[1]...]
    
    for i1 in 2:N

        tt = tv[i1-1]
        xx = xv[i1-1]
    
        # RK4 on the fly
        f1 = ff(tt, xx)
        f2 = ff(tt+dt/2, xx+dt*f1/2)
        f3 = ff(tt+dt/2, xx+dt*f2/2)
        f4 = ff(tt+dt, xx+dt*f3)

        xv[i1] = xx + dt * (f1+2*f2+2*f3+f4)/6

        data[i1, 1:3]= [tv[i1], xv[i1]...]
        
    end
        
    p1 = plot(
        tv, data[:, 2],
        label="numerical", alpha = 0.4, color=:red,
        xlabel="t", ylabel="x(t)"
    )
    
    # ref. result
    plot!(tv, t -> data[1,2] * cos(ww * t) + data[1,3]/ww * sin(ww*t), 
        label = "SHO", color="gray", line=(dash=:dash))
    
    # p2 = plot(data[:, 2], data[:,3])
        
end
    

    
    
Out[4]:
No description has been provided for this image