[Python] Solve_Bvp with acceleration in scipy

Discussão em 'Python' iniciado por Stack, Junho 8, 2025.

  1. Stack

    Stack Membro Participativo

    i have this code, that should calculate the speed and distance with given acceleration. The boundrary condition says that the speed in the beginning or and in the end has to be zero. but i cant get it to work code:

    from scipy.integrate import solve_bvp, cumulative_trapezoid
    from scipy.interpolate import interp1d,UnivariateSpline

    import numpy as np
    import pandas as pd

    data = pd.read_csv("Linear Accelerometer.csv", header=None, skiprows=1)
    t_raw = data.iloc[:, 0].astype(str).str.replace(',', '.').astype(float).values
    a_raw = data.iloc[:, 2].astype(str).str.replace(',', '.').astype(float).values

    n_points = 1000
    t_coarse = np.linspace(t_raw[0], t_raw[-1], n_points)
    a_coarse = np.interp(t_coarse, t_raw, a_raw)


    a_spline = UnivariateSpline(t_coarse, a_coarse, s=0.1) # s > 0 glättet
    a_func = a_spline

    v_guess = cumulative_trapezoid(a_func(t_coarse), t_coarse, initial=0)
    s_guess = cumulative_trapezoid(v_guess, t_coarse, initial=0)"""
    y_initode = np.zeros((2, t_coarse.size))
    DGL-System
    def fun(t, y):
    return np.array([y[1], a_func(t)])

    Randbedingungen v(0)=0, v(end)=0
    def bc(ya, yb):
    return np.array([ya[1] , yb[1] ])

    Solver aufrufen
    sol = solve_bvp(fun, bc, t_coarse, y_initode, max_nodes=100000, verbose=2)

    Prüfen
    print(sol.message)

    Ergebnis extrahieren
    s = sol.sol(t_coarse)[0]
    v = sol.sol(t_coarse)[1]

    import matplotlib.pyplot as plt

    plt.figure(figsize=(12, 6))

    plt.subplot(2, 1, 1)
    plt.plot(t_coarse, s, label="s(t) (Strecke)")
    plt.ylabel("Strecke [m]")
    plt.legend()
    plt.grid()

    plt.subplot(2, 1, 2)
    plt.plot(t_coarse, v, label="v(t) (Geschwindigkeit)", color="orange")
    plt.xlabel("Zeit ")
    plt.ylabel("Geschwindigkeit [m/s]")
    plt.legend()
    plt.grid()

    plt.tight_layout()
    plt.show()


    like this it gives me the error: A singular Jacobian encountered when solving the collocation system.

    I expected the speed to be 0 in the beginning and in the end but it doesnt work as explained. But when i say the distance has to be zero at beginning and the end it works and looks like this:Plot for d(0) = d(T) = 0

    Continue reading...

Compartilhe esta Página