为什么说我有 4 个位置参数?

问题描述 投票:0回答:1

这是我的代码,当我运行它时,我收到一条错误消息,显示“TypeError:integrand() 需要 3 个位置参数,但给出了 4 个”,但我的 integrand() 有 3 个位置参数,所以我不明白为什么它说我有 4 个位置参数。

import numpy as np
from scipy.integrate import quad
import matplotlib.pyplot as plt

# Constants
H0 = 70.0  # Hubble constant in km/s/Mpc
Omega_r0 = 0.0  # Radiation density parameter (ignored)
t0 = 1 / H0  # Approximation for the age of the universe in billions of years 

# Define Omega_M0 and Omega_Lambda0 pairs
Omega_m0_values = 0.31
Omega_lambda0_values = [-0.31, 0.69, 1.7289, 1.728]

# Function to integrate
def integrand(a, Omega_m0, Omega_lambda0):
    return 1.0 / np.sqrt((Omega_m0 / a) + (Omega_lambda0 * a**2) + (1-(Omega_m0-Omega_lambda0)))
                         
# Functiion to solve Friedmann Equation
def solve_friedmann(a, Omega_m0, Omega_lambda0):
    result, _ = quad(integrand, 0, a, args=(a, Omega_m0, Omega_lambda0))
    return result / H0

#Generate a range of scale factor
a_values = np.linspace(0.1, 1.9, 100)
# Calculate corresponding time (t) values
t_values = [solve_friedmann(a, Omega_m0_values, Omega_lambda0_values) for a in a_values]    
# Calculate H0(t - t0)
x_values = H0 * (np.array(t_values) - t0)

# Plotting
plt.figure(figsize=(6, 6))
for Omega_m0 in Omega_m0_values:
    for Omega_lambda0 in Omega_lambda0_values:
            x_values, a_values = solve_friedmann(Omega_m0, Omega_lambda0)
plt.plot(x_values, a_values, label=f"Ω_M0={Omega_m0}, Ω_Λ0={Omega_lambda0}")
plt.xlabel('H_0(t-t_0)')
plt.ylabel('a')
plt.title('Scale Factor Evolution for Different Ω_M0 and Ω_Λ0 pairs')
plt.legend()
plt.grid(True)                                                     
plt.show()

我从被积函数()中删除了一个参数,但仍然给出相同的错误消息,所以我不知道出了什么问题。

python-3.x
1个回答
0
投票

这来自于如何将参数传递给四元函数中的被积函数。除了积分变量之外,

arg
应该只包含被积函数所需的额外参数,在您的情况下是 Omega_m0 和 Omega_lambda0。另外,平方根的处理方式也存在问题。以下是有关如何执行此操作的建议:

import numpy as np
from scipy.integrate import quad
import matplotlib.pyplot as plt

H0 = 70.0  
t0 = 1 / H0  

Omega_m0_values = [0.31]  
Omega_lambda0_values = [-0.31, 0.69, 1.7289, 1.728]

def integrand(a, Omega_m0, Omega_lambda0):
    expr = (Omega_m0 / a) + (Omega_lambda0 * a**2) + (1 - (Omega_m0 - Omega_lambda0))
    return 1.0 / np.sqrt(expr) if expr > 0 else 0

def solve_friedmann(a, Omega_m0, Omega_lambda0):
    result, _ = quad(integrand, 1e-8, a, args=(Omega_m0, Omega_lambda0), limit=100)
    return result / H0

plt.figure(figsize=(6, 6))
a_values = np.linspace(0.1, 1.9, 100)
for Omega_m0 in Omega_m0_values:
    for Omega_lambda0 in Omega_lambda0_values:
        t_values = [solve_friedmann(a, Omega_m0, Omega_lambda0) for a in a_values]
        x_values = H0 * (np.array(t_values) - t0)
        plt.plot(x_values, a_values, label=f'Ω_M0={Omega_m0}, Ω_Λ0={Omega_lambda0}')

plt.xlabel('H_0(t-t_0)')
plt.ylabel('a')
plt.title('Scale Factor Evolution for Different Ω_M0 and Ω_Λ0 pairs')
plt.legend()
plt.grid(True)
plt.show()

这给出了

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.