Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Equation for drawing a bell curve?

Author
Message
Rich Dersheimer
AGK Developer
17
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 31st May 2011 13:10 Edited at: 31st May 2011 13:17
I need a simple formula for drawing this:



Something along the lines of



I know it's a normal distribution curve, but I have zero math training, and could use help.

Neuro Fuzzy? Anybody?

Hodgey
16
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 31st May 2011 13:27
Hey Rich, this is the closest thing I could get to your diagram.


Hope it helps. If you want more info, look up the sine curve.

A clever person solves a problem, a wise person avoids it - Albert Einstein
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 31st May 2011 13:59 Edited at: 1st Jun 2011 09:23
The equation for the gaussian curve is
exp(-x^2)

Of course, you can transform this equation in any way you like.
Shift: exp(-(x-x1)^2)
Scale: A * exp(-x^2)
Slopes: exp(-x^2/s)
or a combination
A * exp(-(x-x1)^2/s)

[edit] For a gaussian distribution curve:
y = A * exp(-(x - x1)^2 / (2*s))
A = 1 / (sigma * sqrt(2 * pi))
x1 = mu
s = sigma
Where mu is the mean (expected value) and sigma^2 is the variance. By using these values, you can ensure that the area under the gauss curve is 1.

Cheers!
Sven B

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 1st Jun 2011 02:26
Quote: "I know it's a normal distribution curve, but I have zero math training, and could use help.

Neuro Fuzzy?"


xD I like having a reputation, but I don't know much calculus or probability stuff. The stuff I posted on this was a result of googling

Sven B's post covers everything about drawing the curve, but there's another use for this function in programming.

The program I posted here:
http://forum.thegamecreators.com/?m=forum_view&t=169807&b=6

has a useful function for use with the normal distribution. Basically... curves whose total enclosed area is 1 can be considered probability distributions. Then you have a function f(), which returns a value on the x axis of the given probability distribution. The probability of the return value being inbetween and points x1 and x2 is equal to the area under the curve inbetween x1 and x2.

So, the code snippet in that thread takes the result of the function sNormInv and draws a line there. The result of sNormInv could, theoretically, be anywhere on the number line, but due to the complex math which I copied and pasted from the corners of the interweb, it /probably/ lies near the origin.

this is the code snippet in question:


soo... I use this function if I want a bunch of objects clustered around an area, but nowhere really specific. just position an object at {normpoint(),normpoint(),normpoint()}, and it will probably be close to the origin. if you make a bunch of objects positioned like that, they will all be clustered around the origin.

Summarizing what I just said, copy the normpoint and SNormInverse functions to your project, then position an object at normpoint to have it closeish to the origin (probably).


Tell me if there's a broken link to images in a thread I post, and I'll fix 'em.
Rich Dersheimer
AGK Developer
17
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 1st Jun 2011 03:41 Edited at: 1st Jun 2011 03:43
Thanks Hodgey, that got me to what I needed. I ended up with this code:



which gave me this:



@ SvenB and Neuro Fuzzy - a little over my head, but I promise I'll give it some study, maybe I can learn some new tricks after all.

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 1st Jun 2011 04:09 Edited at: 1st Jun 2011 04:20
what about


[edit]
or demonstrating the normal distribution whose total area is 1:


[edit2]
and keep in mind, I really don't know /why/ that magic number (~.3989=1/sqrt(2*pi)) is there, I just read that it is so I put it there. It's not important to completely understand the math, you just need to write functions that plug numbers in.


Tell me if there's a broken link to images in a thread I post, and I'll fix 'em.
Rich Dersheimer
AGK Developer
17
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 1st Jun 2011 04:46
@Neuro Fuzzy - that first snippet is cool, but my jaw just hit the floor when I saw that second one. Kind of creepy, like blood about to drip from the ceiling.

Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 1st Jun 2011 09:22 Edited at: 1st Jun 2011 13:25
Quote: "and keep in mind, I really don't know /why/ that magic number (~.3989=1/sqrt(2*pi)) is there, I just read that it is so I put it there. It's not important to completely understand the math, you just need to write functions that plug numbers in."


Hi NeuroFuzzy,

The sqrt(2*pi) is the area under the function e^(-x^2/2) (in other words, the integral from -infinity to infinity). The actual calculation of the area is kind of hard though, because 'normal' techniques won't cut it. I'm sure we had to do it once, but I forgot in which book they did it...
By the way, I noticed that I forgot a 1/2 in the exponent of the gaussian curve formula there. My bad! I'm editing it in a second.

Just thought I'd throw this in.
Sven

[edit] Forgot /2 again... exp(-x^2/2)

Hodgey
16
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 1st Jun 2011 09:24
Quote: "Thanks Hodgey, that got me to what I needed."

You're welcome, glad it helped

A clever person solves a problem, a wise person avoids it - Albert Einstein
Bluespark
15
Years of Service
User Offline
Joined: 18th Mar 2011
Location:
Posted: 2nd Jun 2011 01:16
It's 1/Sqrt[2*Pi]. One way is square the integral and change it to polar form. Since the limit of an infinitely large circle and square are the same, the polar form gives 1/(2*Pi). Since we squared, take the square root and that's where the result comes from.
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 2nd Jun 2011 09:21
Quote: "It's 1/Sqrt[2*Pi]. One way is square the integral and change it to polar form. Since the limit of an infinitely large circle and square are the same, the polar form gives 1/(2*Pi). Since we squared, take the square root and that's where the result comes from."


Ah yes I remember now . We enclosed the square in two concentric circles to prove that the result converges to the same value as the square.

Cheers!
Sven

Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 2nd Jun 2011 11:53
You can get a decent curve very simply with this:
(1-x²)²

In the range 0 <= x <= 1 it will look like that curve and it's often used in metaballs because it's the simplest equation that gives a smooth transition from 0 to 1.

[b]
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 2nd Jun 2011 14:57
When drawing the curve, you can speed up drawing immensely by replacing LINE with BOX. In Neuro's 2nd snippet of the 'dripping' curve, frame rates went from about 100 to 3300.

Login to post a reply

Server time is: 2026-07-10 22:55:09
Your offset time is: 2026-07-10 22:55:09