Aproximate Sin()

STAii

Greetings !
The other day i was fooling around a little with equations, i came up with an equation that (when graphed) look very similiar to the graph of y=Sin() .
Tell me what you think, and if anyone cares to know how i came up with it, i will tell.
y = -1[ x/&pi; ]((-4x2/&pi;2) + (x/&pi;)(8[ x/&pi; ] + 4) - 4[ x/&pi; ]([ x/&pi; ] + 1))
Note that [ x ] means the integer of x.
Edit :
&pi; is supposed to be [pi], i don't know why it looks so ugly !

Last edited:

Yes, tell us, how did you come up with it? For what values of x does it work for?

The pi symbol looks ugly because its capitol pi.

HallsofIvy

Homework Helper
???

For x= pi/2, this formula gives y= -1/2. Doesn't look like sin(x) to me.

STAii

Actually, the graph is 100% right at n[pi]/2 (where n in an integer), it seems you have a mistake somewhere.
Let me demonstrate when x = pi/2
x/pi = pi/2pi = 1/2
[ x/pi ] = [ pi/2pi ] = [ 1/2 ] = 0
x2/pi2 = pi2/4pi2 = 1/4
So :
y = -10((-4*0.25) + 0.5*(0*8 + 4) + 4*0*(0+1))
y = 1*(-1 + 0.5*(0 + 4) + 0)
y = -1 + 4/2
y = -1 + 2
y = 1

I wonder how you got -1/2 ...

I will tell you how i came up with it, but i unfortunately have no time now, some time soon i will post it here.

Thanks !

STAii

Ok, i thought a second about it, maybe saying the [ x ] is the integer of x was misleading.
What i mean by [ x ] is :
[ x ] = n , where n in a certain integer that satisfy :
n <= x < n+1

HallsofIvy

Homework Helper
Well, you're right about pi/2: I wrote a number down wrong.

This really is a very nice approximation to sin(x).

STAii

Ok, so now that i have time, i will write down how i came up with the idea.
It was while i was preparing for my physics exam (i never read the textbook before, this was my first time ), one of the chapters in our textbook talks about simply harmonic motion.
Now, a simply harmonic motion (sorry if the naming is wrong, but you know it hard to get the right word in English), the force must always be in opposite direction of displacement, and must be proportional to distance between the object and the point of equilibrum.
At that point, i tried to think, what is the different between the simple harmonic motion in half a cycle, and the motion of an object in ballestic trajectory (ignoring air resistance).
I tried to think, i reached the conclusion that it must be something related to in which part of the motion the biggest part of change in speed happen.
I thought, a graph would make everything clearer, so, i tried to graph the motion of a balestic (moving upwards) and simply harmonic motion in half a cycle (distance versus time graph), with the same heighest point, and the same time interval.
As we know, the motion of an object moving upwards under the effect of gravity in a distance versus time graph is actually a parabola (not sure of the naming, but it can be written as ax2+bx+c).
So, i took 3 test points, to make my two graphs.
(0,0)
(&pi;/2,1)
(&pi;,0)
So, i made a function, called f(x), defined on the interval [0,&pi;]
f(x)=ax2+bx+c
f(0)=0
f(&pi;/2)=1
f(&pi;)=0
I then solved to get the values of a,b and c.
from the first equation, c=0
from third equation, a=-b/&pi;
solve with second equation, b=4/&pi;
and, a=-4/&pi;2
Now, i graphed my f(x) on [0,&pi;] and sin(x) on the same interval, they looked very similiar (and i was able to figure out the difference between the two types of motion).
My next step, to make my f(x) the same as cos(x) on other intevals.
Now, on the interval [&pi;,2&pi;] is the same as on [0,&pi;], except it will be shifted &pi; to the right, and it will be inverted (or, in other words, multiplied by -1).
So, on the interval [&pi;,2&pi;] -f(x-&pi;) will almost match sin(x) on the same interval, and so on ...
Now, i needed a way to the -&pi; jumps each time x gets &pi; bigger.
I thought, if i found an integer that gets bigger by 1 each time x gets &pi; bigger, i will only need to multiply it by &pi;.
And [ x/&pi; ] is what i needed, this function gets bigger by 1 each time x gets bigger by &pi; (and on steps).
So, the function f(x-&pi;[ x /&pi; ]) is almost what we need, it oscilates, and a new half cycle starts every n&pi;, the only problem is getting it up and down.
We need it up when x is in [ 2n&pi;, 2n&pi; + &pi;] and down when x is in [ 2n&pi; + &pi;, 2n&pi; + 2&pi; ].
I have been taught the trick of -1n (yes, i was taught it, this is the only thing that i didn't think of), when n is odd the result is -1, when n is even the result is 1.
So, my last function was -1[ x/&pi; ]f(x-&pi;[ x/&pi; ]), where f(x) = ax2+bx+c, and a=-4/&pi;2, b=4/&pi; , c=0.
The version of the equation you see in my first post is a simplified one.
If you have any questions or comment, please feel free to tell them .
Thanks for your time all.

Last edited:

bogdan

Well...it uses the fact that sin(x) can be approximated by a a*x^2+b*x+c function in the vecinity of 0...taylor...the rest is very clever...:)

suffian

I commend you on the way you came up with a good approximation to sin(x), even if it tends to more complexity than less. From a practical perspective, i'd just use a table look-up (computers often implement the function this way when they need speed).

But I wanted to comment that you should be using the FLOOR function instead of the INTEGER_PART function.

For example:
[-x/pi] = -[x/pi] or for all x

Which means that from (-pi, 0) your function f(x) is not shifted at all (shift factor of [x/pi] is 0). So the value -pi < x < 0 is plugged into the original function f(x). Using floor will fix this anomaly.

Last edited by a moderator:

STAii

Well, maybe the way i named the function (i called it Integer function) was wrong, but i think i corrected this in previous post, when i said :
What i mean by [ x ] is :
[ x ] = n , where n in a certain integer that satisfy :
n <= x < n+1
According to this definition, when x is in (-pi,0) then [ x/pi ] = -1, which makes the shift to the left.
Right ? (maybe i am missing sth).

EDIT :
Thanks suffian for the graph, and thanks all for your time.
Suffian, you are right, the function makes the whole thing more complicated, but it was fun making it Last edited:

suffian

Well, maybe the way i named the function (i called it Integer function) was wrong, but i think i corrected this in previous post, [...]
oh, sorry, i didn't see that part.

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving