Implementation of a differential equation (Shooting method) in Python by varying the initial and eigenvalues

Asked 2 years ago, Updated 2 years ago, 24 views

As the title suggests, it takes a lot of time to find a combination of \vec{z}'=f two-story differential equation (which is in the form of a differential equation of a first-order vector) that changes the initial value X0 and the parameter epsilon.Actually, I think it would be the fastest if I could do meshgrid, but it didn't work.

Please let me know if there is a way to avoid looping as much as possible and do the same as the code below.

def sol(q_x, kappa, h, x0, epsilon):

    y = Symbol('y')

    defsol(x,y,epsilon):
        return f(x,y,q_x,kappa,h,epsilon)

    y = np.linspace (0.0, 1000, 10001)

    return order (fsol, x0, y, args=(epsilon,))

alpha=np.linspace(0,2*np.pi,101)
X = np.cos (alpha)
Y = np.sin(alpha)
ZEROS = [[0.0]*2 for i in range (len(alpha))]
X0 = np.c_ [X, Y, ZEROS ]

def disposition0(q_x, kappa, h, cutoff):

    defz(x0, epsilon):
        return sol(q_x, kappa, h, x0, epsilon)

    epsilon=np.linspace(0,q_x**2+kappa+h,101)

    Phi1 = np.empty ([alpha.shape[0], epsilon.shape[0]])
    Phi2 = np.empty ([alpha.shape[0], epsilon.shape[0]])
    for i in range(0,len(alpha)):
        for jin range(0,len(epsilon)):
            Phi1[i][j]=z(X0[i], epsilon[j])[1000][0]
            Phi2[i][j]=z(X0[i], epsilon[j])[1000][1]
    energy = 0
    forkin range(0,len(epsilon)):
        ifabs(Phi1[:,k])<cutoff&abs(Phi2[:,k])<cutoff:
            energy=epsilon(k)
            break            

    return energy

python

2022-09-30 19:35

1 Answers

If you look at the code in the question, sol is a routine that solves the normal differential equation, but we call that function 2 million times from dispersion0. Even if it takes 1ms to calculate sol, it takes 33 minutes.

The solution is calculated by dispersion0, but the only way to do this is to reduce it.There is a way to use repeated numerical analysis solutions to remove the ones that are unlikely to be solved by cutting branches in search trees.

Also, fsol is called so often that it should be accelerated using Cython instead of using slow processing like SimPy.


2022-09-30 19:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.