I am trying to solve a system of ODE's related to a coupled mass and spring system using solve_ivp. I am getting a problem stating that "ValueError: setting an array element with a sequence". See below for the code I am trying to use.
from scipy.integrate import solve_ivp
import numpy as np
func(t, w, p):
#unpack the variables and parameters"
x1, y1, x2, y2 = w
m1, m2, k1, k2, L1, L2, b1, b2 = p
# Create the functions
y1 = (-b1 * y1 - k1 * (x1 - L1) + k2 * (x2 - x1 - L2)) / m1,
y2 = (-b2 * y2 - k2 * (x2 - x1 - L2)) / m2
return y1, y2
# Parameter values
# Masses:
m1 = 1.0
m2 = 1.5
# Spring constants
k1 = 8.0
k2 = 40.0
# Natural lengths
L1 = 0.5
L2 = 1.0
# Friction coefficients
b1 = 0.8
b2 = 0.5
# Initial conditions
# x1 and x2 are the initial displacements; y1 and y2 are the initial velocities
x1 = 0.5
y1 = 0.0
x2 = 2.25
y2 = 0.0
# Pack up the parameters and initial conditions:
p = [m1, m2, k1, k2, L1, L2, b1, b2]
w0 = [x1, y1, x2, y2]
t_span = np.linspace(0, 15, 1000)
Sol = solve_ivp(lambda t, w: func(t, w, p), [t_span[0], t_span[-1]], y0=w0, t_eval=t_span)
I am not really sure where I am going wrong because the error doesn't specify which line of code the problem is with. Has anyone experienced this before?
If x
is the position and y
the velocity, then name z
the acceleration and return a vector of the derivatives of the inputs
def func(t, w, p):
#unpack the variables and parameters"
x1, y1, x2, y2 = w
m1, m2, k1, k2, L1, L2, b1, b2 = p
# Create the functions
z1 = (-b1 * y1 - k1 * (x1 - L1) + k2 * (x2 - x1 - L2)) / m1,
z2 = (-b2 * y2 - k2 * (x2 - x1 - L2)) / m2
return y1, z1, y2, z2
Everything else looks like it should work.