Page 1 of 1

Trigometric Graph

Posted: Thu Oct 12, 2006 11:49 pm
by neonash7777
Ok when you graph sine you get the S shaped thing. It's starts with an up hump. I need an equation for a parabola whose curve above the x axis matches up with the first hump of sine.

Essentially then there will be an equation that will do the sine function for you.
All you do is reduce the angle mod 180, enter that into the equation and solve for Y. Then if the original angle mod 360 is > 180 we tack on a minus sine.

Even if the equation is only aprx close so that it gets to like the 3rd decimal spot, that'ld be cool.

Thanks in advance!

Posted: Fri Oct 13, 2006 4:37 am
by zrosnbrick
Using Taylor Series', I get the following approximation for sin(x) which holds true for about the first "hump" (0 to pi):

x - (1/6)x3 + (1/120)x5 - (1/5040)x7 + (1/362880)x9

Posted: Fri Oct 13, 2006 4:44 am
by zrosnbrick
However, you really only need an approximation which holds true for the first half of the first "hump" (0 to pi/2):

x - (1/6)x3 + (1/120)x5

Posted: Fri Oct 13, 2006 6:41 pm
by ThomasH
zrosnbrick, it seems to me, that, if the factor in front of x*x is sufficient small, we will need the whole "hump" from zero to pi. And with the approximation, we still dont have the solution.
For me, problems like this are very well suited for the search of a zero: we look for a root of the function zero(x) = sin(x) - a * x * x . First we make a small list of values of zero(x) and pick two values x1, x2 with zero(x1)>0 and zero(x2)<0 (the process, I am describing, is "bisection"). Next we evaluate zero at xm=(x1+x2)/2 and keep xm and one of x1 or x2 (lets call it x0) in that way, that zero(xm) and zero(x0) have different signs again. We continue the process and end up very soon at sufficient significant places.
If we want to do it with less iterations, we should do it with the "newton" formula.

Posted: Fri Oct 13, 2006 10:08 pm
by zrosnbrick
Actually, the first formula I gave is valid up to two to three decimal places for values of 0 to pi.

Posted: Sat Oct 14, 2006 3:20 am
by neonash7777
zrosnbrick wrote:However, you really only need an approximation which holds true for the first half of the first "hump" (0 to pi/2):

x - (1/6)x3 + (1/120)x5
So let's say I have an angle ß,
I reduce it mod 180
if it's [ß(mod180)]>90 then we do 90-[ß(mod90)]
this will mirror the second half to make a complete hump.

Then we plug that number into the equation and get the digital answer.
Then if the original number mod 360 is >180 we tack on a - sign.

Is this correct?
Here I wrote a code

Code: Select all

  dim x as single
  dim z as single
  z=x mod 180
  if z>90 then
  end if
  If (x mod 360)>180 then
  end if
It doesn't work...what's wrong?

I guess you did say it worked from 0-pi/2 but even the the equation gets off quite a bit.... Isn't there some parabola that can match exactly?

Posted: Sat Oct 14, 2006 8:40 am
by harryh
neonash7777 wrote:It doesn't work...what's wrong?
The expansion suggested by zrosnbrick, requires x to be in radians (not degrees). Judging by the statements "z=x mod 180", "if x>90 then..." etc which you use in your program, I assume you want to enter your angle in degrees. So, before your statement z=z-(1/6)*(Pow(z,3))+(1/120)*(Pow(z,5))
you have to add something like z=z*pi/180 to make it work.

Before that, when the angle (in degrees) is z>900 you should set z=180-z (rather than 90-z), because the 'hump' is symmetric about z=900 ( e.g. sin 1000=sin 800 ).

In addition, depending on the version of VB you are using, (1/6) and (1/120) may evaluate to integers, in which case they will always result to zero. If this happens, change them to (1./6) and (1./120) to force their evauation to floating-points.

