# Problem with method to evaluate e^-(x^2)

Eric A. Nelson

Greenhorn

Posts: 4

posted 6 years ago

I am writing a method to evaluate e^-(x^2) without using pow or factorial.(Ex 6.7 in "How to think.."b.Downey)

Using value (4) for x and (20) for n (# of terms) I get .054946. Tried another method using pow and factorial

got same result. Calculator says result should be 1.12535 (for e^-4^2).Could someone enlighten me ?

I am completely new and this is my first post , any comments welcomed. Thanks

Using value (4) for x and (20) for n (# of terms) I get .054946. Tried another method using pow and factorial

got same result. Calculator says result should be 1.12535 (for e^-4^2).Could someone enlighten me ?

I am completely new and this is my first post , any comments welcomed. Thanks

posted 6 years ago

Welcome to java ranch

i think you have not used code tags properly,

ensure your code is within the starting and ending tags of the code tags

in order to evaluate e^(-x^2) here are the steps:

1. to calculate 'e ^ k' we need to know 'k', so first evaluate 'k'

2. 'k = -x * -x' which i feel is same as 'k = x * x', I do not understand why the negative symbol will be toggled.

3. now that 'k' is known, you could run a loop to execute 'k' times:

'result = result * e'

where 'result' was initially 1

Please state the use of numerator and denominator in the program and i could help you there.

i think you have not used code tags properly,

ensure your code is within the starting and ending tags of the code tags

in order to evaluate e^(-x^2) here are the steps:

1. to calculate 'e ^ k' we need to know 'k', so first evaluate 'k'

2. 'k = -x * -x' which i feel is same as 'k = x * x', I do not understand why the negative symbol will be toggled.

3. now that 'k' is known, you could run a loop to execute 'k' times:

'result = result * e'

where 'result' was initially 1

Please state the use of numerator and denominator in the program and i could help you there.

I agree. Here's the better link: Salvin.in

Eric A. Nelson

Greenhorn

Posts: 4

posted 6 years ago

Thanks I am trying to evaluate using infinite series expansion where e^-x^2 = 1 - 2x + 3x^2/2! -4x^3/3! +5x^4/4!.......

I am using the numerator / denominators to store value for next term. The method should return" the sum of the terms

=(-1)^i(i+1)x^i/i! ". Maybe I am missing the point of the excerise ?

I am using the numerator / denominators to store value for next term. The method should return" the sum of the terms

=(-1)^i(i+1)x^i/i! ". Maybe I am missing the point of the excerise ?

posted 6 years ago

okay,

so its a series...

whenever i see summation series, i think in this way:

Now, to evaluate [(-1)]^[i(i+1)x]^[i/i!]

we know that nextNumber is '1' if '[i(i+1)x]^[i/i!]' is even and '-1' if '[i(i+1)x]^[i/i!]' is odd

so, we are now reduced to this equation : [i(i+1)x]^[i/i!]

This is where I see an issue:

eg:

4/4! = 4/(4 * 3 * 2 * 1) = 1 / 6

and if my maths is not that weak,

k ^ (n/m) = (k^n) * (mth root of k)

Generalizing [I may be wrong here]: k ^ [i/i!] = (i-1)th root of k

so, you need (i-1)th root of i(i+1)x

I cant help you there

Phew !!

all the best !

so its a series...

whenever i see summation series, i think in this way:

Now, to evaluate [(-1)]^[i(i+1)x]^[i/i!]

we know that nextNumber is '1' if '[i(i+1)x]^[i/i!]' is even and '-1' if '[i(i+1)x]^[i/i!]' is odd

so, we are now reduced to this equation : [i(i+1)x]^[i/i!]

This is where I see an issue:

eg:

4/4! = 4/(4 * 3 * 2 * 1) = 1 / 6

and if my maths is not that weak,

k ^ (n/m) = (k^n) * (mth root of k)

Generalizing [I may be wrong here]: k ^ [i/i!] = (i-1)th root of k

so, you need (i-1)th root of i(i+1)x

I cant help you there

Phew !!

all the best !

I agree. Here's the better link: Salvin.in

Eric A. Nelson

Greenhorn

Posts: 4

posted 6 years ago

this doesn't look to bad...this:

1 - 2x + [ 3(x^2) /2!] - [4(x^3) /3!] +[5(x^4) /4!] -

looks like this:

(-1)^0 * (1 * x^0)/0! + (-1)^1 * (2 * x^1)/1! + (-1)^3 * (3*x^2)/2!)...

in general, the nth term (using 0-based indexing) is

(-1)^n * ((n+1)x^n)/n!

you just have to add up the n terms you want.

1 - 2x + [ 3(x^2) /2!] - [4(x^3) /3!] +[5(x^4) /4!] -

looks like this:

(-1)^0 * (1 * x^0)/0! + (-1)^1 * (2 * x^1)/1! + (-1)^3 * (3*x^2)/2!)...

in general, the nth term (using 0-based indexing) is

(-1)^n * ((n+1)x^n)/n!

you just have to add up the n terms you want.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Campbell Ritchie

Sheriff

Posts: 49845

70

posted 6 years ago
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

I based it off this:

1 - 2x + [ 3(x^2) /2!] - [4(x^3) /3!] +[5(x^4) /4!]...

so the terms are

in each case, the coefficient is (the term number + 1).

or am I making a dumb mistake?

1 - 2x + [ 3(x^2) /2!] - [4(x^3) /3!] +[5(x^4) /4!]...

