I am trying to solve a system of differential equations using ode solver in scipy.integrate. My problem is, I am getting 'singular matrix' error when my matrix is not supposed to be singular. The main issue is when I am trying to find the inverse of the matrix B in my code below. In the code below B is 3x3 matrix, A is 3x1 matrix and U is 3x1 matrix as well! How can I remedy this problem?
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
import math
import parameter_projection as pp
from scipy.integrate import ode
import sympy as sm
c=10
k,k1,k2,k3=10,9,8,7
eta=2.5
gamma,gamma1,gamma2=2,3,10
a=[]
for i in range(30):
a.append(i)
a=np.array(a)
def aero(t,Y):
V,alpha,beta,p,q,r,phi,theta=Y[0],Y[1],Y[2],Y[3],Y[4],Y[5],Y[6],Y[7]
sg=np.cos(alpha)*np.cos(beta)*np.sin(theta)-np.sin(beta)*np.sin(phi)*np.cos(theta)-np.sin(alpha)*np.cos(beta)*np.cos(phi)*np.cos(theta)
smcg=np.cos(alpha)*np.sin(beta)*np.sin(theta)+np.cos(beta)*np.sin(phi)*np.cos(theta)-np.sin(alpha)*np.sin(beta)*np.cos(phi)*np.cos(theta)
cmcg=np.sin(theta)*np.sin(alpha)+np.cos(alpha)*np.cos(phi)*np.cos(theta)
#error
ev=V-np.sin(t)
ebeta=beta-np.sin(t)
etheta=theta-np.sin(t)
ethetad=q*np.cos(phi)-r*np.sin(phi)-np.cos(t)
sv,sbeta,stheta=ev,ebeta,etheta+ethetad
s=np.array([[sv],[sbeta],[stheta]])
A=np.array([[-a[1]*V**2*np.sin(beta)-a[2]*V**2*np.sin(beta)-a[4]*np.sin(gamma)-np.cos(t)],[p*np.sin(alpha)-r*np.cos(alpha)+a[16]*V+a[15]*smcg-np.cos(t)],[ethetad+np.cos(phi)*a[10]*p*r+np.cos(phi)*a[6]*(r**2-p**2)+np.cos(phi)*a[20]*V**2-np.cos(phi)*a[21]*sg+-q*np.sin(phi)*p-q*np.sin(phi)*q*np.sin(phi)*np.tan(theta)-q*np.sin(phi)*r*np.cos(phi)*np.tan(theta)-np.sin(phi)*a[11]*p*q+a[12]*q*r-a[13]*V**2+r*np.cos(phi)*p+r*q*np.cos(phi)*np.sin(phi)*np.tan(theta)+(r*np.cos(phi))**2*np.tan(theta)-np.cos(t)]])
B=np.array([[a[0]*np.cos(alpha)*np.sin(beta),a[7]*np.sin(beta),a[0]*np.sin(alpha)*np.cos(beta)],[-a[9]*np.cos(alpha)*np.sin(beta)/V,a[22]*np.cos(beta)/V,-a[9]*np.sin(alpha)*np.sin(beta)/V],[a[29]*np.cos(phi),a[26]*np.sin(alpha)*np.sin(beta)*np.cos(phi)-a[27]*np.sin(phi)*np.cos(alpha),-a[25]*np.cos(phi)]])
C=np.linalg.inv(B)*A
U=(C-np.linalg.inv(B)*k*s-np.linalg.inv(B)*eta*np.tanh(s))
Vdot=a[0]*np.cos(alpha)*np.sin(beta)*U[0]-a[1]*V**2*np.cos(beta)-a[2]*V**2*np.sin(beta)-a[3]*sg+a[7]*np.sin(beta)*U[1]+a[0]*np.sin(alpha)*np.cos(beta)*U[2]
alphadot=q-(p*np.cos(alpha)+r*np.sin(alpha))*np.sin(beta)/np.cos(beta)+a[4]*V-a[14]*cmcg-a[8]*np.sin(alpha)*U[0]/V+a[8]*np.cos(alpha)*U[2]/V
betadot=p*np.sin(alpha)-r*np.cos(alpha)+a[16]*V+a[17]*smcg-a[9]*np.cos(alpha)*np.sin(beta)*U[0]/V+a[22]*np.cos(beta)*U[1]/V-a[9]*np.sin(alpha)*np.sin(beta)*U[2]/V
pdot=a[5]*q*r+a[17]*p*q+a[18]*V**2-a[19]*smcg+a[23]*U[0]-a[28]*U[2]+a[24]*np.sin(alpha)*np.cos(beta)*U[1]
qdot=a[10]*p*r+a[6]*(r**2-p**2)+a[20]*V**2-a[21]*sg+a[29]*U[0]-a[25]*U[2]+a[26]*np.sin(alpha)*np.sin(beta)*U[1]
rdot=a[11]*p*q-a[12]*q*r+a[13]*V**2+a[27]*np.cos(alpha)*U[2]
phidot=p+q*np.sin(phi)*np.tan(theta)+r*np.cos(phi)*np.tan(theta)
thetadot=q*np.cos(phi)-r*np.sin(phi)
return[Vdot,alphadot,betadot,pdot,qdot,rdot,phidot,thetadot]
y0=[0.01,0.2,0,0,0,0,0,0.1]
t0=0
V=[]
alpha=[]
beta=[]
p=[]
q=[]
r=[]
phi=[]
theta=[]
t=[]
r=ode(aero).set_integrator('dopri5',method='bdf',nsteps=1e10)
r.set_initial_value(y0,t0)
t1=10
dt=.01
while r.successful() and r.t<t1:
r.integrate(r.t+dt)
V.append(r.y[0])
alpha.append(r.y[1])
beta.append(r.y[2])
p.append(r.y[3])
q.append(r.y[4])
r.append(r.y[5])
phi.append(r.y[6])
theta.append(r.y[7])
t.append(r.t)
V=np.array(V)
alpha=np.array(alpha)
beta=np.array(beta)
p=np.array(p)
q=np.array(q)
r=np.array(r)
phi=np.array(phi)
theta=np.array(theta)
plt.plot(t,V)
plt.show()
For your initial values with beta=phi=0
and additionally a[0]=0
from setting a[k]=k
¸ your matrix B
has a first line of zeros in the first step. Which makes a singular matrix.
Changing the test data to a[i]=i+1
removes this error but gives immediately a new error
rv_cb_arr is NULL
Call-back cb_fcn_in___user__routines failed.
Traceback (most recent call last):
File "odeint-LA-singular_so45165407.py", line 60, in <module>
r.integrate(r.t+dt)
which is probably because all your dot
variables are still arrays, so the returned derivatives vector is a list of np.array
of size 3 each.
The first speculation here is that you mistakenly believe that *
is the matrix product. This only applies if you construct the objects as np.matrix
. With np.array
use the dot
for matrix-vector products and np.linalg.solve
instead of the product with the inverse.
And indeed that is the case, use the following lines instead
C = np.linalg.solve(B,A)
U = C-np.linalg.solve(B,k*s - eta*np.tanh(s))
After that you find that you use the variable name r
both for the component array and for the ODE solver. Repairing that you still get that the step size becomes too small.
(add jul/19) Using as per comments the variant
U = -C-np.linalg.solve(B,k*s - eta*np.tanh(s))
I changed the integration loop to
t0=0.
y0=[0.01,0.2,0.,0.,0.,0.,0.,0.1]
names = [ "V", "alpha", "beta", "p", "q", "r", "phi", "theta" ]
sol = [ [yy] for yy in y0 ]
t=[t0]
solver=ode(aero).set_integrator('dopri5',nsteps=1e3)
solver.set_initial_value(y0,t0)
t1=3
dt=5e-3
for t1 in range(1,11):
if not solver.successful(): break;
while solver.successful() and solver.t<t1:
solver.integrate(solver.t+dt)
for k in range(8): sol[k].append(solver.y[k]);
t.append(solver.t)
print "step"
print len(t)
for k in range(8):
plt.subplot(4,2,k+1)
plt.plot(t,sol[k])
plt.ylabel(names[k])
plt.show()
where the automated treatment of the solution components makes for better code and a plot is produces whenever t
passes an integer value. The final graph is
with the singularity in alpha
occurring at t=3.292894674
with widely unstable values in alphadot
varying from -1.2e06
to +2.9e06
with variations in the size of 1e-09
in time.