# Math Help - Need a generic function to perform a task.

1. ## Need a generic function to perform a task.

I need to perform a task using a math function (i'm a programer not a mathematician unfortunately).

The task i need to perform is to cycle through a list in a sinusoidal motion according to an increasing count.

the count (time) is increasing at a fixed rate, no problems there, however, I need the number of items in the cycle to change.

For example I have a 3 item cycle, 1->2->3->2->1->2 etc

I have managed to write an equation that will give me the result I need for this case:

$y = sin(rad(90)*x)-rad(90))+2$

So if t = x, number of items = 3:

Code:
t0 = 1,
t1 = 2,
t2 = 3,
t3 = 2,
t4 = 1
(with rounding of the result)

However, I may need to vary the number of items in the cycle to any arbitrary number, and as such would need for example the following to be true:

8 items:

Code:
t0 = 1,
t1 = 2,
t2 = 3,
t3 = 4,
t4 = 5,
t6 = 7,
t7 = 8,
t8 = 7,
t9 = 6,
t10 = 5,
...
t of 14 = 1
how would I modify the equation to take into account the number of items in the cycle? (If at all possible)

Thanks

2. Originally Posted by Alias_NeO
I need to perform a task using a math function (i'm a programer not a mathematician unfortunately).

The task i need to perform is to cycle through a list in a sinusoidal motion according to an increasing count.

the count (time) is increasing at a fixed rate, no problems there, however, I need the number of items in the cycle to change.

For example I have a 3 item cycle, 1->2->3->2->1->2 etc

I have managed to write an equation that will give me the result I need for this case:

$y = sin(rad(90)*x)-rad(90))+2$

So if t = x, number of items = 3:

Code:
t0 = 1,
t1 = 2,
t2 = 3,
t3 = 2,
t4 = 1
(with rounding of the result)

However, I may need to vary the number of items in the cycle to any arbitrary number, and as such would need for example the following to be true:

8 items:

Code:
t0 = 1,
t1 = 2,
t2 = 3,
t3 = 4,
t4 = 5,
t6 = 7,
t7 = 8,
t8 = 7,
t9 = 6,
t10 = 5,
...
t of 14 = 1
how would I modify the equation to take into account the number of items in the cycle? (If at all possible)

Thanks
This has nothing to do with sinusoids, you seem to want a sequence which increments/decrements by 1 repeatedly between and upper and lower limit.

Now why do you want a formula for the n-th term of this why not just generate the sequence programaticaly?

CB

3. Unfortunately, the structure of my application means it cannot be done programatically, there is no fixed number of times the calculation will be performed, nor is the time between calculations fixed and incrementing. I need it to be calculated on the spot according to whatever value of t is passed at the time.

In other words, as far as my application (abstract and programming) goes, time is not linearly increasing. It could be the same, it could be the same time for 10 or a hundred iterations before time changes.

It's hard for me to explain unless you can tell me you're a developer and I can use some technical terms?

4. Hi

I have benn writing apps for may years, so explain away and I will see if I can help

5. I think I may have found a solution using modulus now, but for the sake of completion I'll explain the initial requirement.

I have a function in my program that is called every time the screen of the device is refreshed, this is an arbitrary, unknown number. Because I don't want the value to increment the function is called, I pass it a "tick" this tick is my "t" above, it checks against this if time has changed or not and calculates the new value accordingly.

Another method would be for me to store the previous time and check that way and increment if time has changed, but because my class and method are static storage isn't ideal.

6. Let $a$ be some parameter. Consider $f(n)=a-|(n\mathrel{\mathrm{mod}}2a)-a|$. Here $n\mathrel{\mathrm{mod}}2a$ is the remainder when n is divided by 2a.

Explanation. The absolute value |n| goes down to 0 when n is negative goes back up when n is positive. Next, |n-a| means shifting the graph of |n| to the right a units, so now the value at a is 0. Next, a-|n-a| flips the graph upside down and raises it a units, so now the value at 0 and 2a is 0, and the value at a is a. Finally, replacing n with (n mod 2a) makes the function periodic with the period 2a.

7. Originally Posted by emakarov
Let $a$ be some parameter. Consider $f(n)=a-|(n\mathrel{\mathrm{mod}}2a)-a|$. Here $n\mathrel{\mathrm{mod}}2a$ is the remainder when n is divided by 2a.

Explanation. The absolute value |n| goes down to 0 when n is negative goes back up when n is positive. Next, |n-a| means shifting the graph of |n| to the right a units, so now the value at a is 0. Next, a-|n-a| flips the graph upside down and raises it a units, so now the value at 0 and 2a is 0, and the value at a is a. Finally, replacing n with (n mod 2a) makes the function periodic with the period 2a.
Perfect, that's exactly the modulus function I needed. With a couple of slight changes to shift the values up from zero, that works perfectly. Thanks.