Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- static double x(int i, int n);
- static double w(int n);
- static double lambda_inv(int k);
- static double T(int k, double x);
- static double f(double x);
- static double c(int k, int n);
- static double f_interpolation(int n, double x);
- static double x(int i, int n) {
- return cos(M_PI_2 / n * (2 * i - 1));
- }
- static double w(int n) {
- return M_PI / n;
- }
- static double lambda_inv(int k) {
- return (k == 0) ? M_1_PI : M_2_PI;
- }
- static double T(int k, double x) {
- return (k == 0) ? 1 : (k == 1) ? x : 2 * x * T(k - 1, x) - T(k - 2, x);
- }
- static double f(double x) {
- return 1. / (1 + 25 * x * x);
- }
- static double c(int k, int n) {
- double sum = 0.;
- for (int i = 1; i <= n; ++i) {
- double xi = x(i, n);
- sum += w(n) * T(k, xi) * f(xi);
- }
- return lambda_inv(k) * sum;
- }
- static double f_interpolation(int n, double x) {
- double sum = 0.;
- for (int k = 0; k < n; ++k) {
- sum += c(k, n) * T(k, x);
- }
- return sum;
- }
- int main(void) {
- FILE *out;
- char name[BUFSIZ];
- int ns[] = { 5, 9, 17 };
- for (size_t i = 0; i < sizeof(ns) / sizeof(ns[0]); ++i) {
- sprintf(name, "%d.dat", ns[i]);
- out = fopen(name, "w");
- for (int x = -100; x <= 100; ++x) {
- double xi = x / 100.;
- fprintf(out, "%lf %lf\n", xi, f_interpolation(ns[i], xi));
- }
- fclose(out);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement