This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.

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

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.

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 ?

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 !

Eric A. Nelson
Greenhorn

Joined: Jan 05, 2010
Posts: 4

posted

0

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.

Are you sure about the (n + 1) factor? It is a long time since I did that sort of maths, so I might have forgotten, but I thought the terms were x^n / n! Obviously if x < 0 then the terms will alternate between positive and negative.

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

or am I making a dumb mistake?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43993

33

posted

0

In Wikipedia they call it a Taylor series (or a McLaurin series), and that for e^x doesn't have the term number as a factor, only as an exponent and a factorial.

Of course, I might still be very mistaken about what this series is supposed to represent. Check carefully; I could be out by miles.

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.

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.

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028

10

posted

0

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.

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

Indeed. My apologies.

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028

10

posted

0

Christophe Verré wrote:

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

Indeed. My apologies.

Not at all - your post got me to look at the book, which allowed us to find the real problem.

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028

10

posted

0

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 a correct formula for e^-(x^2). But that doesn't mean it's the only one.

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028

10

posted

0

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 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

Joined: Mar 05, 2008
Posts: 3028

10

posted

0

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

Joined: Jan 05, 2010
Posts: 4

posted

0

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.

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

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43993

33

posted

0

I'm glad to be of assistance too, even though I seem to have stirred things up.

I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com