You are on page 1of 1

import numpy as np

from scipy.integrate import odeint


import matplotlib.pyplot as plt

n = eval(input("Enter n for which you need D_n: "))


xim = float(eval(input("Enter maximum range of xi for plotting\n(value <= 20 for
accuracy-computation time balance):\n")))
if (xim>20): xi_max = xim
else: xi_max = 20.
initial = 1e-11 #inital xi
niter = int(2e6)
xisur = 0.

def model(z, xi):


dDdxi = -z[1] / xi**2. #dDn/dxi = -phi/xi**2
dpdxi = pow(z[0], n) * xi**2. #dphi/dxi = D^n*xi**2
return [dDdxi, dpdxi]

z0 = [1., 0.] #initial condition: [D(0), phi(0)]


xi = np.linspace(initial, xi_max, niter)

z = odeint(model, z0, xi)

it = int(xim * (float(niter)-1.) / xi_max)


print('\nD_%d(%.2f) = %f' % (int(n), xim, z[it][0]))

for i in range(niter):
if (z[i][0] < 0.):
xisur = xi[i-1]
print('At surface, xi = %f' % xisur)
break
if (i == niter-1): print('xi_surface not found in corresponding range')

#if ((xisur != 0.) and (n > 0)):


# k = 1.5 #kappa
# rho_c = 1.5
# G = 6.67259 * 1e-11
# lamn = (float(n)+1.) * k * pow(rho_c, (1.-float(n))/float(n)) / (4*np.pi*G)
# R = lamn * xisur
# print 'Radius = %f\n' % R
#else: print 'Radius cannot be calculated'
print('\n')

zero = np.zeros(niter)
plt.plot(xi, z[:,0], label = r'$D_{%d}$' % n)
plt.plot(zero)
plt.scatter(xim, z[it][0], label = r'$D_{%d}(%.2f)$' % (int(n), xim))
plt.ylabel(r'$D_{%d}$' % n)
plt.xlabel(r'$\xi$')
plt.legend()
plt.xlim(0, xim+1)
plt.ylim(-2, np.max(z[:,0])+1)
plt.show()

You might also like