Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <cstdio>
- double mycos(double x, double eps, int &n, double &rn) {
- double s = 0, u = 1;
- x = fabs(x);
- while (x >= 2.0 * M_PI) {
- x -= 2.0 * M_PI;
- }
- n = 1;
- while (fabs(u) >= eps) {
- s += u;
- u = - x * x / (2 * n * (2 * n - 1)) * u;
- n++;
- }
- rn = u;
- return s;
- }
- int main() {
- double a = 6.8;
- double b = 34.9;
- double x = (a + b) * 0.5;
- double h = (b - a) / 10.0;
- int n = 0;
- double rn = 0;
- printf("| eps | n | abs error | rem term |\n");
- for (double eps = 1e-2; eps > 1e-14; eps *= 1e-2) {
- double y = mycos(x, eps, n, rn);
- printf("| %.0e | %2i | %8.5e | %12.5e |\n", eps, n, fabs(cos(x) - y), fabs(rn));
- }
- printf("\n| xi | abs error | rem term |\n");
- for (int i = 0; i <= 10; i++) {
- double x = a + h * i;
- double y = mycos(x, 1e-8, n, rn);
- printf("| %6.2f | %8.5e | %12.5e |\n", x, fabs(cos(x) - y), fabs(rn));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement