Need help with python!?

How would you write this equation in python?

f(x)= 1/sqrt(12π) * e^−x^2/2.

also, x = 0.15

I don t know how I would write and solve the equation.

1 Answer

Relevance
  • 10 months ago
    Favorite Answer

    I'll guess that you meant e^(-x^2 / 2). What you typed groups as (e^(-x^2)) / 2, performing exponentiation before

    import math

    PI = math.pi

    E = math.e

    # version 1: literal translation:

    def f_literal(x):

    .... return E**(-x**2 / 2) / (12 * PI)**0.5

    For powers of e, it's better to use math.exp() than the ** operator; and math.sqrt() is probably faster than ** too. Here's how I'd write it:

    FSCALE = 1.0 / math.sqrt(12 * PI) # compute the constant scale factor just once

    def f(x):

    .... 'returns exp(-x**2 / 2) / sqrt(12*pi)'

    .... return FSCALE * math.exp(-0.5 * x * x)

    That formula for f(x) isn't really an equation to solve. It's a definition of what f(x) means. If you want the value of f when x == 0.15, that looks almost the same in Python as it does in algebra:

    print('f(0.15) =', f(0.15))

    The expression f(0.15) calls f() with a value of 0.15 as the x argument.

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.