World's most popular travel blog for travel bloggers.

Producing an algebric equation from a graph

, ,
Problem Detail:

I'm writing a computer game and one of my game's objects must follow a movement path that is very similar to the following graph.

The bold lines are the Y and X axis. In order to be able to code this movement thought I need the algebric equation that translates to this graph. For example f(x) = x^2.

Is there any mathematic technique I could use to obtain the algebric from of this function?

We can start from $\sin(x)$ which has a nice regular graph. To avoid negative values you can simply use the absolute value $|\sin(x)|$. This produces a graph similar to your but with constant height.

To decrease height as $x \to \infty$ we want to multiply that function by something that decrements. For example $\frac{1}{1+|x|}$ goes to $0$ as $x \to \infty$ and is symmetric. So $\frac{1}{1+|x|}|\sin(x)|$ is something similar to what you want with a maximum height of $1$.

If you multiply by a constant $A$ you "set" the maximum height to $A$. If you want to change the width of the bumps you can simply multiply the argument of the $\sin$ function, for example $\frac{A}{1+|x|}|\sin(2x)|$ will have maximum height $A$ and a width that is half the width of the normal $\sin$, while using $\sin(x/2)$ would produce a bump that is twice the width of the normal $\sin$.

See this wolfram alpha's graph for an example result.

You can also change the degradation of height by setting a different power for the $|x|$. For example using $\frac{1}{1+x^2}$ you'd have a faster drop to $0$, while using $\frac{1}{1+\sqrt{|x|}}$ would produce a slower change in height.