so the terms are

in each case, the coefficient is (the term number + 1).

or am I making a dumb mistake?

Mike Simmons

Ranch Hand

Posts: 3090

14

posted 6 years ago

A MacLaurin series is a specific type of Taylor series - one that uses x = 0 as its point of expansion. The wikipedia article gives the correct MacLaurin series for e^x:

e^x = 1 + x + x^2/2! + x^3/3! + x^4/4! + ...

But the original question was how to get e^-x^2. To do that, we need a couple transformations.

Let x = -y and we get:

e^-y = 1 -y + y^2/2! - y^3/3! + y^4/4! + ...

Then let y = z^2

e^-(z^2) = 1 - z^2 + z^4/2! - z^6/3! + z^8/4! + ...

The general form for the nth term is (-1)^n * z^(2n)/n!

e^x = 1 + x + x^2/2! + x^3/3! + x^4/4! + ...

But the original question was how to get e^-x^2. To do that, we need a couple transformations.

Let x = -y and we get:

e^-y = 1 -y + y^2/2! - y^3/3! + y^4/4! + ...

Then let y = z^2

e^-(z^2) = 1 - z^2 + z^4/2! - z^6/3! + z^8/4! + ...

The general form for the nth term is (-1)^n * z^(2n)/n!

Mike Simmons

Ranch Hand

Posts: 3090

14

posted 6 years ago

It will help, if the book is wrong. Which it is in this case. The formula it gives is actually for the Gaussian function, which is the

So, I think the correct approach here is to assume that when the book says

they really meant to say

Thus, evaluating e^-(x^2) with a calculator is irrelevant. You get a different number because that's not what the formula they gave represents.

Christophe Verré wrote:Fighting over a formula is not going to help Eric. Download the PDF from here and check Exercise 6.7 The simplified form of the formula is already written there.

It will help, if the book is wrong. Which it is in this case. The formula it gives is actually for the Gaussian function, which is the

*integral*of e^-(x^2). Those familiar with integral calculus can see that by taking the formula I gave, and integrating it. Comes out to the book formula. Each formula is correct; the book just misidentified what it was talking about.

So, I think the correct approach here is to assume that when the book says

One way to evaluate e^-(x^2) is to use the infinite series expansion

they really meant to say

One way to evaluate the Gaussian function (which is the integral of e^-(x^2)) is to use the infinite series expansion

Thus, evaluating e^-(x^2) with a calculator is irrelevant. You get a different number because that's not what the formula they gave represents.

posted 6 years ago

Indeed. My apologies.

It will help, if the book is wrong. Which it is in this case.

Indeed. My apologies.

[My Blog]
*All roads lead to JavaRanch*

Mike Simmons

Ranch Hand

Posts: 3090

14

Mike Simmons

Ranch Hand

Posts: 3090

14

posted 6 years ago

Actually as I look at it some more, I think I jumped the gun here and misremembered some things. My "the book is wrong" post above may well be wrong. I'll need to look at this again later tonight. Until then, take that post with a grain of salt.

I do still think the formula I gave is

I do still think the formula I gave is

*a*correct formula for e^-(x^2). But that doesn't mean it's the only one.
Mike Simmons

Ranch Hand

Posts: 3090

14

posted 6 years ago

OK, I stand by my formula, and I stand by my statement that the book is wrong. I was, however, wrong about some of the details.

Contrary to my original "the book is wrong" post:

1. The Gaussian function is

2. The book formula is not the integral of my formula. I blew that entirely.

However, the book formula is

So the moral is the same: do not expect the book formula to give you the same answer as putting e^-(x^2) into a calculator. They are

Contrary to my original "the book is wrong" post:

1. The Gaussian function is

*not*the integral of e^-(x^2). It*is*e^-(x^2), modulo a few constants.2. The book formula is not the integral of my formula. I blew that entirely.

However, the book formula is

*not*a formula for e^-(x^2). I'm not sure what it*is*a formula for, but not that. This can be seen most easily by looking at the symmetry (or lack thereof). The function e^-(x^2) is symmetric about the Y axis - if you replace x with -x, it evaluates to the same thing (since (-x)^2 = x^2). But the book formula is clearly*not*symmetric about Y, since half the terms have odd power.So the moral is the same: do not expect the book formula to give you the same answer as putting e^-(x^2) into a calculator. They are

*not*equivalent.
Mike Simmons

Ranch Hand

Posts: 3090

14

posted 6 years ago

As a side note, the integral of the Gaussian function

*is*of considerable use in many real-world applications - it's known as the error function. And one of the best ways to calculate it is by using a Taylor series of the Gaussian, and integrating it. That's what inspired the misstatements I made before - I was thinking of a related problem, and figured that was what the book was really trying to address. Oops.
Eric A. Nelson

Greenhorn

Posts: 4

posted 6 years ago

argh !!

glad to be of assistance

Eric A. Nelson wrote:I believe my notation was defective what I meant to say was :

e^-(x^2) = 1 - 2x + [ 3(x^2) /2!] - [4(x^3) /3!] +[5(x^4) /4!] - ......

I apologize . Translating "handwritten" to "keyboard" notation is new to me.

argh !!

Eric A. Nelson wrote:Thank you to all who helped me with my problem, I was beginning to question my sanity.

I am learning on my own so I don't have access to an instructor so your help was invaluable

Thanks again.

glad to be of assistance

I agree. Here's the better link: Salvin.in