I'm working on finding an plotting solutions to the Lane-Emden equation for values n=[0,6]
, in intervals of 1/2
. I'm new to Python, and can't seem to figure out how to use RK4 to make this work. Please help!
Current progress.
TypeError: unsupported operand type(s) for Pow: 'int' and 'list' on line 37 in main.py
The error just appeared after I added in the equations defined as r2
, r3
, r4
and k2
, k3
, k4
.
import numpy as np
import matplotlib.pyplot as plt
n = [0,1,2,3,4,5,6,7,8,9,10,11,12,13]
theta0 = 1
phi0 = 0
step = 0.01
xi0 = 0
xi_max = 100
theta = theta0
phi = phi0
xi = xi0 + step
Theta = [[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
Phi = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
Xi = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
for i in n:
Theta[i].append(theta)
Phi[i].append(phi)
Xi[i].append(xi)
def dTheta_dXi(phi,xi): #r1
return -phi/xi**2
def r2(phi,xi):
return dTheta_dXi(phi+step,xi+step*dTheta_dXi(phi,xi))
def r3(phi,xi):
return dTheta_dXi(phi+step,xi+step*r2(phi,xi))
def r4(phi,xi):
return dTheta_dXi(phi+step,xi+step*r3(phi,xi))
def dPhi_dXi(theta,xi,n): #k1
return theta**(n)*xi**2
def k2(theta,xi,n):
return dPhi_dXi(theta+step,xi+step*dPhi_dXi(theta,xi,n),n)
def k3(theta,xi,n):
return dPhi_dXi(theta+step,xi+step*k2(theta,xi,n),n)
def k4(theta,xi,n):
return dPhi_dXi(theta+step,xi+step*k3(theta,xi,n),n)
for i in n:
while xi < xi_max:
if theta < 0:
break
dTheta = (step/6)*(dTheta_dXi(phi,xi)+2*r2(phi,xi)+2*r3(phi,xi)+r4(phi,xi))
dPhi = (step/6)*(dPhi_dXi(theta,xi,i/2.)+2*k2(theta,xi,n)+2*k3(theta,xi,n)+k4(theta,xi,n))
theta += dTheta
phi += dPhi
xi += step
Theta[i].append(theta)
Phi[i].append(phi)
Xi[i].append(xi)
print i/2., round(xi,4), round(dTheta_dXi(phi,xi),4), round(xi/3./dTheta_dXi(phi,xi),4), round(1./(4*np.pi*(i/2.+1))/dTheta_dXi(phi,xi)**2,4)
theta = theta0
phi = phi0
xi = xi0 + step
See Question&Answers more detail:
os