papaya
papaya

Reputation: 1535

Python Overflow error multiplication

My code structure for a equation i am working on goes like this.

import matplotlib.pyplot as plt  
    for x in range (0 ,20):
        temp = x * (-1*10**(-9)*(2.73**(x/0.0258*0.8) - 1) + 3.1)
        P.append(temp)
        Q.append(x)
        print temp
plt.plot(Q,P)
plt.show()

Printing temp gives me this

    4.759377049180328889121938118
-33447.32349862001706705983714
-2238083697441414267.104517188
-1.123028419942448387512537968E+32
-5.009018636753031534804021565E+45
-2.094526332030486492065138064E+59
-8.407952213322881981287736804E+72
-3.281407666305436036872349205E+86
-1.254513385166959745710275399E+100
-4.721184644539803475363811828E+113
-1.754816222227633792004755288E+127
-6.457248346728221564046430946E+140
-2.356455347384037854507854340E+154
-8.539736787129928434375037129E+167
-3.076467506425168063232368199E+181
-1.102652635599075169095479067E+195
-3.934509583907661118429424988E+208
-1.398436369682635574296418585E+222
-4.953240988408539700713401539E+235
-1.749015740500628326472633516E+249

The results shown are highly inaccurate. I know this because, the graph obtained is not what i am supposedly to get. A quick plotting of the same equation in google gave me this

This pic shows the differences in the graphs The actual plot is the google.com one. I m fairly certain that the errors are due to the floating point calculations. Can someone help me correct the formulated equations ?

Upvotes: 0

Views: 635

Answers (1)

cel
cel

Reputation: 31349

Beginning from around 0.7 your scores drop into nothingness. Google is very clever to figure that out and limits the y-axis to a reasonable scale. In matplotlib you have to set this scale manually.

Also note that you are plotting integers from 0 to 19. When plotting continuous functions, linearly spaced points in an interval often make more sense.

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 0.8, 100)
y =  x * (-1e-9 *(2.73**(x/0.0258*0.8) - 1) + 3.1)
plt.plot(x,y)
plt.ylim(-0.5, 2)

plot

Upvotes: 2

Related Questions