Keyword: 多項式補間, チェビシェフ
概要
本サンプルは多項式補間のチェビシェフ級数表現の構築を行うC言語によるサンプルプログラムです。 本サンプルは以下に示されるデータについて補間を構築しチェビシェフ級数表現の係数を出力します。
※本サンプルはnAG Cライブラリに含まれる関数 nag_1d_cheb_interp() のExampleコードです。本サンプル及び関数の詳細情報は nag_1d_cheb_interp のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで
入力データ
(本関数の詳細はnag_1d_cheb_interp のマニュアルページを参照)1 2 3 4 5 6
このデータをダウンロード |
nag_1d_cheb_interp (e01aec) Example Program Data 4 2.0 6.0 0 2.0 1.0 1 4.0 2.0 -1.0 0 5.0 1.0 2 6.0 2.0 4.0 -2.0
- 1行目はタイトル行で読み飛ばされます。
- 2行目に独立変数の数(m)、xの下限(xmin)、xの上限(xmax)を指定しています。
- 3~6行目に最高階導関数の階数(p)、xの値、yの値を指定しています。
出力結果
(本関数の詳細はnag_1d_cheb_interp のマニュアルページを参照)1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
この出力例をダウンロード |
nag_1d_cheb_interp (e01aec) Example Program Results Total number of interpolating conditions = 7 Interpolating polynomial i Chebyshev Coefficient a(i+1) 0 9.1250 1 -4.5781 2 0.4609 3 2.8516 4 -2.8125 5 2.2266 6 -0.7109 x R Rth derivative Residual 2.0 0 1.0 0.000000 4.0 0 2.0 0.000000 1 -1.0 0.000000 5.0 0 1.0 0.000000 6.0 0 2.0 -0.000000 1 4.0 -0.000000 2 -2.0 0.000000
- 3行目に補間条件の合計数が出力されています。
- 7~14行目にチェビシェフ係数が出力されています。
- 16~23行目にxの値、導関数の階数、導関数と残差が出力されています。
ソースコード
(本関数の詳細はnag_1d_cheb_interp のマニュアルページを参照)
※本サンプルソースコードはnAG数値計算ライブラリ(Windows, Linux, MAC等に対応)の関数を呼び出します。
サンプルのコンパイル及び実行方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
このソースコードをダウンロード |
/* nag_1d_cheb_interp (e01aec) Example Program. * * CLL6I261D/CLL6I261DL Version. * * Copyright 2017 Numerical Algorithms Group. * * Mark 26.1, 2017. */ #include <stdio.h> #include <nag.h> #include <nag_stdlib.h> #include <nage01.h> int main(void) { /* Scalars */ double xmax, xmin; Integer exit_status, i, pmax, ires, iy, j, k, m, n, itmin, itmax, num_iter; NagError fail; /* Arrays */ double *a = 0, *perf = 0, *x = 0, *y = 0; Integer *p = 0; exit_status = 0; INIT_FAIL(fail); printf("nag_1d_cheb_interp (e01aec) Example Program Results\n"); /* Skip heading in data file */ scanf("%*[^\n] "); while (scanf("%ld%lf%lf%*[^\n] ", &m, &xmin, &xmax) != EOF) { if (m > 0) { /* Allocate memory for p and x. */ if (!(p = nAG_ALLOC(m, Integer)) || !(x = nAG_ALLOC(m, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } /* Read p, x and y arrays */ n = 0; pmax = 0; for (i = 1; i <= m; ++i) { scanf("%ld%lf", &p[i - 1], &x[i - 1]); k = n + p[i - 1] + 1; /* We need to extend array y as we go along */ if (!(y = nAG_REALLOC(y, k, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } for (j = n + 1; j <= k; ++j) scanf("%lf", &y[j - 1]); scanf("%*[^\n] "); if (p[i - 1] > pmax) pmax = p[i - 1]; n = k; } /* Allocate array a */ if (!(a = nAG_ALLOC(n, double)) || !(perf = nAG_ALLOC(pmax + n + 1, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } itmin = -1; itmax = -1; /* nag_1d_cheb_interp (e01aec). * Interpolating functions, polynomial interpolant, data may * include derivative values, one variable */ nag_1d_cheb_interp(m, xmin, xmax, x, y, p, itmin, itmax, a, perf, &num_iter, &fail); printf("\n"); if (fail.code == NE_NOERROR) { printf("Total number of interpolating conditions =" " %ld\n", n); printf("\n"); printf("Interpolating polynomial\n"); printf("\n"); printf(" i Chebyshev Coefficient a(i+1)\n"); for (i = 1; i <= n; ++i) printf("%4ld%20.4f\n", i - 1, a[i - 1]); printf("\n"); printf(" x R Rth derivative Residual\n"); iy = 0; ires = pmax + 1; for (i = 1; i <= m; ++i) { for (j = 1; j <= p[i - 1] + 1; ++j) { ++iy; ++ires; if (j - 1 != 0) printf(" %4ld%12.1f%17.6f\n", j - 1, y[iy - 1], perf[ires - 1]); else printf("%4.1f 0%12.1f%17.6f\n", x[i - 1], y[iy - 1], perf[ires - 1]); } } } else { printf("Error from nag_1d_cheb_interp (e01aec).\n%s\n", fail.message); exit_status = 1; } } } END: nAG_FREE(a); nAG_FREE(x); nAG_FREE(y); nAG_FREE(p); nAG_FREE(perf); return exit_status; }