Finally, I doubt that your approach will work for some negative values of the angle. I think that for e.g. x=-150, the function (x mod 180) returns -150, rather than 30, which is what you may have expected. I believe that you can find your way around this small difficulty. If not, say so and we'll help.
... Isn't there some parabola that can match exactly?
No, there is no such 'parabola'. The three-term expansion that zrosnbrick gave, will give a result accurate to 4 or 5 decimal digits for angles 00 to 450 and less accurate results for larger angles (you will only get results accurate to 2 or 3 decimal digits for angles approaching 900). If you need higher accuracy you can use the five-term expansion, and if that's still not good enough for your purposes, you can use even more terms. Just remember that, whereas (for a certain small range of angles) all these look a bit like parabolas, they are definitely not parabolas in the strict mathematical sense.

Posted: Sat Oct 14, 2006 10:07 pm
by hk
Here are some functions to calculate sin and cos for any angle in degrees.
The error is always less then 6*10^-6.
The costants are specially calculated so that the special values 0,30,45,60 etc are calculated with a very great precision.
The functions mysmallcos and mysmallsin are auxiliary functions and it's better not to use them directly unless you are knowing what you do.

Code: Select all

function mysmallsin(x:single):single;
var y:single;
//mysmallsin:=a*x+b*power(x,3)+c*power(x,5) can be written as:

function mysmallcos(x:single):single;
//mysmallcos:=1+a*power(x,2)+b*power(x,4)+c*power(x,6) can be written as:

function mycos(z:single):single;
var x,y:single;
while z>360 do z:=z-360;
if z>180 then z:=360-z;
if z>90 then x:=180-z else x:=z;
if x<65 then y:=mysmallcos(x) else y:=mysmallsin(90-x);
if z>90 then y:=-y;

function mysin(z:single):single;

Re: Trigometric Graph

Posted: Wed Dec 15, 2010 4:54 am
by entitycs
For f(x)=sin(x):

The curve starts with x and y equal to zero; y works its way to one, and then repeats, with a period of 2 Pi.

The top of the curve is when y = 1 (one). This happens when x is equal to Pi/2, or half Pi.

Using the vertex formula, g(x) = a(x-h)^2 + k, we can insert the maximum of f(x) as the vertex, (Pi/2 , 1).

then solve for a. So g(x) = a(x-h)^2 + k becomesg(x) = a[x- (Pi/2) ]^2 + 1.

You can then plug in other (x , y) coordinates of the formula f(x) into the formula g(x); (Pi/6 , 1/2); (Pi , 0); (Pi/4 , sqrt(2)/2);

You end up with formulas of REAL parabolas, ranging from g(x)=(-4/Pi)[x - (Pi/2)]^2 + 1 to g(x)=(-4.5/Pi)[x - (Pi/2)]^2 + 1.

You could make a "piecewise" function in which the -4 to -4.5 is not a constant, but a variable tied to x...

-4.5 would make the values near the x-intercepts more accurate. -4.25 splits the difference pretty well.

So the most accurate real parabola, IMO, would be g(x)=(-4.25/Pi)[x - (Pi/2)]^2 + 1.

variable a = -4.25;
variable h = (Pi/2);
variable k = 1;

Once your x (or i in loop) is equal to 2*Pi, you multiply variables a and k by negative one. h is increased or decreased by 2*Pi depending on x.
So once you have reached ~2*Pi, OR g(x) is not greater than 0 OR if x % 2*Pi ==0 OR whatever... THEN

variable a *= -1;
variable h += (2*Pi);
variable k *= -1;

You can run the same function twice, then loop for a period. hypothetically, of coarse.

Re: Trigometric Graph

Posted: Wed Dec 15, 2010 7:45 am
by Lord_Farin
Any parabola will be significantly off, since the power series of the sine at $\pi/2$ is equal to that of the cosine at zero, which is (first terms) given by $$\sin(x+\pi/2)=\cos(x)\approx 1-\frac{x^2}{2}+\frac{x^4}{4!}+...$$ from which we see that the quartic term contributes a factor 1/24 and the quadratic term contributes 1/2. So if your formula is to be correct up to 0 and $\pi$, there is no parabola which is everywhere correct up to the third digit.

For your interest, the formal power series of sine at $\pi/2$ is given by $$sin(x+\pi/2)=\sum_{n=0}^\infty \frac{(-1)^nx^{2n}}{(2n)!}$$