im trying to solve ODE with python 'solve_ivp' m(x''- u'') + k * (x - u) = 0 where u'' - sine function depended on time, m and k - state scalars Here is my code:
m = 100
k = 0.35
def dSdx(x, S, d2u_dt2, u):
x, v = S
return [v,
d2u_dt2(Amp1, T1, t1) - k*x/m + k*u(Amp1, T1, v0, d0, t1)/m]
v_0 = 0
x_0 = 0
S_0 = (x_0, v_0)
sol = solve_ivp(dSdx, y0=S_0, t_span=t, args=(d2u_dt2, u))
print(sol)
d2u_dt2(Amp1, T1, t1) and u(Amp1, T1, v0, d0, t1) return array
Im getting error:
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.
First part of code:
import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate
import sympy as sp
from scipy.integrate import odeint
from scipy.integrate import ode
from scipy.integrate import quad
from scipy.integrate import solve_ivp
Amp1 = 80
Amp2 = -70
T1 = 5
T2 = 6
N = 50
v0=0
d0=0
t = np.linspace(0, T1+T2, N)
ax1 = plt.subplot(212)
ax2 = plt.subplot(221)
ax3 = plt.subplot(222)
def d2u_dt2(Amp, T, t):
return Amp*np.sin((np.pi*t)/(T))
def du_dt(Amp, T, v0, t):
return -(Amp*T/np.pi)*np.cos(np.pi*t/T) + (v0+(Amp*T/np.pi))
def u(Amp, T, v0, d0, t):
return -Amp*((T/np.pi)**2)*np.sin(np.pi*t/T) + (v0 + (Amp*T/np.pi))*t + d0
t1 = np.linspace(0, 5, 50)
t2 = np.linspace(0, 6, 50)
t3 = np.linspace(5,11, 50)
ax1.plot(t1, d2u_dt2(Amp1, T1, t1), t3, d2u_dt2(Amp2, T2, t2))
ax2.plot(t1, du_dt(Amp1, T1, v0, t1), t3, du_dt(Amp2, T2, du_dt(Amp1, T1, v0, T1), t2))
ax3.plot(t1, u(Amp1, T1, v0, d0, t1), t3, u(Amp2, T2, du_dt(Amp1, T1, v0, T1), u(Amp1, T1, v0, d0, T1), t2))
plt.show()