你好我想知道为什么当我更改特定函数中的初始半径量时,我得到一个LinAlgError:奇异矩阵。
以下是工作代码的概要:
import numpy as np
from numpy import *
from matplotlib import pyplot as plt
from scipy.interpolate import interp1d
tnew = np.linspace(.28, 13900, 1000)
x = np.linspace(.016, 4.52, 1000)
ri = np.arange(1.08E-3, 3.4E-3, 2.5E-4)
rho_b = 1/(6*pi*G*tnew**2) #background density
ti = .28 #initial time in Myr
ri1 = 8.3E-4 #initial radius of first shell
G = 4.52E-21 #Msun Mpc^-3 Myr^-2
di1 = -4.63E-3 #initial density contrast of first shell
t1 = (3*ti)/(4*abs(di1)**(1.5))*(sinh(x)-x) #time equation
rho_i1 = (di1+1)*rho_b[0] #initial density of first shell
M1 = (4./3)*pi*rho_i1*ri1**3 #initial mass of first shell
f = interp1d(t1,x, bounds_error=False, kind='cubic')
new_theta = f(tnew)
Rv1 = (ri1/(2.*abs(di1)))*(cosh(new_theta)-1.) #final, evolved radius of first shell
for j in range(0, len(ri)):
#set up initial density equations
Mj = (4./3)*pi*rho_b[0]*(ri[j]**3 - ri1**3)
vtot = (4./3)*pi*ri[j]**3
rho_ij = (M1+Mj)/vtot
#density contrast and time
dij = (rho_ij/rho_b[0])-1
tj= (3*ti)/(4*abs(dij)**(1.5))*(sinh(x)-x)
#interpolation
fj = interp1d(tj,x, bounds_error=False, kind='cubic')
new_thetaj = fj(tnew)
Rvj = (ri[j]/(2.*abs(dij)))*(cosh(new_thetaj)-1.)
我按预期获得最终半径(Rvj)并且一切都很好,但是一旦我将初始半径列表(ri)更改为:
ri = np.linspace(1.08E-3, 3.4E-3, 50)
我收到奇异矩阵错误。我理解这意味着什么,但我不确定它为什么会出现以及如何解决它!
任何帮助将不胜感激!
答案 0 :(得分:0)
不确定你是否仍然坚持这个错误,但我注意到如果你交换tj,x参数提供给interp1d,它不再抛出奇异矩阵错误。函数interp1d期望按顺序传递x和y参数,所以我假设参数应该如图所示传递,看你已经用x来定义tj:
fj = interp1d(x,tj, bounds_error=False, kind='cubic')
然而,这确实会为以下行抛出溢出警告,但它至少允许脚本一直运行到最后。