And can you give me an example of an algorithm? alt text .jpg
EDIT: And then how would I calculate the math using Javascript? Can someone add that? Sorry to not include that context originally..
NOTE: I am using 'flot' to graph it and the input for flot is a javascript array like this:
[[x,y],[x,y],[x,y]...]
So given the values that change the curve I output all the points to an array with a loop and spit it out to flot to graph.
And can you give me an example of an algorithm? alt text http://ryancalderoni./archive/ideal_curve.jpg
EDIT: And then how would I calculate the math using Javascript? Can someone add that? Sorry to not include that context originally..
NOTE: I am using 'flot' to graph it and the input for flot is a javascript array like this:
[[x,y],[x,y],[x,y]...]
So given the values that change the curve I output all the points to an array with a loop and spit it out to flot to graph.
Share Improve this question edited Aug 13, 2010 at 19:09 MetaGuru asked Aug 13, 2010 at 18:50 MetaGuruMetaGuru 43.9k68 gold badges195 silver badges300 bronze badges 6- 2 better off on math.stackexchange? EDIT: Didn't know there was two. – James Commented Aug 13, 2010 at 18:53
- 3 You might want to try asking at math.stackexchange.. StackOverflow is meant for programming-related questions. – derekerdmann Commented Aug 13, 2010 at 18:53
- 3 @James: No. Definitely not. Math Overflow is for research-level math questions, not for things an undergrad might ponder (mostly, at least). Please stop remending that one for every math question you see here because it's usually severely out of scope there. – Joey Commented Aug 13, 2010 at 18:54
- 1 here I'll make an edit to include the programming details because that is the context I am using this in – MetaGuru Commented Aug 13, 2010 at 19:03
- 1 @Frustrated - MathOverflow is an SE 1.0 site and as such there isn't (nor will there ever be) a migration route. If MathOverflow ever migrates to SE 2.0 there may well be a migration route, but as @Johannes points out this question would be off topic there and would get instantly closed. – ChrisF ♦ Commented Aug 13, 2010 at 20:14
7 Answers
Reset to default 4A typical sigmoid curve is the tanh(x) curve.
By definition,
tanh(x) = sinh(x) / cosh(x) =
= [(1/2) (e^x - e^-x)] / [(1/2) (e^x + e^-x)] =
= (e^x - e^-x) / (e^x + e^-x) =
= (e^(2x) - 1) / (e^(2x) + 1)
(High-res)
Notice that the lines of symmetry are shifted with respect to your sample picture. To make a tanh graph look more like your example, simply move it up and to the right:
y = 1 + (e^(2x - 6) - 1) / (e^(2x - 6) + 1)
(High-res)
In JavaScript you implement this expression most efficiently as
exp2x = Math.exp(2*x)
y = (exp2x - 1) / (exp2x + 1)
Update (again)
OK, if you want y to range from 0 to 100, and x to range from 0 to 100, than you might want to try
y = 50 + 50*tanh((x−50)/10)
which looks like
(High-res)
Now
y = 50 + 50 * tanh((x−50)/10)
= 50 + 50 * (e^((x−50)/5) - 1) / (e^((x−50)/5) + 1)
The error function, erf, looks quite similar, but is much more difficult to pute (unless JavaScript has a built-in erf function).
Ryan (OP) adds: implemented!
var y = 50 + 50 * tanh((n-50)/10);
function tanh (arg) {
return (Math.exp(arg) - Math.exp(-arg)) / (Math.exp(arg) + Math.exp(-arg));
}
If you want a single, analytic function, then ArcTangent and Hyperbolic Tangent both have that shape, you just need to shift it over a bit. If you want it to only start at the origin and be flat, look at Exp[-1/x^n], for n>=1. This produces a curve that is exceedingly flat at the origin.
Try to lookup sigmoid function, it looks a lot like it.
(source: iag at ulcar.uml.edu)
A Bézier curve? The corresponding algorithm to draw it would be de Casteljau's algorithm.
Might I sugggest:
- The error function (
erf(x)
in C) - The normal cumulative distribution function (
0.5 * erfc(-x/sqrt(2))
) - The logistic function (
1.0 / (1.0 + exp(-x))
) - Any other sigmoid function
What you are looking for is called Sigmoid function. You can look it up in Wikipedia. Some function that are used for this are error function or logistic function. These are most monly use for neural networks.
All this is wonderful, but there's another thought:
Assume a third order polynomial:
(source: equationsheet.)
You have four boundary conditions:
(source: equationsheet.)
(source: equationsheet.)
If you substitute these into the equation and work out the algebra you get the following result:
(source: equationsheet.)
where
(source: equationsheet.)
and
(source: equationsheet.)
It may not be exact, but you can easily approximate it based on the values you know:
(source: equationsheet.)