Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C Program for sum of cos(x) series
We are given with the value of x and n where, x is the angle for cos and n is the number of terms in the cos(x) series. The cosine series allows us to calculate cos(x) using polynomial approximation instead of built-in trigonometric functions.
Cos(x) Function
Cos(x) is a trigonometric function which calculates the cosine of an angle. It can be represented as an infinite series using Taylor expansion.
Syntax
cos(x) = 1 - (x²/2!) + (x?/4!) - (x?/6!) + (x?/8!) - ...
Mathematical Formula
The cosine series expansion is −
cos(x) = 1 − (x² / 2!) + (x? / 4!) − (x? / 6!) + (x? / 8!) − ...
This can be written as: cos(x) = ?(k=0 to ?) [(-1)^k * x^(2k) / (2k)!]
Example 1: Basic Cosine Series Calculation
Calculate cos(x) using series expansion with a specified number of terms −
#include <stdio.h>
#include <math.h>
const double PI = 3.14159265359;
double cosine_series(double x, int n) {
/* Convert degrees to radians */
x = x * (PI / 180.0);
double result = 1.0; /* First term is 1 */
double term = 1.0;
for (int i = 1; i < n; i++) {
/* Calculate next term: (-1)^i * x^(2i) / (2i)! */
term = term * (-1) * x * x / ((2 * i - 1) * (2 * i));
result += term;
}
return result;
}
int main() {
double x = 60.0; /* 60 degrees */
int n = 10; /* 10 terms */
printf("cos(%.1f°) using %d terms = %.6f<br>", x, n, cosine_series(x, n));
printf("cos(%.1f°) using math.h = %.6f<br>", x, cos(x * PI / 180.0));
return 0;
}
cos(60.0°) using 10 terms = 0.500000 cos(60.0°) using math.h = 0.500000
Example 2: Comparing Different Number of Terms
This example shows how accuracy improves with more terms −
#include <stdio.h>
#include <math.h>
const double PI = 3.14159265359;
double cosine_series(double x, int n) {
x = x * (PI / 180.0);
double result = 1.0;
double factorial = 1.0;
double power = 1.0;
for (int i = 1; i < n; i++) {
power *= x * x;
factorial *= (2 * i - 1) * (2 * i);
if (i % 2 == 1) {
result -= power / factorial;
} else {
result += power / factorial;
}
}
return result;
}
int main() {
double x = 45.0;
double actual = cos(x * PI / 180.0);
printf("Angle: %.1f degrees<br>", x);
printf("Actual cos(x): %.6f<br>", actual);
printf("\nTerms\tApproximation\tError<br>");
printf("-----\t-------------\t-----<br>");
for (int terms = 2; terms <= 8; terms += 2) {
double approx = cosine_series(x, terms);
double error = fabs(actual - approx);
printf("%d\t%.6f\t%.6f<br>", terms, approx, error);
}
return 0;
}
Angle: 45.0 degrees Actual cos(x): 0.707107 Terms Approximation Error ----- ------------- ----- 2 0.691747 0.015360 4 0.707055 0.000052 6 0.707107 0.000000 8 0.707107 0.000000
Key Points
- Convergence: More terms provide better accuracy, especially for larger angles.
- Degree to Radian: Always convert degrees to radians using x * (?/180).
- Alternating Signs: Each term alternates between positive and negative.
- Factorial Growth: Denominators grow as (2k)! making later terms very small.
Comparison Table
| Method | Accuracy | Speed | Use Case |
|---|---|---|---|
| Few Terms (2-3) | Low | Fast | Quick approximation |
| Many Terms (8-10) | High | Slower | Precise calculation |
| Built-in cos() | Highest | Fastest | Production code |
Conclusion
The cosine series provides an excellent way to understand how trigonometric functions work internally. With sufficient terms, the series converges rapidly to the actual cosine value, making it useful for both educational purposes and custom implementations.
