### taylor sequence in c++

Pages: 12
hello,

i have a small project which ask the user to input value of 'x', then show sin(x),cos(x) and tan(x) using this fourmla

http://www.daniweb.com/forums/attachment.php?attachmentid=16080&stc=1&d=1280001204

and the project is says that i can use this fourmla to minmise the computation and increase the efficiency

http://www.daniweb.com/forums/attachment.php?attachmentid=16081&stc=1&d=1280001204

so my first question, when i use the first fourmla, then how can i use the second one?

please give me a simple example (please notice that i'm really poor in math)

* * * * *

my second question,

this is my function to approximate sin(x)

 123456789101112 float sin(float x) { float result = x; int limit = 15; int sign = -1; for(int i=3 ; i

and this to approximate cos(x)

 123456789101112 float cos(float x) { float result = 1; int limit = 15; int sign = -1; for(int i=2 ; i

but the answer is not clear in some cases and in other cases it displays wrong answers

try to input 1.5707963

cos should equals to 0.0000000

but my function shows -0.263853, so the answer is not that good

waiting for you
 when i use the first fourmla, then how can i use the second one?
I don't understand the question.

Line 9 is wrong. You're supposed to just assign the value of the expression to result, not increment result by the value of the expression.
helios wrote:
Line 9 is wrong. You're supposed to just assign the value of the expression to result, not increment result by the value of the expression.

That wouldn't be quite right either.
I believe empror9 is trying to take advantage of the fact that x²/2! * x²/(4!/2!) = x^4/4! and so on.
This doesn't work in practice, because result contains the sum of all elements so far, not just the last.

It works with a small change:
 12345678910111213141516 float cos_taylor(float x) { float result = 1; int limit = 20; int sign = -1; float last=1; for(int i=2 ; i
Last edited on
welcome helios

i mean when i use the first formula then the second formula is for what? and how to use it?

do you mean like this one? result = sign*result*(x*x)/(i*(i-1)); but the result is not clear

*****

welcome Athar

hmmm so what do you suggest?

i jsut tried your function but it's not correct :(
 result contains the sum of all elements so far, not just the last.
Not according to http://www.daniweb.com/forums/attachment.php?attachmentid=16081&stc=1&d=1280001204
According to that, termi is a product, not a sum.

 i mean when i use the first formula then the second formula is for what? and how to use it?
Use one OR the other. The second one seems to be an optimization of the first one.

 the result is not clear
What do you mean, "not clear"?
 Not according to http://www.daniweb.com/forums/attachment.php?attachmentid=16081&stc=1&d=1280001204 According to that, termi is a product, not a sum.

Didn't see that, but it just defines the terms used in the sum.
The sum of x/1 and all terms results in sin x/cos x.
Last edited on
sorry, your code in some cases it shows a very close value, but in som others it show a really diffrent value even whan i use a double data type

try to input 13.6135682
helios i mean the result is not correct and not close

ok can i use both to get the real value of x?
The Taylor approximation only applies for the range -π +π.
Check this graph:
http://en.wikipedia.org/wiki/File:Taylorsine.svg
actully these all numbers are examples of the project (samples run for my project)

so that's why i tried larges numbers

what should i do now?

ok, i try to input 1.5707963

sin should equals to 1.0000000 but the result is 0.849976

so if i use the second fourmla,the result will be same as 1.000000?
Well, you should show your new sin source code... you probably still have a mistake in it.
The 15th degree Taylor polynomial should be fairly accurate up to abs(1.5*pi), so there must still be some bug in your code.

If you need something that's accurate on the entire real line, though, why don't you just use the regular trigonometric functions, instead of their approximations?
ok this is the function

 123456789101112 double sin(double x) { double result = x; int limit = 15; int sign = -1; for(int i=3 ; i

******

the queston says that i must use the fourmlas in the project, which are the two fourmlas here in my topic

so i can't :(
Ouch.
What I said obviously applies to sin as well! Looks like my first impression of your math skills was quite wrong...
here is another function which somebody given me and he said it's working

 123456789101112131415 float sin(float x) { float result = x; float term = x; // ADD THIS int limit = 15; int sign = -1; for(int i=3 ; i

it's working but in some cases it shows a really diffrenet result

Last edited on
athar i'm really tried the function but still show a diffrent result

see this is my pervous comment

sorry, your code in some cases it shows a very close value, but in som others it show a really diffrent value even whan i use a double data type

try to input 13.6135682
this function is working fine, but i have to increase the limit to 100!!

 12345678910111213141516 double sin(double x) { float result = x; float term = x; // ADD THIS int limit = 100; int sign = -1; for(int i=3 ; i
... do you even listen?

When doing about 20-25 iterations, the approximation gives good results for the range -2π 2π, but not beyond that.
Surely you realize that cos and sin are periodic? And that is why you can use fmod to make sure x stays in that range.
By using fmod, you don't have to increase the number of iterations to get valid results for large x values.
But you've been told so on another forum already.
Last edited on