To fix the issue you can do The return value of odeint is the solution to z(t) which you have defined to be z = [x,y,x',y']. Therefore the second derivative is not a part of the solution returned by odeint. You can approximate the second derivative of x and y by taking finite differences of the returned values of the first derivatives. For example: import numpy as np
from scipy.integrate import odeint
from numpy import sin, cos, pi, array
import matplotlib.pyplot as plt
init = array([0,pi/18,0,0])
def deriv(z, t):
x, y, dxdt, dydt = z
dx2dt2=(4+x)*(dydt)**25*x+9.81*cos(y)
dy2dt2=(9.81*sin(y)2*(dxdt)*(dydt))/(0.4+x)
return np.array([dxdt, dydt, dx2dt2, dy2dt2])
time = np.linspace(0.0,10.0,1000)
sol = odeint(deriv,init,time)
x, y, xp, yp = sol.T
# compute the approximate second order derivative by computing the finite
# difference between values of the first derivatives
xpp = np.diff(xp)/np.diff(time)
ypp = np.diff(yp)/np.diff(time)
# the second order derivatives are now calculated at the midpoints of the
# initial time array, so we need to compute the midpoints to plot it
xpp_time = (time[1:] + time[:1])/2
plt.xlabel("time")
plt.ylabel("y")
plt.plot(time, x, label='x')
plt.plot(time, y, label='y')
plt.plot(time, xp, label="x'")
plt.plot(time, yp, label="y'")
plt.plot(xpp_time, xpp, label="x''")
plt.plot(xpp_time, ypp, label="y''")
plt.legend()
plt.show()
plt.plot(time, deriv(sol.T,time)[2], label="x''")
plt.plot(time, deriv(sol.T,time)[3], label="y''")
Boards Message : 
You Must Login
Or Sign Up
to Add Your Comments . 
Share :

Python complex coupled ODEs error
Tag : python , By : xie renhui
Date : March 29 2020, 07:55 AM
I hope this helps you . At the moment, I am trying to solve a system of coupled ODEs with complex terms. I am using scipy.integrate.ODE, I have successfully solved a previous problem involving a coupled ODE system with only real terms. In that case I used odeint, which is not suitable for the problem I am facing now. The system has 9 coupled ODEs, and this is my code: , This should solve this issue: def resol(t, y, par):
p1111, p1212, p2121, p1112, p1121, p1122, p1221, p1222, p2122 = y
gamma, h, H = par

Solving two coupled ODEs by matrix form in Python
Tag : python , By : Mihai Mocanu
Date : March 29 2020, 07:55 AM
like below fixes the issue The initial value to odeint must be an array, not a matrix. Try use y0=np.hstack((y_init, m_init)) and put that as the initial value (y0 is the second argument to odeint).

python two coupled second order ODEs Runge Kutta 4th order
Tag : python , By : Marcos de Carvalho
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Cutting away unused and contradicting variables and statements a running reduction and correction of your code is import numpy as np
from math import sqrt
import matplotlib.pyplot as plt
# Parameters
mr = 2
m = 1
c = 5
k = 36
F = 3
# Equations:
def V(u,t):
x1, x2, v1, v2 = u
dx = x2x1;
dv = v2v1;
return np.array([ v1, v2, (c*dv+k*dx)/mr, (F+c*dv+k*dx)/m ])
def rk4(f, u0, t0, tf , n):
t = np.linspace(t0, tf, n+1)
u = np.array((n+1)*[u0])
h = t[1]t[0]
for i in range(n):
k1 = h * f(u[i], t[i])
k2 = h * f(u[i] + 0.5 * k1, t[i] + 0.5*h)
k3 = h * f(u[i] + 0.5 * k2, t[i] + 0.5*h)
k4 = h * f(u[i] + k3, t[i] + 0.5*h)
u[i+1] = u[i] + (k1 + 2*(k2 + k3) + k4) / 6
return u, t
u, t = rk4(V, np.array([0., 0., 1., 1.]) , 0. , 1. , 10)
x1, x2, v1, v2 = u.T
plt.plot(t, x1, t, x2)
plt.grid('on')
plt.show()

How do I convert the x and y values in polar form from these coupled ODEs to to cartesian form and graph them?
Date : March 29 2020, 07:55 AM
this one helps. The first solution should give you the expected result, but there is a mistake in the implementation of the ode. The function you pass to odeint should return an array containing the solutions of a 1storder differential equations system. import numpy as np
from scipy.integrate import odeint
from numpy import sin, cos, pi, array
import matplotlib.pyplot as plt
def deriv(z, t):
x, y, dxdt, dydt = z
dx2dt2 = (0.415 + x) * (dydt)**2  50 / 1.006 * x + 9.81 * cos(y)
dy2dt2 = (9.81 * 1.006 * sin(y)  2 * (dxdt) * (dydt)) / (0.415 + x)
return np.array([dxdt, dydt, dx2dt2, dy2dt2])
init = array([0, pi / 18, 0, 0])
time = np.linspace(0.0, 10.0, 1000)
sol = odeint(deriv, init, time)
plt.plot(sol[:, 0], sol[:, 1], label='hi')
plt.show()
x = sol[:,0]
y = sol[:,1]
def plot(h):
x, y = h
n = (0.4 + x) * sin(y)
u = (0.4 + x) * cos(y)
return np.array([n, u])
n,u = plot( (x,y))

Using python builtin functions for coupled ODEs
Tag : python , By : ArdentRogue
Date : March 29 2020, 07:55 AM
To fix this issue You can wrap your existing functions into a function accepted by odeint (option tfirst=True) and solve_ivp as def odesys(t,u):
theta,omega = u[:n],u[n:]; # or = u.reshape(2,1);
return [ *f(omega), *g(theta,omega) ]; # or np.concatenate([f(omega), g(theta,omega)])
u0 = [*theta0, *omega0]
t = linspan(t0, tf, timesteps+1);
u = odeint(odesys, u0, t, tfirst=True);
#or
res = solve_ivp(odesys, [t0,tf], u0, t_eval=t)

