bhjghjh
bhjghjh

Reputation: 917

ValueError: Maximum allowed size exceeded, variable will range from 0 to 3e33

I have this strange problem with np.arange. I want to plot a simple equation which basically looks like y = Ax^{-1/3}(1-Bx^{4/3})^{1/2}

However, I can get a almost working-quality plot from wolfram mathematica with my provided equation but I am struggling to generate the same plot in python!

import numpy as np
import matplotlib.pyplot as plt
import math

# evenly sampled time at 200ms intervals
x = np.arange(0., 10**33, 10**8.)

plt.plot(x**(-1/3)*(1.102*10**20)*(1-(x**(4/3)*2.424*10**(-45)))**(1/2))

plt.xlim(math.pow(10,31), 3*math.pow(10,33))
plt.ylim(5*math.pow(10,8), 2.5*math.pow(10,9))

plt.xlabel("M(g)", fontsize =13)
plt.ylabel("R(cm)", fontsize=13)
plt.show()

my variable x should run from 0 to 3e33 and I want to see the plot both in linear and loglog plot, but I am having memory issues with the x range and if I set a smaller range, I basically get no plot at all. I am sure I am doing something wrong here, I just do not see it. Your help is appreciated.

Upvotes: 1

Views: 15793

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339480

There are several problems in the code:

  1. x has too many points. Reduce the number of points, to e.g. 1000 points.
  2. x should not start at 0, since 0**(-1/3) is undefined (you cannot divide by 0). Thus a sensible definition of x may be

    x = np.linspace(1e30, 1e33, 1001)
    
  3. The x values do not actually appear in the plot, since you only plot y, plt.plot(y) instead of y vs. x: plt.plot(x,y)

In total,

from __future__ import division # if using python 2
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(1e30, 1e33, 1001)
y = x**(-1/3)*(1.102*10**20)*(1-(x**(4/3)*2.424*10**(-45)))**(1/2)
plt.plot(x,y)

plt.xlabel("M(g)", fontsize =13)
plt.ylabel("R(cm)", fontsize=13)
plt.show()

will provide

enter image description here

Upvotes: 3

Related Questions