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

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

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)

``````float sin(float x)
{
float result = x;

int limit = 15;
int sign = -1;

for(int i=3 ; i<limit ; i+=2,sign=-sign)
result += sign*result*(x*x)/(i*(i-1));

return result;
}``````

and this to approximate cos(x)

``````float cos(float x)
{
float result = 1;

int limit = 15;
int sign = -1;

for(int i=2 ; i<limit ; i+=2,sign=-sign)
result += sign*result*(x*x)/(i*(i-1));

return result;
}``````

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

I know how to fix your code, but to let you do it on your own, I will say this: the recursive formula (the second one) is used to compute each term of the first equation one after the other. In your implementation, you use "result" in the recursive formula, …

## All 23 Replies

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

The first and second formula has nothing to do in this case. The first formula
shows one definition on cos and sin, while the second shows another definition of
cos and sin. The second definition is a recursive definition.

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

The first and second formula has nothing to do in this case. The first formula
shows one definition on cos and sin, while the second shows another definition of
cos and sin. The second definition is a recursive definition.

hi,

so what should i use now the first formula or the second one? and why?

I know how to fix your code, but to let you do it on your own, I will say this: the recursive formula (the second one) is used to compute each _term_ of the first equation one after the other. In your implementation, you use "result" in the recursive formula, not the "term", you need to use the recursive formula to compute the terms and separately add them to get the result.

commented: thanks +1

welcome mike,

i see, later we can discuss about it(it looks easy)

but what about the first Q? what is my mistake?

well from what I understand for your two questions, it seems that you are already essentially computing approximations of sin and cos using Taylor series (first formula) with the recursive computation of the terms (second formula). You don't have a problem... one additional line of code in each function will solve the problem with the answer not being right at the end.

that's why i came here hehehe

but why you said the answer not will be right at the end

Ok.. well to spare you suspense, and since the code is so near to complete, I guess I can post the solution, for the sin only :)

``````float sin(float x)
{
float result = x;
float term = x;    // ADD THIS

int limit = 15;
int sign = -1;

for(int i=3 ; i<limit ; i+=2,sign=-sign) {
term = -term*x*x/(i*(i-1)); //
result += term;                 //make this change
};

return result;
}``````

it's me with the same nick name 'empror9'

it's me with the same nick name 'empror9'

I'm aware, but that's a correct solution for the cos approximation.

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?

I'm aware, but that's a correct solution for the cos approximation.

do you mean this code?

``````float cos_taylor(float x)
{
float result = 1;

int limit = 20;
int sign = -1;
float last=1;

for(int i=2 ; i<limit ; i+=2,sign=-sign)
{
last*=(x*x)/(i*(i-1));
result += sign*last;
}

return result;
}``````

i tried the function but it's not correct :(

i tried the function but it's not correct :(

What makes you think that?
It's correct.

how it's correct?, i tried to input 1.5707963

The taylor series just gets you an approximation of the real value when doing 20 iterations. The result -0.00000361132 is very close to zero, though. The result I'm getting is even closer.
If you want more precision, use double instead of float.
Then the function will give you exactly the same result as the "real" cos function (0.000000026795).

welcome Aranarth,

yse you right ,but 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

If I run the code that I have posted (which is correct, for sure!) I get the following:

Average error is: 0.038862 for limit = 15;
Average error is: 3.11248e-07 for limit = 30;

This was by calculating at every 0.01 rad increment between 0 and 2pi. This is totally as expected. Taylor series calculation is approximate! So expect some error.

One note on this, the Taylor series expansion is only good around a value of 0 rad (up to and beyond 2pi), but don't try to calculate the sin or cos on a really large value, keep it within the -2pi to 2pi range (you can even add a check at the start of your function to make sure the input is in the -pi to pi range).

welcome back mike,

but don't try to calculate the sin or cos on a really large value, keep it within the -2pi to 2pi range

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?

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?

Increase the variable "limit" to 30 or 40 or 100 and see what happens.

Increase the variable "limit" to 30 or 40 or 100 and see what happens.

welcome back mike,

ok i tried to increase the limit and the result is being more closley to the samples run in the project except this input 2.3145000, the result is 0.285567 but in sample run is 0.7359662

so etheir there is a small mistake in your function or may be i should to use the second fourmal because of this photo

now, sin function is wrok correctly

except cos function ((only if input is 1.5708000))

cos(x) = -3.67321e-006

but in sample run cos(x) = -0.000000

Well I don't know what weird thing is going on on your side, but copy/paste this:

``````#include <iostream>
#include <cmath>

using namespace std;

float sin_taylor(float x)
{
float result = x;
float term = x;    // ADD THIS

int limit = 30;
int sign = -1;

for(int i=3 ; i<limit ; i+=2,sign=-sign) {
term = -term*x*x/(i*(i-1)); //
result += term;                 //make this change
};

return result;
}

int main() {

int i = 0;
float sum = 0.0;
for(float x=0;x<6.28;x+=0.01, ++i) {
sum += fabs(sin(x) - sin_taylor(x));
};

sum /= i;

cout << "Average error is: " << sum << endl;

cout << "sin( 2.3145 ) = " << sin_taylor(2.3145) << endl;

return 0;
};``````

If the result in the console is not the following, then call an exorcist!
Average error is: 3.11248e-07
sin( 2.3145 ) = 0.735966

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.