Reputation: 480
First of all, let me tell you, I am learning programming.
Today, I tried to find the approximate value of cosine by using the taylor series. When I put n=0, my code gives me correct result of 1. But when I put n=1 or anything else, my code does not give correct result.
I am unable to understand where the problem is. Can anyone help?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
float xnot = atof(argv[1]);
float n = atof(argv[2]);
float cosine = cos(xnot*(3.14159265/180));
float result;
printf("%.4f\n", cosine);
float min;
float d, c, b;
c = 1;
d = 2 * n;
for(b = 1; b <= d; b++){
c = c * b; /*value of the factorial is in c*/
}
c = c;
float power;
power = pow((-1), n);
xnot = pow(xnot, 2*n);
for(min = 0; min <= n; min++)
{
result += ((power * xnot) / c);
}
printf("%.4f", result);
}
Upvotes: 0
Views: 139
Reputation: 44246
You need to redo all calculations inside the for-loop. Keeping as much as your original code as possible, it could be something like:
int n = atoi(argv[2]); // n is an integer
...
...
float result = 1; // The first term (n=0) gives 1
for(int i = 1; i <= n; i++) // Start the loop from 1
{
float d, c, b;
c = 1;
d = 2 * i; // Use i instead of n
for(b = 1; b <= d; b++){
c = c * b; /*value of the factorial is in c*/
}
float power;
power = pow((-1), i); // Use i instead of n
xnot = pow(xnot, 2*i); // Use i instead of n
result += ((power * xnot) / c);
}
The code can be optimized - both for performance and precision - but as already stated I tried to keep it close to your original code.
Upvotes: 1
Reputation: 84521
When computing either sine or cosine with Taylor series, you also need to take the angular quadrants into consideration to minimize error growth. The following is a short example:
#define TSLIM 20 /* Series Limit (no. of terms) */
...
/** cos with taylor series expansion to n = TSLIM
* (no function reliance, quadrants handled)
*/
double cosenfq (const double deg)
{
double fp = deg - (int64_t)deg, /* save fractional part of deg */
qdeg = (int64_t)deg % 360, /* get equivalent 0-359 deg angle */
rad, cose_deg = 1.0; /* radians, cose_deg */
int pos_quad = 1, /* positive quadrant flag 1,4 */
sign = -1; /* taylor series term sign */
qdeg += fp; /* add fractional part back to angle */
/* get equivalent 0-90 degree angle, set pos_quad flag */
if (90 < qdeg && qdeg <= 180) { /* in 2nd quadrant */
qdeg = 180 - qdeg;
pos_quad = 0;
}
else if (180 < qdeg && qdeg <= 270) { /* in 3rd quadrant */
qdeg = qdeg - 180;
pos_quad = 0;
}
else if (270 < qdeg && qdeg <= 360) /* in 4th quadrant */
qdeg = 360 - qdeg;
rad = qdeg * M_PI / 180.0; /* convert to radians */
/* compute Taylor-Series expansion for sine for TSLIM / 2 terms */
for (int n = 2; n < TSLIM; n += 2, sign *= -1) {
double p = rad;
uint64_t f = n;
for (int i = 1; i < n; i++) /* pow */
p *= rad;
for (int i = 1; i < n; i++) /* nfact */
f *= i;
cose_deg += sign * p / f; /* Taylor-series term */
}
return pos_quad ? cose_deg : -cose_deg;
}
With a 20 term limit, max error is approximately 1.2E-15
(compared to math.h cos()
)
Upvotes: 0
Reputation: 50007
When implementing the Taylor series you have to recompute the value of the terms for each value of 'n'. Here it looks like you've computed the value of -1^n
(as xnot
) for the maximum value of n
and then you're just multiplying by that value for each iteration. That's wrong. Same for the values of x^2n / (2n)!
- you have to recompute this for each value of n
as you increment it, then sum up the values.
Best of luck.
Upvotes: 2