Python迭代问题

2024-03-28 16:22:43 发布

您现在位置:Python中文网/ 问答频道 /正文

ai = 1.0

# potentiall edit the 0.7 as I believe this is speciic to the De Sitter Universe
phi0 = (mp * (1/(H_0_true**(2) * 0.7))**(1/alpha))

for i in del_t_range:
    #friedmann equation which is updated every iteration with new values of scale factor
    H = (H_0_true * m.sqrt(((w_m/(ai**3)) + (w_r/(ai**4)) + (w_v) + (w_c/(ai**2)))))

    #H values needed for analysis purposes
    H_vals.append(H)
    time.append(i)

    #finite difference differentiation method for universe expansion
    del_a = ai * H * del_t
    a_val = ai + del_a
    a.append(a_val)
    ai = a_val

    ############################# ISSUE ###########################

    #field potential calculation 
    V = (phi0 / mp)**(-alpha)

    #appended to use for graphing
    V_vals.append(V)

    #print(V)

    #differentiation of the potential wrt the field
    V_dash = alpha * (phi0 / mp)**(-alpha - 1.0)

    #print(V_dash)
    #finite differnce of time derivative of phi
    y_val = yi - (3 * H * yi - V_dash) * del_t

    #print(y_val)
    # needed for graphing
    y_list.append(y_val)

    # used for updating values of phi
    phi_val = phi0 + (yi * del_t)
    phi_list.append(phi_val)

    # Energy Density calculations
    rho_phi = 0.5 * (y_val / mp)**(2) + (V / mp**(4)) * (1 / tp)**(2)

    #calculation required to sort out units
    rho_phi_true = rho_phi / mp**(2)

    #Used for graphing
    rho_phi_vals.append(rho_phi_true)

    # updates values for next iteration of loop
    phi0 = phi_val
    yi = y_val

下面的代码是循环的,因此我可以获得变量phi0、rho_phi、yi、V和V_dash的更新值。然后将每个更新的值附加到python列表并绘制。然而,我似乎在更新phi0的值时遇到了问题,尽管我已经编写了phi0的新值等于循环末尾的phièval。每次迭代的phi0值保持不变。例如

[1.0011701031373587e+30, 1.0011701031373587e+30, 1.0011701031373587e+30, 1.0011701031373587e+30, ...]

我很抱歉如果我一直含糊不清或困惑,请让我知道如果有任何问题。非常感谢!:-)


Tags: ofthealphatrueforvalmpai