Lesson 17 of 18
Maclaurin Series for cos(x)
Maclaurin Series for
The Maclaurin series is a Taylor series centred at . For cosine:
This series converges for all real .
Building Terms Iteratively
Computing each term from scratch (with a factorial and power) is slow. Instead, use the recurrence:
Starting with :
double maclaurin_cos(double x, int terms) {
double sum = 0.0, term = 1.0;
for (int k = 0; k < terms; k++) {
sum += term;
term *= -x * x / ((2.0*k + 1) * (2.0*k + 2));
}
return sum;
}
Accuracy vs. Terms
| Terms | approximation | Error |
|---|---|---|
| 3 | 23% | |
| 5 | 0.03% | |
| 10 |
Key Values
cos(0) = 1
cos(π/2) = 0
cos(π) = -1
cos(2π) = 1
Your Task
Implement double maclaurin_cos(double x, int terms) using the iterative term recurrence.
TCC compiler loading...
Loading...
Click "Run" to execute your code.