This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Eric A. Nelson
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
salvin francis
Bartender
Pie
Posts: 1229
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eric A. Nelson
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be e^-(x^2) then, not e^(-x^2)
 
salvin francis
Bartender
Pie
Posts: 1229
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
Eric A. Nelson
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Campbell Ritchie
Sheriff
Pie
Posts: 47292
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


 
Campbell Ritchie
Sheriff
Pie
Posts: 47292
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will help, if the book is wrong. Which it is in this case.

Indeed. My apologies.
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
salvin francis
Bartender
Pie
Posts: 1229
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47292
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad to be of assistance too, even though I seem to have stirred things up.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic