为什么说“ ValueError:函数返回的数组在两次调用之间更改了大小”?

问题描述

我正在尝试计算积分,但我不理解错误输出,它是什么意思,有什么问题以及如何解决? 输出为“ ValueError:函数返回的数组在调用之间更改了大小”,我添加了完整的输出。 请帮助, 谢谢

import numpy as np
import matplotlib.pyplot as plt
import astropy.cosmology as cosmo
from astropy import units as u
from astropy import constants as const
import scipy.integrate as integrate
from scipy.optimize import fsolve
def delta_t(m_lens,z_lens,y_impact):
    return 4*const.G*const.c**-3*m_lens*const.M_sun*(1+z_lens)*(0.5*y_impact*np.sqrt(y_impact**2+4)+np.log((np.sqrt(y_impact**2+4)+y_impact)/(np.sqrt(y_impact**2+4)-y_impact)))
def y_max(r_max):
    return np.sqrt((1+r_max)/r_max**0.5-2)
def y_min(y,r_max,m_lens,z_source,dt_min):                         # implicit function of y_min from eq (2) with delta_t_min=1ms
    y = np.linspace(0,100)
    return -dt_min*u.s+4*const.G*const.c**-3*m_lens*const.M_sun*(1+z_lens)*(0.5*y*np.sqrt(y**2+4)+np.log((np.sqrt(y**2+4)+y)/(np.sqrt(y**2+4)-y)))
def Y_min(r_max,dt_min):
    return fsolve(lambda y: delta_t(m_lens,y) - dt_min*u.s,-10)[0]
print(Y_min(5,30,0.5,1,10**-3))
def cosmo_dependent_part(z_lens,f_dm):
    return 3/2*f_dm*0.24/const.c*((cosmo.WMAP9.H0*1000*u.meter/u.kilometer)**2*cosmo.WMAP9.angular_diameter_distance_z1z2(0,z_lens)*cosmo.WMAP9.angular_diameter_distance_z1z2(z_lens,z_source))/(cosmo.WMAP9.H(z_lens)*1000*u.meter/u.kilometer*cosmo.WMAP9.angular_diameter_distance_z1z2(0,z_source))
print(cosmo_dependent_part(0.5,1))
def optical_depth_fixed_source_integrand(z_source,f_dm,dt_min):
    z_lens = np.linspace(0,100)
    return cosmo_dependent_part(z_lens,f_dm)*(1+z_lens)**2*(y_max(r_max)**2-Y_min(r_max,dt_min)**2)
def optical_depth_fixed_source(z_source,100)
    return integrate.trapz(optical_depth_fixed_source_integrand(z_source,dt_min),z_lens)
print(optical_depth_fixed_source(1,5,10**-3))

这是输出

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-199-5b86909da8d5> in <module>
      5     z_lens = np.linspace(0,100)
      6     return integrate.trapz(optical_depth_fixed_source_integrand(z_source,z_lens)
----> 7 print(optical_depth_fixed_source(1,10**-3))             # z_source=1

<ipython-input-199-5b86909da8d5> in optical_depth_fixed_source(z_source,dt_min)
      4 def optical_depth_fixed_source(z_source,dt_min):
      5     z_lens = np.linspace(0,100)
----> 6     return integrate.trapz(optical_depth_fixed_source_integrand(z_source,z_lens)
      7 print(optical_depth_fixed_source(1,10**-3))             # z_source=1

<ipython-input-199-5b86909da8d5> in optical_depth_fixed_source_integrand(z_source,dt_min)
      1 def optical_depth_fixed_source_integrand(z_source,dt_min):
      2     z_lens = np.linspace(0,100)
----> 3     return cosmo_dependent_part(z_lens,dt_min)**2)
      4 def optical_depth_fixed_source(z_source,100)

<ipython-input-197-ef9a0e8a24a2> in Y_min(r_max,dt_min)
     16 
     17 def Y_min(r_max,dt_min):
---> 18     return fsolve(lambda y: delta_t(m_lens,-10)[0]
     19 print(Y_min(5,10**-3))

~\anaconda3\lib\site-packages\scipy\optimize\minpack.py in fsolve(func,x0,args,fprime,full_output,col_deriv,xtol,maxfev,band,epsfcn,factor,diag)
    145                'diag': diag}
    146 
--> 147     res = _root_hybr(func,jac=fprime,**options)
    148     if full_output:
    149         x = res['x']

~\anaconda3\lib\site-packages\scipy\optimize\minpack.py in _root_hybr(func,jac,eps,diag,**unknown_options)
    223             maxfev = 200 * (n + 1)
    224         retval = _minpack._hybrd(func,--> 225                                  ml,mu,diag)
    226     else:
    227         _check_func('fsolve','fprime',Dfun,n,(n,n))

ValueError: The array returned by a function changed size between calls

解决方法

我更改了

的变量

optical_depth_fixed_source_integrand

以使它们包含函数y_min和y_max而不是其vaviabes,现在它可以正常工作

def optical_depth_fixed_source_integrand(z_source,z_lens,f_dm,y_min,y_max):
    z_lens = np.linspace(0,z_source,100)
    return cosmo_dependent_part(z_lens,f_dm)*(1+z_lens)**2*(y_max**2-y_min**2)

def optical_depth_fixed_source(z_source,100)
    optical_depth_fixed_source = integrate.trapz(optical_depth_fixed_source_integrand(z_source,y_max),z_lens)
    return optical_depth_fixed_source
print(optical_depth_fixed_source(1,0.5,1,y_min(5,30,1e-3),y_max(5))) 

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...