I want to write a function def normalxmusigma in python vers


I want to write a function( def normal((x,mu,sigma)), in python( version 2.7),to print out values of the normal probability function( picture above, y=...)at n equally spaced points on an interval (a,b).
to test the function:
(mu,sigma)=(0,1);(a,b)=(-3,3);n=10

Solution

import math #importing math to use functions for square root, pi and exponential required to calculate normal function
def normal(x, mu, sigma):
    return (1/(sigma*math.sqrt(2*math.pi)))*math.exp(-0.5*((x-mu)/sigma)**2)

#if you need to take input, use these
#a = input(\"a: \")
#b = input(\"b: \")
#mu = input(\"mu: \")
#sigma = input(\"sigma: \")
#n = input(\"n: \")


#inputing test values
mu = 0
sigma = 1
a = -3
b = 3
n = 10


npf = []
#to store the values of npf in case required later

for i in range(n):
    x = a+i*(b-a)/(n-1) #calculating x at each interval
    ans = normal(x, mu, sigma) #using above function to calculate the value of the normal
    npf.append(ans) #adding to the list
    print \"For x =\", str(x)+ \", the value of the normal probability function is:\", ans

 I want to write a function( def normal((x,mu,sigma)), in python( version 2.7),to print out values of the normal probability function( picture above, y=...)at n

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site