1 year ago

#347239

test-img

inkok

Problem with ode system, using solve_ivp. ValueError: setting an array element with a sequence

I'm trying to solve ODE system. It is simulation of vehicle movement which includes P regulator.

At this point I don't know what is exactly wrong. Error occurs: ValueError: setting an array element with a sequence. I've tried to do it with ode.integrate but the results is the same. Also I don't exactly know how to indicate some dependencies like position- velocity or di/dt - i where i is electric current, it's probably the reason.

Some code explanation: velocity is regulated by torque(T_m), which is regulated by electric current (i), which is regulated by voltage (U_m), voltage is equal to difference between current velocity and velocity from V(t) function. V(t) function is giving back interpolated velocity from CSV file.

import numpy as np
import matplotlib.pyplot as plt
import csv
from scipy.interpolate import interp1d
from scipy.integrate import solve_ivp


def F_t(y,p):
    if y[1]>0.1:
         return p[3]*(1+p[4]*y[1])
    else:
        return 0
    
def T_m(y,p):
    return y[3]*p[11]

def F_n(y,p):
    return T_m(y,p)*p[7]*p[13]/p[6] - F_t(y,p)
    
def U_m(t,y):
    return (V(t) - y[1])*p[10]

def carx(t, y, p):
    s, v, s_c, i = y
    p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16 = p # there is a lot of p's because later there will be more equations
    ds = v
    dv = F_n(y,p)/(p5*p3)
    ds_c = V(t)            # theoretical distance
    di = (U_m(y,p) - i*p8 - v*p12*p7/p6)/p9
    return np.array([ds, dv, ds_c, di])

y0 = np.array([0, 0, 0, 0])
t_span = np.linspace(0, 1801, 18000)
sol = solve_ivp(lambda t, y: carx(t, y, p), [t_span[0], t_span[-1]], y0, t_eval=t_span) 

There is more code, but i don't think it's necessary to show importing CSV file. I hope everything is clear. I did working code in Julia, so i can show if someone ask.

python

math

scipy

ode

differential-equations

0 Answers

Your Answer

Accepted video resources