Я относительно новичок в Python, и я использовал его для решения системы уравнений в проекте по космологии.Вот код:
#Import Math Module for Square Root Function, Numpy for Computations and Matplot for Plotting
import math
import numpy as np
import matplotlib.pyplot as plt
#Planck Mass
Mpl=2.43*10**18*10**9*1.6*10**-19 #Mpl value is 2.43*10^18 GeV
#Compute Initial Values of the Densities
RhoM_today= 2.746*(10**(-27)) #Today's Value of Matter Density
RhoRad_today= 4.546*(10**(-31)) #Today's Value of Radiation Density
RhoL_today=0.0 #Value of Cosmological Constant Density
Rho=RhoM_today+RhoRad_today+RhoL_today #Initial Value of the Overall Density
#Set Initial Values
#Initial Values of the Fields
Sigma = 1.0 # Initial Value of Sigma (a=10)
Phi=1.0
#Values of Parameters
Omega=1.0 #Dummy Value of Omega
Alpha_Three=1.0 #Dummy Value of Alpha_3
Alpha_Four=1.0 #Dummy Value of Alpha_4
Alpha_Sigma=1.0 #Dummy Value of Alpha_Sigma
C1=1.0 #Dummy Value of C1
mg=1.0 #Dummy Value of the Graviton Mass
Lambda=0.0 #Value of the Cosmological Constant
H=2.27*10**-18 #Initial Value of H
# Initial Values for computing the number of Steps for RK4
x=0 #Corresponding to a = 1 (today)
xn=-11.5 #Corresponding to a ~ 10^-5
h = 0.115 #Step Size
n = int((x-xn)/h) #Number of Steps/Points
#Note: n+1 points but n Steps?
print(n)
#Creating Arrays for Storing Data
xp=np.linspace(x,xn,n+1) #Array for Storing x
Sigmap=np.empty(n+1,float) #Array for Storing Sigma
up=np.empty(n+1,float) #Array for Storing u
Hp=np.empty(n+1,float) #Array for Storing H
#Hxp=np.empty(n+1,float) #Array for Storing Hx
#Sigmap[0]=Sigma #Save Initial Value of Sigma
#up[0]=u #Save Initial Value of u
#Hp[0]=H #Save Initial Value of H
#Hp[0]=H/H0 #Save Initial Value of H
#Hxp[0]=Hx #Save Initial Value of Hx
#CHp=np.empty(n+1,float) #Array for Conformal Hubble Parameter
#CHp[0]=H*math.exp(x)
#for i in range (0,n+1):
# print (Hp[i])
#Print Parameters and Initial Values
print('Planck Mass is',Mpl)
print('Initial Value of H is ',H )
print('Initial Value of Sigma is ',Sigma)
print('Initial Value of Energy Density Rho is ',Rho)
print('a goes from ',math.exp(x),'to ',math.exp(xn))
print('x (lna) goes from ',x,'to ',xn)
print('Total Number of Steps in RK1 is ',n)
#The Header of the output table
print('x \t\t\t\t\t\t \t\tSigma\t\t\t\t\t\t u \t\t\t\t\t \t\t\t\t H')
#print(x,'\t\t\t\t\t\t\t',Sigma,'\t\t\t\t\t\t',u,'\t\t\t\t\t\t',H)
#print('%f \t\t\t\t\t \t %f \t\t\t\t \t %f \t\t\t\t\t \t %f'% (x,Sigma,u,H)) # The Initial Values of x, Sigma & u
#Start of RK1
#Start of The RK1 Loop
for i in range(0, n):
# Generate Values of Rho (from RhoM, RhoRad & H, re-written in terms of x)
RhoM = RhoM_today / (math.exp(x) ** 3)
RhoRad = RhoRad_today / (math.exp(x) ** 4)
RhoL = 0.0
Rho = RhoM + RhoRad + RhoL # New Value of the Overall Density
# Generate Values of the Pressures
Pressure_Matter = 0
Pressure_Radiation = RhoRad / 3
Pressure = Pressure_Matter + Pressure_Radiation
#Save the Values of Sigma and H
Sigmap[i] = Sigma # Save the Value of Sigma
Hp[i] = H #Save the Value of H
# Compute Values for X, J, Lambda_X & Their Derivatives
X = math.exp(Sigma) / math.exp(x)
print(X)
J = 3 + 3 * Alpha_Three * (1 - X) + Alpha_Four * (1 - X) ** 2
print(J)
Lambda_X = (mg ** 2) * (X - 1) * [J + (X - 1) * {(Alpha_Three) * (X - 1) - 3}]
# Compute Value of u
u = (1 / H) * (math.sqrt(2 / Omega)) * math.sqrt(3 * H * 2 - Lambda - Lambda_X - Rho / Mpl ** 2)
up[i]=u
print('%.3f \t\t\t\t\t \t %.6f \t\t\t\t \t%.6f \t\t\t\t\t \t %9.3e' % (x, Sigma, u, H)) # Newer Computed values of x,Sigma,u & H
# Compute Value of Lambda_X_Dot & Lambda_X_Dash
Lambda_X_Dot = 3 * (mg ** 2) * math.exp(Sigma) * H * [u - 1] * [J + X * {Alpha_Three * (X - 1) - 2}] / math.exp(x)
Lambda_X_Dash = Lambda_X_Dot / H
# Compute Value of q
q = (Alpha_Sigma ** 0.5) * [u / (X * mg)] / math.sqrt([(1 / H ** 2) - {C1 / (math.exp(x) ** 4 * X * (1 - X) * J * H)}])
# Compute Value of H_Dash
H_Dash = (q - 1) * Lambda_X_Dash / (6 * H * (u - 1)) - Omega * H * u ** 2 / 2 - (Rho + Pressure) / (2 * H)
#Compute the next Values of H and Sigma
H=H+H_Dash*h
Sigma=Sigma+u*h
#Increment x (Move on to next h)
x-=h
Я включил весь код для полноты.Я получаю ошибки в конце кода в трех уравнениях - одно в Lambda_X, другое в Lambda_X_Dot и последнее в q).
Получаемые ошибки имеют вид: «ОжидаемыйFloat, вместо него было установлено [float] '&' Expected Float получил список [float] вместо 'и т.д.Я не знаю, поможет ли это, но я использую PyCharm для написания кода.Любой вклад с благодарностью!Заранее спасибо.