Home > database >  Python Numpy Does Not Give Correct Numerical Answer?
Python Numpy Does Not Give Correct Numerical Answer?

Time:08-07

I wish to compute the following quantity:

enter image description here

Where:

t=14.4022468115
a=2255.5422194535317
b=8.75408541256545
m=2.853672849711247
s=0.4316116619435228

My pocket calculator, desmos, and another calculator all agree on the answer 140.636091727. However, when I try to do this in Python with Numpy:

def func(t, a, b, m, s): 
    return b   a*(np.exp((-1)*((np.log(t)-m)**2)/(2*s**2))) / (t)*(s)*np.sqrt(2*np.pi)

func(t,a,b,m,s) gives an answer 163.1201224885765, which is simply not correct.

What had gone wrong? I searched other posts. Some posts says np.exp may not give the right answer in case of overflow or in float64. But the result seems to be the same even if I manually round these numbers. Or can it be a syntax error? I have stared at it for too long to notice.

CodePudding user response:

You have to put the denominator of the fraction in the second section in parentheses:

(t)(s)np.sqrt(2np.pi) -> ( (t)(s)np.sqrt(2np.pi) )

import numpy as np
def func(t, a, b, m, s): 
    return b   a*(np.exp((-1)*((np.log(t)-m)**2)/(2*s**2))) / ((t)*(s)*np.sqrt(2*np.pi))

t=14.4022468115
a=2255.5422194535317
b=8.75408541256545
m=2.853672849711247
s=0.4316116619435228

print(func(t,a,b,m,s)) #140.63609172707763
  • Related